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?
Update
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.