0

我需要从特定文件扩展名的 Windows Shell 中提取图标。提取后,我需要将其保存为具有透明度的 PNG 文件。

但我不知道发生了什么,看起来透明度已损坏。
请看附图(灰色是真正的透明度)。

Windows shell 可以正确显示此图标,但也可以看到一些伪影(如阴影或发光)。请看下面的截图:

截屏

HRESULT  hr = ::SHGetFileInfoA(pszPath, dwAttributes, &shfi, sizeof(shfi), SHGFI_SYSICONINDEX | SHGFI_LARGEICON  );
if (!SUCCEEDED(hr))
    return 0;

ATL::CComPtr<IImageList> pList;
HRESULT h2 = ::SHGetImageList(uState,   IID_IImageList,  (void**)&pList);
if (!SUCCEEDED(h2))
    return 0;

int count = 0;
pList->GetImageCount(&count);

if (count <= shfi.iIcon)
    return 0;

HICON ico = ImageList_GetIcon(IImageListToHIMAGELIST(pList), shfi.iIcon, ILD_TRANSPARENT);
Gdiplus::Bitmap bmp(ico);

Graphics *pGraphics  = Graphics::FromImage(&bmp);

IMAGELISTDRAWPARAMS pimldp = {0};

pimldp.hdcDst = pGraphics->GetHDC();

pimldp.cbSize = sizeof IMAGELISTDRAWPARAMS;
pimldp.himl = IImageListToHIMAGELIST(pList);
pimldp.i = shfi.iIcon;
pimldp.x = 0;
pimldp.y = 0;
pimldp.cx = cx;
pimldp.cy = cy;
pimldp.yBitmap = 0;
pimldp.xBitmap = 0;
pimldp.fStyle = ILD_IMAGE | ILD_SCALE /*| ILD_PRESERVEALPHA*/  ;

ImageList_DrawIndirect(&pimldp);

CLSID clsid;    
GetEncoderClsid(L"image/png", &clsid);

bmp.Save(L"d:\\test.png", &clsid);
::ShellExecuteA(NULL, "open", "d:\\test.png", "", "d:\\", SW_NORMAL);
pGraphics->ReleaseHDC(pimldp.hdcDst);
::DestroyIcon(ico);
4

1 回答 1

2
HICON ico = NULL;
pList->GetIcon(shfi.iIcon, ILD_TRANSPARENT, &ico);

ICONINFO ii = {0};
GetIconInfo(ico, &ii);

Gdiplus::Bitmap bmpIcon(ii.hbmColor, NULL);
Gdiplus::Rect rectBounds(0, 0, bmpIcon.GetWidth(), bmpIcon.GetHeight() );

BitmapData bmData;
bmpIcon.LockBits(&rectBounds, ImageLockModeRead, bmpIcon.GetPixelFormat(), &bmData);

Bitmap bmp(bmData.Width, bmData.Height, bmData.Stride, PixelFormat32bppARGB, (BYTE*)bmData.Scan0);

bmpIcon.UnlockBits(&bmData);

bmp现在包含来自HICON.

于 2012-07-07T22:22:54.233 回答