繁体   English   中英

IImageList-> Draw - 透明度损坏

[英]IImageList->Draw - Corrupted transparency

我需要从特定的文件扩展名中提取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);
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的有效透明/ alphablended位图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM