2009-01-20 81 views
8

按照this question的指示,我有一些代码正在运行以从文件中提取图标并将它们显示在设置为详细模式的ListView中。我希望图标以16 x 16显示,但是当我将ImageList的大小设置为出来的图标看起来很奇怪(不确定如何描述它 - 请参阅附加的屏幕截图)。使用Icon.ExtractAssociatedIcon和ImageList获取完整质量的16 x 16图标

我试过改变大小为32×32,他们出来很好,但一定有一种方法来获得良好的质量16×16图标不得在那里?

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

回答

10

你必须使用2个图像列表,一个用于smallimages,一个用于largeimages得到最好的结果,我想。 (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") 
+1

可悲的是,似乎并没有工作 - 他们仍然显示不好。任何其他想法? – robintw 2009-01-20 22:04:00

+0

即时通讯工作..;) – Stefan 2009-01-20 22:10:02

3

有了这个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); 
1

By defaut Imagelist ColorDepth属性设置为Depth8Bit,将其设置为Depth32Bit