如何叠加两个png? [英] How to overlay two png's?

查看:295
本文介绍了如何叠加两个png?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:-)



我有以下问题:



我有两个CBitMap对象,代表2个.png文件。



现在我想以下面的方式覆盖它们,其中第二个cbitmap在第一个Bitmap上:



如果第二个cbitmap的像素为白色,我想显示第一个cbitmap的像素。如果它不是白色,我想显示第二个cbitmap的像素。





这可能实现吗?





我尝试使用Alphablend()函数,我将第二个cbitmap的白色像素的alpha值设置为零。 br />


总是,只显示第二个png ...



有什么想法吗?



感谢您的帮助!





这是我的代码,供将来使用访客:



hi all:-)

I have the following question:

I have two CBitMap objects, which represent 2 .png-Files.

Now i want to overlay them the following way, where the 2nd cbitmap is over the first Bitmap:

if a pixel of the 2nd cbitmap is white, i want to display the pixel of the first cbitmap. if it is not white, i want to display the pixel of the 2nd cbitmap.


Is this possible to achieve?


I tried it with the Alphablend() function, where i set the alpha-value of the white pixels of the 2nd cbitmap to zero.

Always, only the second png is displayed...

Any ideas?

Thanks for any help!

[edit]
Here is my code, for future visitors:

void PngImage::DrawOverlayedBitmap(CBitmap* pBitmapUnder, CBitmap* pBitmapUpper, CWnd* pWnd, CPoint Offset)
{
	// get bitmap information
	BITMAP bmpInfo;
	pBitmapUnder->GetObject(sizeof(bmpInfo), &bmpInfo);

	// get size
	CSize size;
	size.cx = bmpInfo.bmWidth;
	size.cy = bmpInfo.bmHeight;

	// get window's client device context
	CClientDC* pDC = new CClientDC(pWnd);

	// create memory device context
	CDC* memDC = new CDC; memDC->CreateCompatibleDC(pDC);

	// buffer bitmap
	CBitmap* old = memDC->SelectObject(pBitmapUnder);

	// copy bitmap bits
	pDC->BitBlt
	(
		Offset.x,		// specifies the x-coordinate (in logical units) of the upper-left corner of the destination rectangle
		Offset.y,		// specifies the y-coordinate (in logical units) of the upper-left corner of the destination rectangle
		size.cx,		// specifies the width (in logical units) of the destination rectangle
		size.cy,		// specifies the height (in logical units) of the destination rectangle
		memDC,			// specifies the source device context
		0,				// specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle
		0,				// specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle
		SRCCOPY			// specifies the raster operation to be performed
	);

	// reselect first bitmap
	memDC->SelectObject(pBitmapUpper);

	// copy bitmap bits
	pDC->BitBlt
	(
		Offset.x,		// specifies the x-coordinate (in logical units) of the upper-left corner of the destination rectangle
		Offset.y,		// specifies the y-coordinate (in logical units) of the upper-left corner of the destination rectangle
		size.cx,		// specifies the width (in logical units) of the destination rectangle
		size.cy,		// specifies the height (in logical units) of the destination rectangle
		memDC,			// specifies the source device context
		0,				// specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle
		0,				// specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle
		SRCAND		// specifies the raster operation to be performed
	);

	// delete device context and reset pointer
	delete memDC; memDC = 0;

	// delete device context and reset pointer
	delete pDC; pDC = 0;
}





[/ edit]



[/edit]

推荐答案

你应该将第一个图像加载到DC中,然后将第二个图像加载到内存DC中,然后使用适当的操作代码将第二个图像加载到第一个图像中,如 https://msdn.microsoft.com/en-us/library/windows/desktop/dd183370%28v=vs.85 %29.aspx [ ^ ]。
You should load your first image into a DC then load the second into a memory DC, and then blit the second one into the first, using the appropriate operation code as described in https://msdn.microsoft.com/en-us/library/windows/desktop/dd183370%28v=vs.85%29.aspx[^].


这篇关于如何叠加两个png?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆