使用以下代码从使用Mono的应用程序中获取图标时出现问题
背景颜色是黑色,这真的不是我的期望。
有什么建议吗?
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
private const uint SHGFI_ICON = 0x100;
private const uint SHGFI_LARGEICON = 0x0; // 'Large icon
private const uint SHGFI_SMALLICON = 0x1; // 'Small icon
[DllImport("shell32.dll")]
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
[DllImport("shell32.dll")]
private static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
[DllImport("User32.dll")]
public static extern int DestroyIcon(IntPtr hIcon);
public static Bitmap GetExeIcon(string filename, bool smallIcon)
{
SHFILEINFO shinfo = new SHFILEINFO();
uint index = 0;
uint largeIcom = SHGFI_LARGEICON;
if (smallIcon)
{
largeIcom = SHGFI_SMALLICON;
}
SHGetFileInfo(filename, (uint)index, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | largeIcom);
Icon icon = (Icon)System.Drawing.Icon.FromHandle(shinfo.hIcon).Clone();
DestroyIcon(shinfo.hIcon);
return icon.ToBitmap();
}
BTW,我可以使用Bitmap.FromHicon(shinfo.hIcon)
来获取位图对象,但显示的图像非常粗糙,我认为客户不会喜欢它。