I'm trying to set a WPF Image's source.
XAML works:
<Image Name="ImageThing"
Source="images/Thing.png"/>
Visual Basic fails:
ImageThing.Source = "images/Thing.png"
…with this exception:
Value of type 'String' cannot be converted to 'System.Windows.Media.ImageSource'.
How do I create the System.Windows.Media.ImageSource that I need?
This code adapted from an MSDN example works:
Dim bmp As New BitmapImage()
bmp.BeginInit()
bmp.UriSource = New Uri("images/Thing.png", UriKind.Relative)
bmp.EndInit()
ImageThing.Source = bmp
WPF uses an implicit type converter to convert the xaml string to the expected type. In code you are statically bound by the object type... If you look at the example here it shows how to set the source property to a BitmapImage that is generated from a local uri programatically.
you will probably need to do something like this
Uri i = new Uri("images\\Thing.png");
keep in mind that you need to use a \ not a / for a windows file system
Take a look here
It can be even easier than the above:
ImageThing.Source = New BitmapImage(New Uri("images/Thing.png", UriKind.Relative))
Just change xaml file like this.
<Image Name="ImageThing">
<Image.Source>
<BitmapImage UriSource="images/Thing.png" />
</Image.Source>
</Image>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With