Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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# 从特定文件加载图像_C#_Wpf_Blend - Fatal编程技术网

C# 从特定文件加载图像

C# 从特定文件加载图像,c#,wpf,blend,C#,Wpf,Blend,如何将ImageSource设置为特定文件夹中的图像 我尝试在调试中移动图像并运行以下代码: image.Source = new BitmapImage(new Uri("kafpodhlato.png")); 但我得到了以下错误: 中发生类型为“System.UriFormatException”的未处理异常 System.dll 其他信息:无效的URI:无法确定URI的格式 下定决心 编辑:创建一个名为Resources的文件夹,将其构建操作设置为Resource,然后使用以下代码解决了

如何将ImageSource设置为特定文件夹中的图像

我尝试在调试中移动图像并运行以下代码:

image.Source = new BitmapImage(new Uri("kafpodhlato.png"));
但我得到了以下错误:

中发生类型为“System.UriFormatException”的未处理异常 System.dll

其他信息:无效的URI:无法确定URI的格式 下定决心

编辑:创建一个名为Resources的文件夹,将其构建操作设置为Resource,然后使用以下代码解决了我的问题

image.Source = new BitmapImage(new Uri(@"pack://application:,,,/Resources/panamaxi.png"));
这应该起作用:

image.Source = new BitmapImage(new Uri("kafpodhlato.png", UriKind.Relative));
但是,您可能希望通过从程序集资源加载映像

将图像文件添加到Visual Studio项目中,例如添加到名为Images的文件夹中。然后将其构建操作设置为Resource,并通过

image.Source = new BitmapImage(new Uri("pack://application:,,,/Images/kafpodhlato.png"));
这应该起作用:

image.Source = new BitmapImage(new Uri("kafpodhlato.png", UriKind.Relative));
但是,您可能希望通过从程序集资源加载映像

将图像文件添加到Visual Studio项目中,例如添加到名为Images的文件夹中。然后将其构建操作设置为Resource,并通过

image.Source = new BitmapImage(new Uri("pack://application:,,,/Images/kafpodhlato.png"));

可能使用另一个Uri构造函数?可能使用另一个Uri构造函数?尝试过,没有错误,但图像不会加载。。。也许我必须更好地指定路径?或者将图像放入调试可以解决此问题?@ThP请参阅我的编辑。您应该将图像文件设置为程序集资源。您能解释一下如何将其生成操作设置为资源吗?除名称外,我当前没有要更改的属性。请参见此处:。在VS中,打开文件的“内容”菜单并选择“属性”。是否确实在包URI中使用了正确的项目文件夹名称?图像呢,它是用XAML声明的吗?你确定它是可见的吗?有很多事情可能是错误的,但我们只能猜测。尝试了,没有错误,但图像不会加载。。。也许我必须更好地指定路径?或者将图像放入调试可以解决此问题?@ThP请参阅我的编辑。您应该将图像文件设置为程序集资源。您能解释一下如何将其生成操作设置为资源吗?除名称外,我当前没有要更改的属性。请参见此处:。在VS中,打开文件的“内容”菜单并选择“属性”。是否确实在包URI中使用了正确的项目文件夹名称?图像呢,它是用XAML声明的吗?你确定它是可见的吗?有很多事情可能是错误的,但我们只能猜测。