繁体   English   中英

更改图像元素的来源

[英]Changing source of image element

我尝试更改image1图片。 在集合中,我有“ 1.png”和“ 2.png”。

image1的属性中, Source为“ /Appname;component/pictures/1.png”

我写:

image1.Source = "/Appname;component/pictures/2.png";

但它当然不起作用,因为“不可能从字符串到System.Windows.Media.ImageSource的隐式转换”。

如何将image1的图片更改为“ 2.png”?

您不能直接将string指定为图像源。 但是您可以使用BitmapImage作为源。

例:

BitmapImage bitmap = new BitmapImage(new Uri("/Appname;component/pictures/2.png", UriKind.Relative));
image1.Source = bitmap;

如果这不起作用,则您的Uri (图像路径)出了点问题,您应该看看Zannjaminderson提供的链接(在注释中)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM