1

使用以下代码从具有 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();

    }

顺便说一句,我可以Bitmap.FromHicon(shinfo.hIcon)用来获取位图对象,但是显示的图像太粗糙了,我认为客户不会喜欢它。

4

0 回答 0