How to programmatically set the Image source

Kornelije Petak picture Kornelije Petak · Jun 28, 2011 · Viewed 135.7k times · Source

When the Image's Source property is set the following way, the picture is taken from /Images/down.png.

How would I do the same thing programmatically?

<Image x:Name="myImg" Source="/MyProject;component/Images/down.png" />

The following would not work, since Image.Source property is not of a string type.

myImg.Source = "/MyProject;component/Images/down.png"; 

Answer

Chris Grant picture Chris Grant · Jun 28, 2011

Try this:

BitmapImage image = new BitmapImage(new Uri("/MyProject;component/Images/down.png", UriKind.Relative));