WPF将图像添加到画布而不将它们添加为资源
问题描述:
我正在尝试将图像添加到我的WPF应用程序画布。 根据我的理解,他们需要在VS解决方案中被引用为资源。 不过,我需要能够将图像复制到文件夹,并从XML文件图像的相对URI进行解析,并将图像加载到画布:WPF将图像添加到画布而不将它们添加为资源
Image image = new Image();
var pic = new BitmapImage();
pic.BeginInit();
pic.UriSource = new Uri(url, UriKind.Relative); // url is from the xml
pic.EndInit();
image.Source = pic;
LayoutRoot.Children.Add(image); //since the image is not in VS marked as Resource,
// nothing shows up
感谢您这类通知:
答
如果指定,而不是使用UriKind.Relative
URI为URI的完整路径,它将正常工作:
pic.BeginInit();
pic.UriSource = new Uri(@"C:\Path\To\File.jpg");
pic.EndInit();
怎么样的一部分:“从XML文件的*相对*乌里图像被解析“? – alterfox 2014-10-06 20:49:13
@alterfox只需使用'Path.Combine'来构建完整的URL -OP没有显示URL的生成,所以我不能包括... – 2014-10-06 20:57:47