WPF: "A value of type" String "cannot be converted to" System.Windows.Media.ImageSource "."

I am trying to set the WPF image source.

XAML works:

<Image Name="ImageThing" Source="images/Thing.png"/> 

Visual Basic Error :

 ImageThing.Source = "images/Thing.png" 

& hellip; with this exception :

A value of type 'String' cannot be converted to 'System.Windows.Media.ImageSource'.

How do I create the System.Windows.Media.ImageSource I need?


Update

This code, adapted from the MSDN example , works:

 Dim bmp As New BitmapImage() bmp.BeginInit() bmp.UriSource = New Uri("images/Thing.png", UriKind.Relative) bmp.EndInit() ImageThing.Source = bmp 
+6
data-binding image wpf
source share
5 answers

WPF uses an implicit type converter to convert the xaml string to the expected type. In the code, you are statically linked by the type of the object ... If you look at the example here , it shows how to set the source property in BitmapImage which is generated from the local uri programmatically.

+11
source share

you probably need to do something like this

 Uri i = new Uri("images\\Thing.png"); 

Keep in mind that you need to use \ not a / for the Windows file system

Look here

+5
source share

It may even be simpler than stated above:

ImageThing.Source = New BitmapImage(New Uri("images/Thing.png", UriKind.Relative))

+3
source share

Just modify the xaml file as follows.

 <Image Name="ImageThing"> <Image.Source> <BitmapImage UriSource="images/Thing.png" /> </Image.Source> </Image> 
+1
source share

What about

 Private Sub Google_Click(sender As Object, e As RoutedEventArgs) Mainbrowser.Navigate("http://www.contoso.com") 
-one
source share

All Articles