Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.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图像控件不支持';t显示以编程方式添加/分配的ImageSource_C#_Wpf_Image - Fatal编程技术网

C# WPF图像控件不支持';t显示以编程方式添加/分配的ImageSource

C# WPF图像控件不支持';t显示以编程方式添加/分配的ImageSource,c#,wpf,image,C#,Wpf,Image,下面的此方法将图像从图像文件路径转换为图像源 下面是我测试过的路径 以下是分配imagesource的方式: Image_Control.Source = GetImageSourceFromPath(path); 问题是该图像没有显示在WPF图像控件中 任何帮助都将不胜感激。谢谢。通过以下更改,您的代码可以正常工作 public static ImageSource GetImageSourceFromPath(string path) { return new

下面的此方法将图像从图像文件路径转换为图像源

下面是我测试过的路径

以下是分配imagesource的方式:

Image_Control.Source = GetImageSourceFromPath(path);
问题是该图像没有显示在WPF图像控件中


任何帮助都将不胜感激。谢谢。

通过以下更改,您的代码可以正常工作

 public static ImageSource GetImageSourceFromPath(string path)
     {
         return new BitmapImage(new Uri(path));
     }

    Image_Control.Source = GetImageSourceFromPath(path);

如果您使用的是完整路径,则无需指定
UriKind
,否则请使用
UriKind.RelativeOrbSolute

,您应将
UriKind.RelativeOrbSolute
更改为
UriKind.RelativeOrbSolute
。它允许你设置图像的绝对或相对路径。非常感谢你花时间回答我的问题。
public static ImageSource GetImageSourceFromPath(string path)
 {
     return new BitmapImage(new Uri(path, **UriKind.RelativeOrAbsolute**));
 }
public static ImageSource GetImageSourceFromPath(string path)
 {
     return new BitmapImage(new Uri(path, **UriKind.RelativeOrAbsolute**));
 }