winapi - 如何从 PNG 创建图像列表?

标签 winapi mfc

我见过here您可以创建具有透明度的图像列表。它有效......有点。

我用它来为列表控件创建图像列表。结果有点令人失望:

view of actual list image view of list image

左边的就是它应该的样子。右边是列表控件的显示方式。看起来它只是尝试使用 Alpha 作为 mask ,并且尝试通过抖动来近似任何混合区域。有没有办法让它变得更好,以便我获得实际的 alpha 混合图像?

如果这有什么区别的话,这是来源:

class CDlg : public CDialog
{
    DECLARE_DYNCREATE(CDlg)

public:
    CDlg(CWnd* pParent = NULL);   // standard constructor
    virtual ~CDlg();

    // Dialog Data
    enum { IDD = IDD_BS_PRINT };
    CGdiPlusBitmapResource m_pBitmap;

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    virtual BOOL OnInitDialog();

    DECLARE_MESSAGE_MAP()
public:
    CListCtrl m_printOptions;
};

BOOL CDlg::OnInitDialog()
{
    __super::OnInitDialog();

    m_pBitmap.Load(IDB_RIBBON_HOMELARGE, _T("PNG"), AfxGetResourceHandle());
    HBITMAP hBitmap;
    m_pBitmap.m_pBitmap->GetHBITMAP(RGB(0, 0, 0), &hBitmap);

    CImageList *pList = new CImageList;
    CBitmap bm;
    bm.Attach(hBitmap);
    pList->Create(32, 32, ILC_COLOR32, 0, 4);
    pList->Add(&bm, RGB(255, 0, 255));
    m_printOptions.SetImageList(pList, LVSIL_NORMAL);

//...
    return TRUE;
}

IMPLEMENT_DYNCREATE(CDlg, CDialog)

CBSPrintDlg::CBSPrintDlg(CWnd* pParent /*=NULL*/)
: CBCGPDialog(CBSPrintDlg::IDD, pParent)
{
}

CBSPrintDlg::~CBSPrintDlg()
{
}

void CBSPrintDlg::DoDataExchange(CDataExchange* pDX)
{
    CBCGPDialog::DoDataExchange(pDX);

    DDX_Control(pDX, IDC_PRINT_OPTIONS, m_printOptions);
}

有关 CGdiPlusBitmapResource 实现的源代码,请查看 here .

带有透明度的原始图像是这样的:enter image description here

@Barmak 尝试了不同的图像,看起来不错。我认为这是因为透明度靠近边缘而不位于图像内。请参阅此处:

enter image description here

最佳答案

编辑----------

Gdiplus::GetHBITMAP 中的第一个参数应该是背景颜色。使用 RGB(0, 0, 0) 作为背景颜色会使半透明像素与黑色匹配。

使用Gdiplus::Color(255,255,255,255)(白色)可以改善外观(因为ListView背景也是白色的)。但最好将背景更改为 Gdiplus::Color(0,255,255,255)(透明)以匹配任何背景。

{
    CGdiPlusBitmapResource gdibmp;
    if (gdibmp.Load(IDB_RIBBON_HOMELARGE, _T("PNG"), AfxGetResourceHandle()))
    {
        HBITMAP hBitmap;
        gdibmp.m_pBitmap->GetHBITMAP(Gdiplus::Color::Transparent, &hBitmap);
        ImageList_AddMasked(*pList, hBitmap, 0);
    }
}

假设图像均为 32x32 像素。如果图像大小不同,则必须在添加到图像列表之前调整它们的大小。

{
    CGdiPlusBitmapResource gdibmp;
    if (gdibmp.Load(id, _T("PNG"), AfxGetResourceHandle()))
    {
        //resize image to 32x32 pixels
        Gdiplus::Bitmap newBmp(32, 32, PixelFormat32bppPARGB);
        double oldh = (double)gdibmp.m_pBitmap->GetHeight();
        double oldw = (double)gdibmp.m_pBitmap->GetWidth();
        double neww = 32;
        double newh = 32;

        double ratio = oldw / oldh;
        if (oldw > oldh)
            newh = neww / ratio;
        else
            neww = newh * ratio;

        Gdiplus::Graphics graphics(&newBmp);
        graphics.SetInterpolationMode(Gdiplus::InterpolationMode::InterpolationModeHighQualityBicubic);
        graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
        graphics.DrawImage(gdibmp.m_pBitmap, 0, 0, (int)neww, (int)newh);

        //add `newBmp` to image list
        HBITMAP hBitmap;
        newBmp.GetHBITMAP(Gdiplus::Color::Transparent, &hBitmap);
        ImageList_AddMasked(m_ImageList, hBitmap, 0);
    }
}

<小时/> 使用 GdiPlus::GetHICON 获取图标句柄...使用 CGdiPlusBitmapResource 类,应该可以使用以下内容:

HICON hicon;
m_pBitmap.Load(IDB_RIBBON_HOMELARGE, _T("PNG"), AfxGetResourceHandle());
m_pBitmap.m_pBitmap->GetHICON(&hicon);
pList->Add(hicon);

或使用GetHBITMAP

还要确保Visual Styles启用以改进 ListView 图标的外观。

透明背景测试图像:

enter image description here

关于winapi - 如何从 PNG 创建图像列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26636522/

相关文章:

c++ - CFileImageLoader(LPCTSTR lpszFileName);

c++ - 如何替换/更新 MFC 对话框中的 ActiveX 控件

c++ - Cstring 到 BYTE 的转换

c++ - 遍历数组和线程时访问冲突

使用 WinHTTP API 固定证书

multithreading - 为什么即使在抢占式多任务操作系统 (Windows 7) 上线程也会饥饿

c++ - CreateProcess STATUS_DLL_NOT_FOUND - 哪个 dll?

powershell - 是否可以获取不在 shell 命名空间中的项目的 shell 属性?

c++ - 什么时候调用 CMFCListCtrl::OnGetCellBkColor 函数?

c++ - 从 MFC 和 VS2010 中的编辑控件读取文本