从特定文件加载图像
Load image from specific file
如何将 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.
编辑:创建一个名为 Resources 的文件夹,将其 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
,并通过
加载
image.Source = new BitmapImage(new Uri("pack://application:,,,/Images/kafpodhlato.png"));
如何将 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.
编辑:创建一个名为 Resources 的文件夹,将其 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
,并通过
image.Source = new BitmapImage(new Uri("pack://application:,,,/Images/kafpodhlato.png"));