我有以下 C# 代码从特定 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);
}
这工作正常。有点。因为它不处理透明度。
看一看:
我怎样才能解决这个问题?
编辑
归功于@rbmm
问题不在于上面的代码,而在于我用来从 转换Icon
为Bitmap
. 我正在使用Bitmap.FromHIcon
,它显然丢弃了透明度。我现在使用自定义方法在这两者之间进行转换,它可以完美地工作。