如何从资源中获取 BitmapImage?

发布于 2024-07-21 12:13:23 字数 1317 浏览 10 评论 0 原文

我的程序集包含一个带有 BuildAction==Resource 的图像。 我想从此嵌入资源获取 BitmapImage。

我可以从文件中加载 BitmapImage,如下所示:

var bitmap = new BitmapImage(new Uri(path));

但是如何创建一个引用嵌入资源图像的 Uri?

当我尝试创建 'pack URI'(例如 pack://application:,,,/MyImage.pngpack://application:,,,/MyAssembly;component/MyImage .png),抛出异常:

System.UriFormatException “无效的 URI:需要一个端口,因为存在冒号 (':'),但无法解析该端口。”

我找到了针对此 博客文章< /a>

然而,应用该修复后,我在尝试从包 URI 加载 BitmapImage 时仍然遇到异常。

当使用 pack://application:,,,/Image.png 格式时,我得到一个 NullReferenceException,当使用 pack://application:,,,/AssemblyName;component 时,我得到一个 NullReferenceException /Image.png 格式时,我收到 NotSupportedException“无法识别 Uri 前缀”。


总结 我的问题是,在实例化任何 WPF 控件/窗口等之前,我尝试在进程中使用“pack URI”,因此“pack”URI 方案尚未注册(其他 WPF 所需的“东西”也不得注册)也可以设置,因为手动注册包方案本身并不能解决问题)。 解决方案是等到实例化我的 WPF 用户控件后才使用包 URI。

My assembly includes an image with BuildAction==Resource. I want to obtain a BitmapImage from this embedded resource.

I can load a BitmapImage from file like this:

var bitmap = new BitmapImage(new Uri(path));

But how to I create a Uri that will refer to an embedded resource image?

When I try and create a 'pack URI' (for example pack://application:,,,/MyImage.png or pack://application:,,,/MyAssembly;component/MyImage.png), an exception is thrown:

System.UriFormatException
"Invalid URI: A port was expected because of there is a colon (':') present but the port could not be parsed."

I found the fix, to the UriFormatException in this blog post

However, with that fix applied, I still get exceptions trying to load a BitmapImage from a pack URI.

When using the pack://application:,,,/Image.png format, I get a NullReferenceException, and when using the pack://application:,,,/AssemblyName;component/Image.png format, I get a NotSupportedException "The Uri prefix is not recognized".


Summary
My problem was that I was trying to use a 'pack URI' in a process before any WPF control/window/etc had been instantiated, so the 'pack' URI scheme was not yet registered (other WPF required 'stuff' must also not be set too, because manually registering the pack scheme doesn't itself fix the problem). The solution was to wait until after instantiating my WPF usercontrol to use pack URIs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2024-07-28 12:13:23

此 MSDN 页面包含您可能想了解的有关 WPF 中的资源 URI(通常称为包 URI)的所有信息。 您可能希望更频繁地使用相对 URI,因此请参阅表 4,它应该具有特殊用途。

如果您想更简要地了解资源(包)URI,请参阅 此博文。 它表明语法确实相对简单:

pack://application:,,,/ResourceFile.xaml

pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

但是,有一些怪癖需要解决(根据我的经验),因此找到正确的资源 URI 通常需要进行一些实验。

This MSDN page has all the information you might want to know about resource URIs in WPF (often called pack URIs). You're going to want to use relative URIs more often probably, so see Table 4, which should be of particular use.

If you want a briefer overview of resource (pack) URIs, see this blog post. It shows that syntax is indeed relatively simple:

pack://application:,,,/ResourceFile.xaml

pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

However, there are a few quirks to work out (in my experience), so often finding the correct resource URI requires a bit of experimentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文