How do i get the icon for a file in C#?
i tried using Icon.ExtractAssociatedIcon()
but it returns a weird version of the image with a low bitdepth (aka the wrong colors but in the right shape)
Asked
Active
Viewed 375 times
0

Chris
- 47
- 1
- 1
- 2
-
refer to this [ask] – Prabhjot Singh Kainth Dec 01 '19 at 09:38
-
[Get File Icon used by Shell](https://stackoverflow.com/questions/462270/get-file-icon-used-by-shell) – Dec 01 '19 at 09:45
-
[ShGetFileInfo](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shgetfileinfow) (should you decide to take that path) will return the System default sizes of the Icons, Small Icon and Large Icon, but not the Extra Large Icon and the Jumbo size Icon. You'll have to get the icon through [SHGetImageList](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shgetimagelist) with either the `SHIL_JUMBO` or the `SHIL_EXTRALARGE` flags set. – Jimi Dec 01 '19 at 11:38
-
Then, use the `GetIcon()` method of the [IImageList](https://learn.microsoft.com/en-us/windows/win32/api/commoncontrols/nn-commoncontrols-iimagelist) intrerface (it needs to be implemented using its Guid: `46EB5926-582E-4017-9FDF-E8998DAA0950`). It returns an Icon handle that can be used with [Icon.FromHandle()](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.icon.fromhandle). – Jimi Dec 01 '19 at 11:44
-
The `Icon.ExtractAssociatedIcon()` method uses the base Shell [ExtractAssociatedIcon](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-extractassociatediconw) method with the `pIcon` argument set to `0` (the index of the first icon available). – Jimi Dec 01 '19 at 11:52