从特定文件加载图像
问题描述:
如何将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 action
到Resource
然后使用下面的代码解决了我的问题。
image.Source = new BitmapImage(new Uri(@"pack://application:,,,/Resources/panamaxi.png"));
答
这应该工作:
image.Source = new BitmapImage(new Uri("kafpodhlato.png", UriKind.Relative));
您可以但是要由Pack URI从装配资源加载图像。
将图像文件添加到Visual Studio项目中,例如到名为Images
的文件夹。然后设置其Build Action
到Resource
,并可能使用其他`Uri`构造函数通过
image.Source = new BitmapImage(new Uri("pack://application:,,,/Images/kafpodhlato.png"));
加载它? –