在C#中获取文件和文件夹的透明shell图标

时间:2012-11-13 21:35:15

标签: c# windows com pinvoke explorer

我目前正在开发一个小型库,可以让您从文件和文件夹中获取图标。现在,我不在乎它是否仅适用于win8 +(因为那是我将要使用它的地方),但是,我遇到了一个关于透明度的小问题。如果您看一下下图:

Differences

我生成的(从我的库中)是在左侧,Windows资源管理器在右侧 现在,正如您可能看到的那样,首先在我生成的一行的右上方有2条黑线,其次,背景颜色有差异。所以我想知道的是这个;有没有办法得到Windows资源管理器使用的完全相同的图像,或者我只是做错了?

我的代码(除了简短的结构/外部等)下面,整个代码here

public static class Icon
{
    public static Image GetIcon(string fileName, int size)
    {
        IShellItem shellItem;
        Shell32.SHCreateItemFromParsingName(fileName, IntPtr.Zero, Shell32.IShellItem_GUID, out shellItem);

        IntPtr hbitmap;
        ((IShellItemImageFactory)shellItem).GetImage(new SIZE(size, size), 0x0, out hbitmap);

        // get the info about the HBITMAP inside the IPictureDisp
        DIBSECTION dibsection = new DIBSECTION();
        Gdi32.GetObjectDIBSection(hbitmap, Marshal.SizeOf(dibsection), ref dibsection);
        int width = dibsection.dsBm.bmWidth;
        int height = dibsection.dsBm.bmHeight;

        // zero out the RGB values for all pixels with A == 0 
        // (AlphaBlend expects them to all be zero)
        for (int i = 0; i < dibsection.dsBmih.biWidth * dibsection.dsBmih.biHeight; i++)
        {
            IntPtr ptr = dibsection.dsBm.bmBits + (i * Marshal.SizeOf(typeof(RGBQUAD)));
            var rgbquad = (RGBQUAD)Marshal.PtrToStructure(ptr, typeof(RGBQUAD));
            if (rgbquad.rgbReserved == 0)
            {
                rgbquad.rgbBlue = 0;
                rgbquad.rgbGreen = 0;
                rgbquad.rgbRed = 0;
            }
            else
            {
                ;
            }
            Marshal.StructureToPtr(rgbquad, ptr, false);
        }

        // create the destination Bitmap object
        Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

        // get the HDCs and select the HBITMAP
        Graphics graphics = Graphics.FromImage(bitmap);

        IntPtr hdcDest = graphics.GetHdc();
        IntPtr hdcSrc = Gdi32.CreateCompatibleDC(hdcDest);
        IntPtr hobjOriginal = Gdi32.SelectObject(hdcSrc, hbitmap);

        // render the bitmap using AlphaBlend
        BLENDFUNCTION blendfunction = new BLENDFUNCTION(BLENDFUNCTION.AC_SRC_OVER, 0, 0xFF, BLENDFUNCTION.AC_SRC_ALPHA);
        Gdi32.AlphaBlend(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, width, height, blendfunction);

        // clean up
        Gdi32.SelectObject(hdcSrc, hobjOriginal);
        Gdi32.DeleteDC(hdcSrc);
        graphics.ReleaseHdc(hdcDest);
        graphics.Dispose();
        Gdi32.DeleteObject(hbitmap);

        return bitmap;
    }
}

1 个答案:

答案 0 :(得分:3)

似乎逐像素复制是解决方案。以下似乎与探测器完全相同。

    public static Image GetIcon(string fileName, int size)
    {
        IShellItem shellItem;
        Shell32.SHCreateItemFromParsingName(fileName, IntPtr.Zero, Shell32.IShellItem_GUID, out shellItem);

        IntPtr hbitmap;
        ((IShellItemImageFactory)shellItem).GetImage(new SIZE(size, size), 0x0, out hbitmap);

        // get the info about the HBITMAP inside the IPictureDisp
        DIBSECTION dibsection = new DIBSECTION();
        Gdi32.GetObjectDIBSection(hbitmap, Marshal.SizeOf(dibsection), ref dibsection);
        int width = dibsection.dsBm.bmWidth;
        int height = dibsection.dsBm.bmHeight;

        // create the destination Bitmap object
        Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

        for (int x = 0; x < dibsection.dsBmih.biWidth; x++)
        {
            for (int y = 0; y < dibsection.dsBmih.biHeight; y++)
            {
                int i = y * dibsection.dsBmih.biWidth + x;
                IntPtr ptr = dibsection.dsBm.bmBits + (i * Marshal.SizeOf(typeof(RGBQUAD)));
                var rgbquad = (RGBQUAD)Marshal.PtrToStructure(ptr, typeof(RGBQUAD));

                if (rgbquad.rgbReserved != 0)
                    bitmap.SetPixel(x, y, Color.FromArgb(rgbquad.rgbReserved, rgbquad.rgbRed, rgbquad.rgbGreen, rgbquad.rgbBlue));
            }
        }

        Gdi32.DeleteObject(hbitmap);

        return bitmap;
    }
相关问题