使用 Icon.ExtractAssociatedIcon 和 ImageList 获取完整质量的 16 x 16 图标

发布于 2024-07-12 22:34:09 字数 471 浏览 7 评论 0原文

按照这个问题的指示,我运行了一些代码来从中提取图标文件并在设置为详细信息模式的 ListView 中显示它们。 我希望图标以 16 x 16 的尺寸显示,但是当我将 ImageList 大小设置为该尺寸时,出现的图标看起来非常奇怪(不知道如何描述它 - 请参阅随附的屏幕截图)。

我尝试将尺寸更改为 32 x 32,效果很好,但肯定有办法获得高质量的 16 x 16 图标,不是吗?

http://img165.imageshack.us/img165/4446/badqualityiconscc4.png

Following the directions at this question, I have some code running to extract icons from files and display them in a ListView set to details mode. I want to icons to display at 16 x 16, but when I have the ImageList size set to that the icons that come out look very weird (not sure how to describe it - see attached screenshot).

I've tried changing the size to 32 x 32 and they come out fine, but surely there must be a way to get good quality 16 x 16 icons mustn't there?

http://img165.imageshack.us/img165/4446/badqualityiconscc4.png

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

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

发布评论

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

评论(3

谁把谁当真 2024-07-19 22:34:10

你必须使用 2 个图像列表,一个用于小图像,一个用于大图像才能获得我认为的最佳结果。 (列表视图有两个属性,LargeImageList和SmallImageList)

编辑(发现我尝试时有效的新信息):

这个版本使用插值来获得较小的拇指,应该更好。

    Dim BigIcon As Icon = Nothing
    BigIcon = Icon.ExtractAssociatedIcon("c:\zebra.zip")
    Dim largeimages As New ImageList
    Dim smallimages As New ImageList

    largeimages.Images.Add("1", BigIcon)

    'Fix a smaller version with interpolation
    Dim bm As New Bitmap(BigIcon.ToBitmap)
    Dim thumb As New Bitmap(16, 16)
    Dim g As Graphics = Graphics.FromImage(thumb)
    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
    g.DrawImage(bm, New Rectangle(0, 0, 16, 16), New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
    g.Dispose()
    bm.Dispose()
    smallimages.Images.Add("1", thumb)
    ListView1.SmallImageList = smallimages
    ListView1.LargeImageList = largeimages
    thumb.Dispose()
    ListView1.Items.Add("Test", "Test", "1")

You have to use 2 imagelists, one for smallimages and one for largeimages to get the best result I think. (The listview has two properties, LargeImageList and SmallImageList)

Edit (found new information that worked when I tried):

This version are using interpolation to get the smaller thumb, should be better.

    Dim BigIcon As Icon = Nothing
    BigIcon = Icon.ExtractAssociatedIcon("c:\zebra.zip")
    Dim largeimages As New ImageList
    Dim smallimages As New ImageList

    largeimages.Images.Add("1", BigIcon)

    'Fix a smaller version with interpolation
    Dim bm As New Bitmap(BigIcon.ToBitmap)
    Dim thumb As New Bitmap(16, 16)
    Dim g As Graphics = Graphics.FromImage(thumb)
    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
    g.DrawImage(bm, New Rectangle(0, 0, 16, 16), New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
    g.Dispose()
    bm.Dispose()
    smallimages.Images.Add("1", thumb)
    ListView1.SmallImageList = smallimages
    ListView1.LargeImageList = largeimages
    thumb.Dispose()
    ListView1.Items.Add("Test", "Test", "1")
望笑 2024-07-19 22:34:10

通过此代码项目文章PInvoke.net 上的 ExtractIconEx 演示 您可以编写以下内容:

FileAssociationInfo info = new FileAssociationInfo(".docx");

ProgramAssociationInfo pai = new ProgramAssociationInfo(info.ProgID);
ProgramIcon ico = pai.DefaultIcon;
Icon icoLarge = Martin.Hyldahl.Examples.ExtractIconEx.ExtractIconExample.ExtractIconFromExe(ico.Path, ico.Index, false);

您必须将 ExtractIconFromExe 的签名更改为

public static Icon ExtractIconFromExe(string file, int nIconIndex, bool large)

和将代码更改为几行

if (large)
   readIconCount = ExtractIconEx(file, nIconIndex, hIconEx, hDummy, 1);
else
   readIconCount = ExtractIconEx(file, nIconIndex, hDummy, hIconEx, 1);

With this Code Project Article and the Demo of ExtractIconEx on PInvoke.net you can write the following:

FileAssociationInfo info = new FileAssociationInfo(".docx");

ProgramAssociationInfo pai = new ProgramAssociationInfo(info.ProgID);
ProgramIcon ico = pai.DefaultIcon;
Icon icoLarge = Martin.Hyldahl.Examples.ExtractIconEx.ExtractIconExample.ExtractIconFromExe(ico.Path, ico.Index, false);

you have to change the signature of ExtractIconFromExe to

public static Icon ExtractIconFromExe(string file, int nIconIndex, bool large)

and change the code a few lines down to

if (large)
   readIconCount = ExtractIconEx(file, nIconIndex, hIconEx, hDummy, 1);
else
   readIconCount = ExtractIconEx(file, nIconIndex, hDummy, hIconEx, 1);
百善笑为先 2024-07-19 22:34:10

默认情况下,Imagelist ColorDepth 属性设置为 Depth8Bit,将其设置为 Depth32Bit

By defaut Imagelist ColorDepth property is set to Depth8Bit, set it to Depth32Bit.

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