Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF将图像添加到画布而不将其作为资源添加_C#_.net_Wpf_Canvas - Fatal编程技术网

C# WPF将图像添加到画布而不将其作为资源添加

C# WPF将图像添加到画布而不将其作为资源添加,c#,.net,wpf,canvas,C#,.net,Wpf,Canvas,我正在尝试将图像添加到我的WPF应用程序画布。 据我所知,它们需要在VS解决方案中作为资源引用。 但是,我需要能够将图像复制到文件夹中,并从XML文件解析图像的相对Uri,然后将图像加载到画布中: Image image = new Image(); var pic = new BitmapImage(); pic.BeginInit(); pic.UriSource = new Uri(url, UriKind.Relative); // url is from

我正在尝试将图像添加到我的WPF应用程序画布。 据我所知,它们需要在VS解决方案中作为资源引用。 但是,我需要能够将图像复制到文件夹中,并从XML文件解析图像的相对Uri,然后将图像加载到画布中:

    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

感谢您的善意建议

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

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

“从XML文件解析图像的相对Uri”这一部分怎么样?@alterfox只需使用
Path.combined
来构建完整的URL-OP没有显示当前的URL生成,所以我不能包含这一部分…感谢您的解决方案