本文介绍了从code加载图像在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定有办法解决的读吨在线,但是对于一些愚蠢的原因,我不能让他们的工作。

I must have read tons of solutions online, but for some idiotic reason I can not get them to work.

我在我的项目的资源文件夹为.jpg图像,并且图像设置为构建行动:资源(不嵌入的资源),从不复制到输出文件夹。我的形象的没有的添加到我的resources.resx文件。

I have a .jpg image in the Resources folder of my project, and the image is set to Build Action: Resource (not embedded resource) and never copy to output folder.My image is not added to my resources.resx file.

我试图访​​问该文件,如下所示:

I am trying to access the file like so:

lResult = new BitmapImage(new Uri(@"pack://application:,,,/Resources/ImageMissing.jpg", UriKind.Absolute));

但这个失败说,没有图像出现。

But this fails saying that there is no image there.

这是如此基本的我觉得自己很愚蠢,但我似乎无法掌握资源使用的简单的概念。

This is so basic I feel really stupid, but I just cannot seem to grasp the simple concept of resources usage.

感谢你。

推荐答案

如果您需要在指定的资源被称为是从当地组装引用,那么我会认为你会希望有分量。例如,我有一些code,从产品,在其中我的code驻留在同一组件上的资源加载一个图标。我写这篇文章:

If you need to specify that the resource being referred to is referenced from the local assembly, then I would think that you would want to include "component". For example, I have some code that loads an icon from a resource available from the same assembly in which my code resides. I write this:

var SourceUri = new Uri("pack://application:,,,/MyCompany.MyProduct.MyAssembly;component/MyIcon.ico", UriKind.Absolute);
thisIcon = new BitmapImage(SourceUri);

作为可在http://msdn.microsoft.com/en-us/library/aa970069.aspx,下面的附加实施例说明了使用组分:

As noted in the article available at http://msdn.microsoft.com/en-us/library/aa970069.aspx, the following additional examples illustrate the use of "component":

下面的示例显示了位于引用的程序集的项目文件夹的根目录的XAML资源文件包URI。

The following example shows the pack URI for a XAML resource file that is located in the root of the referenced assembly's project folder.

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

下面的示例显示了位于引用的程序集的项目文件夹的子文件夹一个XAML资源文件包URI。

The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.

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

下面的例子显示了包的URI,它位于一个引用,特定版本的程序集的项目文件夹的根目录文件夹中的XAML资源文件。

The following example shows the pack URI for a XAML resource file that is located in the root folder of a referenced, version-specific assembly's project folder.

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

请注意,该包URI语法引用的程序集资源文件只能与应用程序中使用:///权威。例如,下面是不是在WPF支持。

Note that the pack URI syntax for referenced assembly resource files can be used only with the application:/// authority. For example, the following is not supported in WPF.

pack://siteoforigin:,,,/SomeAssembly;component/ResourceFile.xaml

这篇关于从code加载图像在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:43