【问题标题】:Get full quality 16 x 16 icon using Icon.ExtractAssociatedIcon and ImageList使用 Icon.ExtractAssociatedIcon 和 ImageList 获得完整质量的 16 x 16 图标
【发布时间】:2010-10-02 13:45:00
【问题描述】:

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

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

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

【问题讨论】:

    标签: .net image icons


    【解决方案1】:

    您必须使用 2 个图像列表,一个用于小图像,一个用于大图像,以获得我认为的最佳结果。 (listview有两个属性,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")
    

    【讨论】:

    • 遗憾的是,这似乎不起作用 - 他们仍然显示不佳。还有其他想法吗?
    • 谢谢。我已经尝试了您在编辑中建议的内容,使用 DrawImage 方法,但这似乎也不起作用 - 给出完全相同的结果。如果有帮助,我可以发布我正在使用的 DrawImage 方法的代码。
    • 编辑了我的答案。我认为这会做到。如果不是我没有想法。
    • 任何能从我必须做的实际工作中引起注意的事情都很好! ;)
    【解决方案2】:

    使用 Code Project ArticleDemo of ExtractIconEx on PInvoke.net 您可以编写以下内容:

    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);
    

    【讨论】:

    • 谢谢@HumerGu,我也能从中受益!
    【解决方案3】:

    Imagelist ColorDepth 属性默认设置为 Depth8Bit,设置为 Depth32Bit

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      相关资源
      最近更新 更多