.Net ToolStrip 和 ImageList - 图像大小错误

发布于 2024-10-18 05:30:20 字数 913 浏览 6 评论 0原文

我有一个图标,其中包含尺寸为 16x16、24x24 和 32x32 的 32 位图像。我创建了三个包含三个不同尺寸图像的 ImageList 对象,并根据用户选择的尺寸在 ToolStrip 上分配 ImageList 属性。然而,ToolStrip 对象上显示的图像都是 32x32 图像的缩放版本。例如,我不明白为什么当我创建 16x16 ImageList 时,它没有从图标中提取 16x16 图像。我的代码基本上是这样的:

ImageList m_imageList16 = new ImageList();
m_imageList16.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
m_imageList16.ImageSize = new System.Drawing.Size(16, 16);
m_imageList16.TransparentColor = System.Drawing.Color.Transparent;

// Open is an icon in my resources that contains various sizes of images. 
m_imageList16 .Images.Add(global::MyTestApp.Properties.Resources.Open);

// Later when the user selects the 16 size from a menu, I change the toolbar:
m_toolbar.ImageScalingSize = new Size(16, 16);
m_toolbar.ImageList = m_imageList16;

看起来这只是将我的图标中的 32x32 大小的图像缩小到 16x16,而不是使用图标中定义的 16x16 图像。谁能帮忙解决这个问题吗?预先感谢您的任何意见!

  • 史蒂夫

I have an icon that contains 32-bit images of sizes 16x16, 24x24, and 32x32. I create three ImageList objects that contain the three different size images, and assign the ImageList property on my ToolStrip given what size the user selects. However the images displayed on the ToolStrip objects are all scaled versions of the 32x32 image. I can't figure out why when I create the 16x16 ImageList, for example, that it doesn't extract the 16x16 image from the icon. My code essentially looks like this:

ImageList m_imageList16 = new ImageList();
m_imageList16.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
m_imageList16.ImageSize = new System.Drawing.Size(16, 16);
m_imageList16.TransparentColor = System.Drawing.Color.Transparent;

// Open is an icon in my resources that contains various sizes of images. 
m_imageList16 .Images.Add(global::MyTestApp.Properties.Resources.Open);

// Later when the user selects the 16 size from a menu, I change the toolbar:
m_toolbar.ImageScalingSize = new Size(16, 16);
m_toolbar.ImageList = m_imageList16;

It seems this just takes the 32x32 size image in my icon and scales it down to 16x16, rather than using the 16x16 image defined in the icon. Can anyone help with this? Thanks in advance for any input!

  • Steve

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

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

发布评论

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

评论(2

抹茶夏天i‖ 2024-10-25 05:30:20

我找不到任何证据来支持我的理论,但 ImageList 似乎不知道选择不同大小的图标。我的直觉是它只是将图标转换为位图并存储该数据。

MSDN 论坛,我从 MVP 用户 nobugz 那里找到了这个片段:

我想我可以重现你的问题。当我使用 MicroAngelo(图标编辑器实用程序)从 shell32.dll 中提取图标 #5,将其保存到磁盘,然后将该图标添加到 16x16 ImageList 时,我看到该图标的插值版本很差。当我编辑图标并删除除 16x16x256 颜色之外的所有格式时,我得到了一个漂亮清晰的图标版本。

看起来ImageList 不够智能,无法选择与 ImageList 的 ImageSize 和 ColorDepth 属性最匹配的图标。它从来都不是一个特别智能的控制。这与您的场景不太相符,但结果是相同的。看到 32x32 的 PhysicalDimension 暗示您在提取图标时出现了问题。我在编辑后的图标上看到 16x16,但在未编辑的图标上看到 32x32。

另外,CSharpKey 的此页面似乎是使用 ImageList 和图标的有用资源。这是摘录:

如果您要创建图标列表,则可以使用默认大小 16x16 创建每个图标。在某些情况下(例如如果您打算将图像用于列表视图),您可以创建或设计第二组图标,其尺寸为 32x32(和/或 48x48) 每个。

看起来“言外之意”是,ImageList 仅存储一种尺寸并返回所存储图像的重新采样版本。

编辑

我在CodeProject上找到了另一个参考(Embedding Icons in your VB .NET 应用程序),内容如下:

嗯,这似乎是一个非常好的解决方案,直到我意识到ImageList 仅将图像存储为单一分辨率的位图。因此,如果我想在整个应用程序中使用所有这些不同尺寸的出色图标,也许使用它们不同的尺寸(因为图标在其单个文件中存储了多种尺寸),则 ImageList 不太适合它。< /p>

I can't find any evidence to support my theory, but ImageList appears to not be aware of selecting different size icons. My hunch is that it simply converts icons to bitmaps and stores that data.

At MSDN forums, I found this snippet from MVP user nobugz:

I think I can reproduce your problem. When I use MicroAngelo (an icon editor utility) to extract icon #5 from shell32.dll, save it to disk, then add the icon to a 16x16 ImageList, I see a poorly interpolated version of the icon. When I edit the icon and remove all formats except 16x16x256 colors, I get a nice sharp looking version of the icon.

Looks like ImageList isn't smart enough to select the icon that best matches the ImageList's ImageSize and ColorDepth property. It never was a particularly smart control. This doesn't quite match your scenario but the outcome is the same. Seeing a PhysicalDimension of 32x32 hints that something went wrong when you extracted the icon. I see 16x16 on the edited icon but 32x32 on the un-edited icon.

Also, this page from CSharpKey seemed a useful resource working with ImageLists and icons. Here's an excerpt:

If you are creating a list of icons, you can create each with the default size of 16x16. In some cases (for example if you intend to use the images for a list view), you can create or design a second set of icons whose size are 32x32 (and/or 48x48) each.

It seems to be, "reading between the lines" that the ImageList merely stores one size and returns resampled versions of the image stored.

Edit

I found another reference at CodeProject (Embedding Icons in your VB.NET application) that says this:

Well, that seemed like a pretty good solution, until I realized that an ImageList only stores images as bitmaps at a single resolution. So if I have all these great icons in various sizes that I want to use throughout my application, maybe using their different sizes (since icons have multiple sizes stored within their single file), the ImageList wasn't quite going to cut it.

漆黑的白昼 2024-10-25 05:30:20

我早在 2006 年就提交了这个 bug,微软 bug 人员回应说他们可以重现这个问题,但这不是一个关键问题,他们不会修复它。

我现在使用适当的 png,而不是多尺寸图标。

I filed this bug back in the year 2006, and the Microsoft bug guys responded that they can reproduce the problem, but it is not a critical one, and they are not going to fix it.

I am now using the appropriate pngs instead of multisize icons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文