Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

Setting ImageBrush ImageSource error in Powershell

$
0
0

I have a xaml rectangle:

<Rectanglex:Name=" Width="150" Height="150" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="2,2,0,0"><Rectangle.Fill><ImageBrushImageSource="$($PSScriptRoot)\logo.jpg"/></Rectangle.Fill></Rectangle>

and want to change ImageScource based on URL:

$empURL ="https://in.portal.int/Images/Intranet/"+ $empUser.toLower()+".JPG"
$uri =new-object system.uri($empURL)
$imagesource =new-objectSystem.Windows.Media.Imaging.BitmapImage $uri
$imagebrush =new-objectSystem.Windows.Media.ImageBrush  $imagesource
$WPFimage.fill = $imagebrush

It works only one time, after starting PowerGUI script editor, all subsequent changes (even after restart debugging) ending in an error message:

new-object : Exception calling ".ctor" with "1" argument(s): "The system cannot find the file specified. (Exception from HRESULT: 0x80070002)" At C:\Users*****\Desktop\Tool\tl.ps1:113 char:20 +     $imagesource = new-object System.Windows.Media.Imaging.BitmapImage $uri +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Can you please help me to understand why it's working once, and how to get it to work? Tried also with

<Imagex:Name="image"Source="$($PSScriptRoot)\logo.jpg">

and

$WPFimage.source ="https://in.portal.int/Images/Intranet/"+ $empUser.toLower()+".JPG"

with similar results: the first replacement working OK, than there is a exception.


Viewing all articles
Browse latest Browse all 2314