How to programmatically set the Image source

后端 未结 6 1079
囚心锁ツ
囚心锁ツ 2021-02-01 14:16

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?

相关标签:
6条回答
  • 2021-02-01 14:16

    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"; 
    
    0 讨论(0)
  • 2021-02-01 14:17

    Try to assign the image that way instead:

    imgFavorito.Source = new BitmapImage(new Uri(base.BaseUri, @"/Assets/favorited.png"));
    
    0 讨论(0)
  • 2021-02-01 14:27

    Try this:

    BitmapImage image = new BitmapImage(new Uri("/MyProject;component/Images/down.png", UriKind.Relative));
    
    0 讨论(0)
  • 2021-02-01 14:37
    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".

    0 讨论(0)
  • 2021-02-01 14:39
    {yourImageName.Source = new BitmapImage(new Uri("ms-appx:///Assets/LOGO.png"));}
    

    LOGO refers to your image

    Hoping to help anyone. :)

    0 讨论(0)
  • 2021-02-01 14:39

    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);
    
    0 讨论(0)
提交回复
热议问题