C# 如何以编程方式设置图像源

C# 如何以编程方式设置图像源,c#,image,silverlight,code-behind,C#,Image,Silverlight,Code Behind,当图像的源属性按以下方式设置时,图片将从/Images/down.png拍摄 我如何以编程方式做同样的事情 <Image x:Name="myImg" Source="/MyProject;component/Images/down.png" /> 试试这个: BitmapImage image = new BitmapImage(new Uri("/MyProject;component/Images/down.png", UriKind.Relative)); 使用asp:i

当图像的源属性按以下方式设置时,图片将从
/Images/down.png
拍摄

我如何以编程方式做同样的事情

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

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

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

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);
不要忘记将构建操作设置为“内容”,并将复制到输出目录设置为“始终”

标志是指你的形象


希望能帮助任何人。:)

尝试以这种方式指定图像:

imgFavorito.Source = new BitmapImage(new Uri(base.BaseUri, @"/Assets/favorited.png"));

可能的重复:虽然Silverlight和WPF在许多方面相似,但我不会说这是重复的。特别是当涉及到资源位置时,请注意问题的标签上写着Silverlight。我没有在ASP中做任何事情。不过还是要感谢你的回复。哦,基本上,是Uri类进行了转换,具体取决于字符串中指定的内容。我不知道。然后:myImg.Source=image;如果指定绝对路径,请记住将其设置为UriKind.absolute。
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);
myImg.Source = new BitmapImage(new Uri(@"component/Images/down.png", UriKind.RelativeOrAbsolute)); 
{yourImageName.Source = new BitmapImage(new Uri("ms-appx:///Assets/LOGO.png"));}
imgFavorito.Source = new BitmapImage(new Uri(base.BaseUri, @"/Assets/favorited.png"));