2014-10-06 62 views
1

我正在嘗試將圖像添加到我的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 

感謝您這類通知:

回答

0

如果指定,而不是使用UriKind.Relative URI爲URI的完整路徑,它將正常工作:

pic.BeginInit(); 
pic.UriSource = new Uri(@"C:\Path\To\File.jpg"); 
pic.EndInit(); 
+0

怎麼樣的一部分:「從XML文件的*相對*烏里圖像被解析「? – alterfox 2014-10-06 20:49:13

+0

@alterfox只需使用'Path.Combine'來構建完整的URL -OP沒有顯示URL的生成,所以我不能包括... – 2014-10-06 20:57:47