Reputation: 1162
I have the following C# code to extract an icon with a specific index from a specific DLL:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
public class ExtractIcon
{
public static Icon Extract(string file, int number, bool largeIcon)
{
IntPtr large;
IntPtr small;
ExtractIconEx(file, number, out large, out small, 1);
try
{
return Icon.FromHandle(largeIcon ? large : small);
}
catch
{
return null;
}
}
[DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);
}
This works fine. Sort of. Because it doesn't handle transparency.
Take a look:
How can I fix this?
Credits to @rbmm
The problem was not the code above but rather the code I was using to convert from Icon
to Bitmap
. I was using Bitmap.FromHIcon
, which apparently discards the transparency. I now use a custom method to convert between these two and it works flawlessly.
Upvotes: 1
Views: 1479