【问题标题】:How do I copy an HICON from GDI to GDI+ with transparency?如何透明地将 HICON 从 GDI 复制到 GDI+?
【发布时间】:2012-07-05 11:37:34
【问题描述】:

无论我做什么,我都会在图标周围出现白色/黑色边框......这是什么原因?!

是否可能正确地做到这一点?如何将 HICON 复制到具有透明度的 GDI+ 位图?

【问题讨论】:

    标签: winapi visual-c++ transparency gdi+ gdi


    【解决方案1】:

    我只是浪费了几个小时。

    加上我之前浪费了多少次,是的,这很令人沮丧。

    原来这是 GDI+ 的问题。解决方法是here;下面是一些可能有帮助的代码:

    HICON hIcon = ...;
    
    ICONINFO ii; GetIconInfo(hIcon, &ii);
    BITMAP bmp; GetObject(ii.hbmColor, sizeof(bmp), &bmp);
    
    Gdiplus::Bitmap temp(ii.hbmColor, NULL);
    Gdiplus::BitmapData lockedBitmapData;
    Gdiplus::Rect rc(0, 0, temp.GetWidth(), temp.GetHeight());
    
    temp.LockBits(&rc, Gdiplus::ImageLockModeRead, temp.GetPixelFormat(), &lockedBitmapData);
    
    Gdiplus::Bitmap image(
        lockedBitmapData.Width, lockedBitmapData.Height, lockedBitmapData.Stride,
        PixelFormat32bppARGB, reinterpret_cast<BYTE *>(lockedBitmapData.Scan0));
    
    temp.UnlockBits(&lockedBitmapData);
    
    // Now 'image' has the icon, with transparency
    

    【讨论】:

    • 非常感谢。我遇到了同样的黑线问题。我尝试了您的解决方案,但遇到了另一个问题:缺少一些线条和像素。我尝试使用 BITMAP 并根据它们调整此代码,现在图像已完美呈现!
    • @hackjutsu:我认为应该是lockedBitmapData
    猜你喜欢
    • 2011-12-03
    • 2015-05-04
    • 2014-03-12
    • 2012-03-15
    • 2019-03-16
    • 2010-11-23
    • 2017-07-02
    • 1970-01-01
    • 2011-06-04
    相关资源
    最近更新 更多