对于一个需要 10-20 个小图标和图片进行说明的 WPF 应用程序,将它们存储在程序集中作为嵌入式资源是正确的选择吗?
如果是这样,请问我该如何在 XAML 中指定 Image 控件应该从嵌入式资源中加载图片?
对于一个需要 10-20 个小图标和图片进行说明的 WPF 应用程序,将它们存储在程序集中作为嵌入式资源是正确的选择吗?
如果是这样,请问我该如何在 XAML 中指定 Image 控件应该从嵌入式资源中加载图片?
如果您将在多个位置使用图像,则值得将图像数据仅加载一次到内存中,然后在所有Image
元素之间共享它。
要实现这一点,请在某个地方创建一个BitmapSource
资源:
<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />
然后,在您的代码中,使用类似以下的内容:
<Image Source="{StaticResource MyImageSource}" />
在我的情况下,我发现我必须将Image.png
文件的构建操作设置为Resource
,而不仅仅是Content
。这将导致图像被包含在编译后的程序集中。我发现使用图片、视频等的最佳实践是:
<Image Source="/WPFApplication;component/Images/Start.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
new Uri(@"pack://application:,,,/" + pathInApplication)
也解决了问题。 - Adam Calvet Bohl有些人在询问如何在代码中执行此操作,但并未得到答案。
经过多小时的搜索,我发现了一种非常简单的方法,我没有找到任何例子,因此在这里分享我的方法。该方法适用于图像(我的是.gif格式)。
概述:
它返回一个BitmapFrame,ImageSource“目标”似乎很喜欢。
使用:
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);
}
经验教训:
根据我的经验,打包字符串不是问题所在,检查流是否正确,特别是如果首次读取设置了指针到文件结尾,需要重新将其设置为零后再次读取。
希望这可以帮助你节省我曾经浪费的许多时间!
是的,这是正确的方法。
你可以使用资源文件中的图片,只需要使用路径即可:
<Image Source="..\Media\Image.png" />
您必须将图像文件的构建操作设置为“资源”。
这对我很有效:
<BitmapImage x:Key="MyImageSource" UriSource="Resources/Image.png" />
是的,这是正确的方式。您可以使用路径在资源文件中使用图像:
<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资源。
Images
的文件夹。Images
文件夹下添加MyImage.png
文件。MyImage.png
的属性窗口中,将Build Action
设置为Resource
。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>
<UserControl ...>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
...
<UserControl ...>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
...
<Image Source="{DynamicResource ResourceKey=ServiceLevel1Source}" />
...
DynamicResource
标记扩展而不是StaticResource
,假设你在编译时知道键。在WPF中,你可以在运行时创建资源字典。实际上,当你加载一个Xaml文档时,就会发生这种情况,只是你看不到等效的C#代码。 - Drew Noakes<UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Dictionary1.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> - Dan Mitchell
Image
中添加Width="{Binding Source.PixelWidth, RelativeSource={RelativeSource Self}}"
,否则我经常看到图像因某种原因变得非常扭曲(例如将16x16的图标拉伸到看起来像200x200像素)。 - O. R. Mapper