从特定文件加载图像

从特定文件加载图像

问题描述:

如何将ImageSource设置为来自特定文件夹的图像?从特定文件加载图像

我试图移动在调试图像和运行下面的代码:

image.Source = new BitmapImage(new Uri("kafpodhlato.png")); 

,但我得到了以下错误:

An unhandled exception of type 'System.UriFormatException' occurred in System.dll

Additional information: Invalid URI: The format of the URI could not be determined.

编辑:创建一个文件夹命名为资源,设置它的Build actionResource然后使用下面的代码解决了我的问题。

image.Source = new BitmapImage(new Uri(@"pack://application:,,,/Resources/panamaxi.png")); 
+0

加载它? –

这应该工作:

image.Source = new BitmapImage(new Uri("kafpodhlato.png", UriKind.Relative)); 

您可以但是要由Pack URI从装配资源加载图像。

将图像文件添加到Visual Studio项目中,例如到名为Images的文件夹。然后设置其Build ActionResource,并可能使用其他`Uri`构造函数通过

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

[MSDN链接](https://msdn.microsoft.com/zh-cn/library/ms131565)。 –

+0

试过了,没有错误,但图像不会加载...也许我必须更好地指定路径?或者将图像放在Debug中解决了这个问题? – ThP

+0

@ThP看我的编辑。你应该让你的镜像文件成为一个程序集资源。 – Clemens