.ico 中的图像作为 DynamicResource

发布于 2024-11-14 10:40:24 字数 751 浏览 3 评论 0原文

情况如下:

  • 我的应用程序中有很多图标,并且它们以几种不同的尺寸使用。
  • 我使用图标作为动态资源,例如:

  • 一些图标采用.xaml格式,一些采用.png格式

  • 我添加新图标,例如:

问题:
我想要 .ico 格式的图标,我可以将其用作动态资源。

我想要 .ico 格式的图像,因为这种文件格式允许在一个文件中包含几种不同的图像大小。 .xaml 中的图标可以完全调整大小,但加载需要很长时间(因为我真的有很多图标!)。

是否可以将 .ico 文件添加为 DynamicResource 并向其添加 x:key
即使我以某种方式添加这些 .ico 图像,它们会改变大小(取决于它们有多少位置)?

Situation looks like that:

  • I have many icons in application and they are used in few different sizes.
  • I use icons as DynamicResource for example like that:

    <igRibbon:MenuTool (...) LargeImage="{DynamicResource IconAdd}" />

    <s:Item (...) Icon="{DynamicResource IconAdd}"/>

  • Some of icons are in .xaml and some in .png format

  • I add new icons for example like that:
    <BitmapImage x:Key="IconAdd" UriSource="../Icons/IconAdd.png" />

The problem:
I would like to have icons in .ico format that I can use as DynamicResource.

I want images in .ico because this file format allows to have few different image sizes in one file. Icons in .xaml are fully resizable but they took to much time to load (because I have really a lot of them!).

Is it possible to add .ico file as DynamicResource and add x:key to it?
Even if I somehow add those .ico images, will they change size (depending on how much place they have)?

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

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

发布评论

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

评论(1

独孤求败 2024-11-21 10:40:24

是的,可以将 .ico 文件添加为 DynamicResource(当然,因为它是资源,所以它必须有一个 x:Key)。

然而,它们不会自动改变大小。每个尺寸都可以像这样从 .ico 中提取,其中我为每个图标的框架创建一个 Image ,设置为框架的确切尺寸,然后添加该 Image > 到名为 imageStackStackPanel

var iconUri = new URI( "pack://application:,,,/MyIcon.ico", UriKind.RelativeOrAbsolute );
var iconDecoder = new IconBitmapDecoder( iconUri,
    BitmapCreationOptions.None, BitmapCacheOption.Default );

foreach ( var frame in iconDecoder.Frames ) {
    var img = new Image(){
        Height = frame.PixelHeight,
        Width = frame.PixelWidth,
        Source = frame }
    imageStack.Children.Add( img );
}

当您直接使用 .ico 时,它将选择具有最大像素分辨率的帧,并根据该位图的大小调整该位图的大小图像控件,或无论您的 ImageButton 或显示它的其他控件有什么内容对齐/大小调整属性。

用于控制添加哪个框架的一些选项并不完整,但可以作为解决方案的想法:

  • 以编程方式将图标拆分为 BitmapFrames 并将它们添加到 ResourceDictionary > 带有 'MyIcon16''MyIcon32' 等键。
  • 创建一个 MarkupExtensionIValueConverter 以提取符合特定条件(例如索引或大小)的帧。
  • 由于您使用的是DynamicResource,因此您可以随时更改与特定资源键关联的框架。
  • 您可以通过范围来控制它。您可以使用 'MyIcon' 键将 32x32 帧作为 WindowResourceDictionary 中的资源,并将 64x64 帧作为在不同范围内具有相同键的资源,例如该 Window 内的 Grid。在 Grid 中使用 {DynamicResource MyIcon} 的任何内容都将显示 64x64 帧,而窗口中的其他所有内容都显示 32x32 帧。

Yes, it is possible to add .ico files as a DynamicResource (and of course, because it is a resource, it must have an x:Key).

They will not automatically change size, however. Each size is extractable from the .ico like this, in which I make an Image for each of the icon's frames set to the exact size of the frame, and then add that Image to a StackPanel called imageStack:

var iconUri = new URI( "pack://application:,,,/MyIcon.ico", UriKind.RelativeOrAbsolute );
var iconDecoder = new IconBitmapDecoder( iconUri,
    BitmapCreationOptions.None, BitmapCacheOption.Default );

foreach ( var frame in iconDecoder.Frames ) {
    var img = new Image(){
        Height = frame.PixelHeight,
        Width = frame.PixelWidth,
        Source = frame }
    imageStack.Children.Add( img );
}

When you just use the .ico directly, it will choose whichever frame has the largest pixel resolution and resize that bitmap based on the size of the Image control, or whatever content alignment/sizing properties are in place for your Image, Button, or other control that is displaying it.

A few options for controlling which frame is added that aren't complete, but may serve as ideas towards a solution:

  • Programmatically split apart the icon into BitmapFrames and add them to the ResourceDictionary with keys like 'MyIcon16', 'MyIcon32', and so on.
  • Create a MarkupExtension or IValueConverter to extract a frame matching certain criteria such as index or size.
  • Since you are using DynamicResource, you can change which frame is associated with a particular resource key at any time.
  • You could control it by scope. You could have the 32x32 frame as a resource in the Window's ResourceDictionary with the key 'MyIcon', and have the 64x64 frame as a resource with the same key in a different scope, such as a Grid within that Window. Anything using {DynamicResource MyIcon} in the Grid will show the 64x64 frame, while everything else in the window shows the 32x32 frame.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文