我在here看到您可以创建具有透明度的图像列表。它有效......有点。
我用它来为列表控件创建图像列表。结果有点令人失望:
左边的就是它应该的样子。右边是列表控件的显示方式。看起来它只是尝试使用 Alpha 作为遮罩,并且尝试通过抖动来近似任何混合区域。有没有办法让它变得更好,以便我获得实际的 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
实现的来源,请查看此处。
@Barmak 尝试了不同的图像,看起来不错。我认为这是因为透明度靠近边缘而不位于图像内。请看这里:
编辑----------
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);
}
}
HICON hicon;
m_pBitmap.Load(IDB_RIBBON_HOMELARGE, _T("PNG"), AfxGetResourceHandle());
m_pBitmap.m_pBitmap->GetHICON(&hicon);
pList->Add(hicon);
或使用
GetHBITMAP
还要确保启用视觉样式以改进 ListView 图标的外观。
透明背景测试图像:
PNG 图像包含部分透明的像素(alpha < 255). That's a pretty common accident with a program like Photoshop, the most likely cause is overlaying the spyglass image on top of the document image and not merging the layers correctly.
如上所述,图像只有在浅灰色或白色背景上显示时才会看起来不错。但那并没有发生,背景是黑色的。现在,望远镜周围的抗锯齿像素变得非常明显,它们根据其 Alpha 值变成各种深灰色,并且不再与文档图像的白色背景混合。当你使用 GDI 函数时一个非常典型的不幸,它不喜欢 alpha。
您可以使用 GDI+ 对其进行修改,确保背景颜色正确。但这是相当大量的工作,而且仍然给您带来正确猜测原始背景颜色的麻烦。
最好回到您使用的任何绘画工具并解决那里的问题。最快的修复方法应该是将其重新保存为 24bpp BMP 图像文件,ymmv。