存储WPF映像资源

对于一个WPF应用程序,它将需要10 - 20个小图标和图像用于说明目的,将它们作为嵌入式资源存储在程序集中是正确的方法吗?

如果是这样,我如何在XAML中指定图像控件应该从嵌入式资源加载图像?

499074 次浏览

是的,这是正确的方法。

你可以在资源文件中使用图像,只使用路径:

<Image Source="..\Media\Image.png" />

您必须将映像文件的构建操作设置为“资源”。

如果你正在使用混合,为了使它更加简单,并且没有任何问题,为属性获得正确的路径,只需将图像从项目面板拖放到设计器上。

如果你将在多个地方使用图像,那么值得只将图像数据加载到内存中一次,然后在所有Image元素之间共享它。

为此,在某处创建BitmapSource作为资源:

<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />

然后,在你的代码中,使用如下代码:

<Image Source="{StaticResource MyImageSource}" />

在我的例子中,我发现我必须将Image.png文件设置为具有Resource的构建操作,而不仅仅是Content。这将导致在已编译程序集中携带映像。

我发现使用图片、视频等的最佳做法是:

    将你的文件“构建action"改为“Content"。一定要检查复制到构建目录
    • 找到&;右键点击&;菜单中的解决方案资源管理器窗口。
  • 图片来源格式如下:
    • “/ < em >«YourAssemblyName»< / em >;组件/«YourPath»/ < em >«YourImage.png»< / em >“

例子

<Image Source="/WPFApplication;component/Images/Start.png" />

好处:

  • 文件未嵌入程序集。
    • 资源管理器会因为太多的资源(在构建时)而引发一些内存溢出问题。
  • 可以在程序集之间调用。

如何使用资源的完整描述:WPF应用程序资源,内容和数据文件

以及如何引用它们,请阅读“在WPF中打包uri”。

简而言之,甚至还有从被引用/引用程序集引用资源的方法。

有些人问在代码中这样做,但没有得到答案。

在花了很多时间搜索之后,我找到了一个非常简单的方法,我没有找到例子,所以我在这里分享我的方法 它适用于图像。(我的是。gif格式)

简介:

它返回一个ImageSource“目的地”似乎喜欢的BitmapFrame。

使用:

doGetImageSourceFromResource ("[YourAssemblyNameHere]", "[YourResourceNameHere]");

方法:

static internal ImageSource doGetImageSourceFromResource(string psAssemblyName, string psResourceName)
{
Uri oUri = new Uri("pack://application:,,,/" +psAssemblyName +";component/" +psResourceName, UriKind.RelativeOrAbsolute);
return BitmapFrame.Create(oUri);
}

经验:

根据我的经验,包字符串不是问题,检查你的流,特别是如果第一次读取它已经设置了指针

.

.

.

.

我希望这能节省你很多时间,我希望这篇文章为我!

  1. Visual Studio 2010 Professional SP1。
  2. . net Framework 4客户端配置文件。
  3. 在项目属性上添加PNG图像作为资源。
  4. 自动在资源文件夹中创建新文件。
  5. 构建操作集到资源。

这招对我很管用:

<BitmapImage x:Key="MyImageSource" UriSource="Resources/Image.png" />

在代码中加载执行程序集中的资源,其中我的图像Freq.png位于文件夹Icons中,并定义为Resource:

this.Icon = new BitmapImage(new Uri(@"pack://application:,,,/"
+ Assembly.GetExecutingAssembly().GetName().Name
+ ";component/"
+ "Icons/Freq.png", UriKind.Absolute));

我还做了一个函数:

/// <summary>
/// Load a resource WPF-BitmapImage (png, bmp, ...) from embedded resource defined as 'Resource' not as 'Embedded resource'.
/// </summary>
/// <param name="pathInApplication">Path without starting slash</param>
/// <param name="assembly">Usually 'Assembly.GetExecutingAssembly()'. If not mentionned, I will use the calling assembly</param>
/// <returns></returns>
public static BitmapImage LoadBitmapFromResource(string pathInApplication, Assembly assembly = null)
{
if (assembly == null)
{
assembly = Assembly.GetCallingAssembly();
}


if (pathInApplication[0] == '/')
{
pathInApplication = pathInApplication.Substring(1);
}
return new BitmapImage(new Uri(@"pack://application:,,,/" + assembly.GetName().Name + ";component/" + pathInApplication, UriKind.Absolute));
}

用法(假设将函数放在ResourceHelper类中):

this.Icon = ResourceHelper.LoadBitmapFromResource("Icons/Freq.png");

请注意:参见WPF中的MSDN uri包:
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml

下面工作和待设置的图像是在属性中的资源:

    var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(MyProject.Properties.Resources.myImage.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
MyButton.Background = new ImageBrush(bitmapSource);
img_username.Source = bitmapSource;

是的,这条路对。你可以使用一个路径在资源文件中使用图像:

<StackPanel Orientation="Horizontal">
<CheckBox  Content="{Binding Nname}" IsChecked="{Binding IsChecked}"/>
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\sitepermission.png"/>
<TextBlock Text="{Binding Path=Title}"></TextBlock>
</StackPanel>

根据Drew Noakes的回答,以下是创建资源字典、向其中添加BitmapImage资源以及在用户控件中引用BitmapImage资源的完整步骤。

  1. 在项目根目录下添加Images文件夹。
  2. Images文件夹下添加MyImage.png
  3. MyImage.png属性窗口中,将Build Action设置为Resource
  4. 在项目根目录下创建一个名为MainResourceDictionary.xaml的资源字典:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<BitmapImage x:Key="MyImageSource" UriSource="Images/MyImage.png" />
</ResourceDictionary>
  1. 在控件中添加对资源字典的引用:
<UserControl ...>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
...
  1. 在控件中引用图像资源:
<UserControl ...>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
...
<Image Source="{DynamicResource ResourceKey=ServiceLevel1Source}" />
...