改变图像元素的来源

本文关键字:元素 图像 图像元 改变 | 更新日期: 2023-09-27 18:14:03

我尝试更改image1的图片。在集合中我有"1.png"answers"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(在评论中)提供的链接。