wpf 如何从资源中获取 BitmapImage?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/857732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:25:13  来源:igfitidea点击:

How can I get a BitmapImage from a Resource?

wpfresourcesbitmapimagepack-uri

提问by mackenir

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

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

I can load a BitmapImage from file like this:

我可以像这样从文件加载 BitmapImage:

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

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

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

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

当我尝试创建一个“ pack URI”(例如pack://application:,,,/MyImage.pngpack://application:,,,/MyAssembly;component/MyImage.png)时,会抛出异常:

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

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

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

我在这篇文中找到了解决 UriFormatException的方法

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

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

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

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



SummaryMy 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.

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

采纳答案by Noldorin

This MSDN pagehas 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 URIsmore often probably, so see Table 4, which should be of particular use.

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

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

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

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.

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