43

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"; 
Cœur
  • 34,719
  • 24
  • 185
  • 251
Kornelije Petak
  • 9,172
  • 14
  • 67
  • 96

6 Answers6

82

Try this:

BitmapImage image = new BitmapImage(new Uri("/MyProject;component/Images/down.png", UriKind.Relative));
Chris Grant
  • 2,435
  • 22
  • 27
15
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".

AnjumSKhan
  • 9,389
  • 1
  • 23
  • 36
5

Try to assign the image that way instead:

imgFavorito.Source = new BitmapImage(new Uri(base.BaseUri, @"/Assets/favorited.png"));
Maxime Rouiller
  • 13,453
  • 9
  • 55
  • 106
3
{yourImageName.Source = new BitmapImage(new Uri("ms-appx:///Assets/LOGO.png"));}

LOGO refers to your image

Hoping to help anyone. :)

fady zarif
  • 79
  • 1
  • 11
-1

try this

PictureBox picture = new PictureBox
        {
            Name = "pictureBox",
            Size = new Size(100, 50),
            Location = new Point(14, 17),
            Image = Image.FromFile(@"c:\Images\test.jpg"),
            SizeMode = PictureBoxSizeMode.CenterImage
        };
p.Controls.Add(picture);
IvanH
  • 4,812
  • 13
  • 58
  • 75
Anonymous
  • 197
  • 1
  • 4
  • 14
-1

Use asp:image

<asp:Image id="Image1" runat="server"
           AlternateText="Image text"
           ImageAlign="left"
           ImageUrl="images/image1.jpg"/>

and codebehind to change image url

Image1.ImageUrl = "/MyProject;component/Images/down.png"; 
ChrisBint
  • 12,658
  • 6
  • 36
  • 56