从DLL中提取带有透明度的图标

时间:2018-03-18 15:01:18

标签: c# windows winapi icons shell32.dll

我有以下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);
}

这很好用。有点。因为它不能处理透明度。

看看:

enter image description here

我该如何解决这个问题?

修改

@rbmm

的信用

问题不在于上面的代码,而是我用来将Icon转换为Bitmap的代码。我使用Bitmap.FromHIcon,显然丢弃了透明度。我现在使用custom method在这两者之间进行转换,它完美无缺。

0 个答案:

没有答案
相关问题