gpt4 book ai didi

c# - 从特定文件加载图像

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:00 24 4
gpt4 key购买 nike

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

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

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"));

关于c# - 从特定文件加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42489555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com