8

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

Community
  • 1
  • 1
robintw
  • 27,571
  • 51
  • 138
  • 205

3 Answers3

11

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")
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
Stefan
  • 11,423
  • 8
  • 50
  • 75
  • 1
    Sadly that doesn't seem to work - they're still displaying badly. Any other ideas? – robintw Jan 20 '09 at 22:04
  • Thanks. I've tried what you suggested in your edit, using the DrawImage method and that doesn't seem to work either - gives exactly the same results. I can post the code I'm using with the DrawImage method if that would help. – robintw Jan 20 '09 at 22:14
  • Edited my answer. I think this will do it. If not Im out of ideas. – Stefan Jan 20 '09 at 22:27
  • 2
    Anything that draws the attention from the actual work I have to do is allways nice! ;) – Stefan Jan 20 '09 at 22:44
4

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);
We Are All Monica
  • 13,000
  • 8
  • 46
  • 72
relascope
  • 4,399
  • 4
  • 22
  • 27
1

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

Hugues Gauthier
  • 639
  • 8
  • 18