Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set the Image 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";  
like image 538
Kornelije Petak Avatar asked Jun 28 '11 08:06

Kornelije Petak


People also ask

How do I change the source of a picture on android?

Using setBackgroundResource() method: myImgView. setBackgroundResource(R.

What is SRC in ImageView?

The background will stretch according to the length given by the ImageView component, and SRC will hold the size of the original image without stretching. SRC is the picture content (foreground), BG is the background, can be used at the same time.

What is the use of set image resource method?

If you don't want to set image to xml file and show image programmatically then you need to use this method. For E.g. You set image in xml which showing ON Switch now when you click ON Button its should be set to OFF Switch so in that case you can set using this setImageResource() programmatically.


2 Answers

Try this:

BitmapImage image = new BitmapImage(new Uri("/MyProject;component/Images/down.png", UriKind.Relative)); 
like image 80
Chris Grant Avatar answered Sep 29 '22 11:09

Chris Grant


myImg.Source = new BitmapImage(new Uri(@"component/Images/down.png", UriKind.RelativeOrAbsolute));  

Don't forget to set Build Action to "Content", and Copy to output directory to "Always".

like image 25
AnjumSKhan Avatar answered Sep 29 '22 10:09

AnjumSKhan