//创建24位bmp区域
HBITMAP CreateBitmapSection(HDC hdc,int cx,int cy,BYTE** ppbit)
{
BITMAPINFO bmi ;
memset(&bmi.bmiHeader, 0, sizeof(BITMAPINFOHEADER));
bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER) ;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biWidth = cx;
bmi.bmiHeader.biHeight = cy;
return CreateDIBSection(hdc,&bmi,DIB_RGB_COLORS,(void**)ppbit,NULL,0);
}
//叠加两张bmp图片并输出到制定DC,Alpha取值范围 0~255(0为全透明,255为不透明)
void TransParentPicture(CDC *pDC, HBITMAP hOneBitmap, HBITMAP hTwoBitmap, CRect ClientRect,int Alpha)
{
BITMAP OneBitmap;
BITMAP TwoBitmap;
GetObject(hOneBitmap, sizeof(BITMAP), &OneBitmap);
GetObject(hTwoBitmap, sizeof(BITMAP), &TwoBitmap);
int nOneImageWidth = OneBitmap.bmWidth;
int nOneImageHeight = OneBitmap.bmHeight;
int nTwoImageWidth = TwoBitmap.bmWidth;
int nTwoImageHeight = TwoBitmap.bmHeight;
if (nOneImageWidth < nTwoImageWidth)
{
nOneImageWidth = nTwoImageWidth;
}
else
{
nTwoImageWidth = nOneImageWidth;
}
if (nOneImageHeight < nTwoImageHeight)
{
nOneImageHeight = nTwoImageHeight;
}
else
{
nTwoImageHeight = nOneImageHeight;
}
int nImageWidth = nOneImageWidth;
int nImageHeight = nOneImageHeight;
CDC MemDC1, MemDC2;
HBITMAP hOldBitmap1 = NULL, hOldBitmap2 = NULL;
BYTE *pOneBits = NULL;
MemDC1.CreateCompatibleDC(pDC);
MemDC2.CreateCompatibleDC(pDC);
HBITMAP hOneBmp = NULL;
hOneBmp = CreateBitmapSection(pDC->m_hDC, nImageWidth, nImageHeight, &pOneBits);
hOldBitmap1 = (HBITMAP)SelectObject(MemDC1.m_hDC, hOneBmp);
hOldBitmap2 = (HBITMAP)SelectObject(MemDC2.m_hDC, hOneBitmap);
//BitBlt(MemDC1.m_hDC, 0, 0, m_nTwoImage_w, m_nTwoImage_h, MemDC2.m_hDC, 0, 0, SRCCOPY);
StretchBlt(MemDC1.m_hDC, 0, 0, nImageWidth, nImageHeight,
MemDC2.m_hDC, 0, 0, OneBitmap.bmWidth, OneBitmap.bmHeight, SRCCOPY);
CDC MemDC3, MemDC4;
HBITMAP hOldBitmap3 = NULL, hOldBitmap4 = NULL;
BYTE *pTwoBits = NULL;
MemDC3.CreateCompatibleDC(pDC);
MemDC4.CreateCompatibleDC(pDC);
HBITMAP hTwoBmp = NULL;
hTwoBmp = CreateBitmapSection(pDC->m_hDC, nImageWidth, nImageHeight, &pTwoBits);
hOldBitmap3 = (HBITMAP)SelectObject(MemDC3.m_hDC, hTwoBmp);
hOldBitmap4 = (HBITMAP)SelectObject(MemDC4.m_hDC, hTwoBitmap);
//BitBlt(MemDC3.m_hDC, 0, 0, m_nTwoImage_w, m_nTwoImage_h, MemDC4.m_hDC, 0, 0, SRCCOPY);
StretchBlt(MemDC3.m_hDC, 0, 0, nImageWidth, nImageHeight,
MemDC4.m_hDC, 0, 0, TwoBitmap.bmWidth, TwoBitmap.bmHeight, SRCCOPY);
CDC MemDC5;
HBITMAP hOldBitmap5 = NULL;
BYTE* pNewBits=NULL;
MemDC5.CreateCompatibleDC(pDC);
HBITMAP hNewBitmap = NULL;
hNewBitmap = CreateBitmapSection(pDC->m_hDC, nImageWidth, nImageHeight, &pNewBits);
hOldBitmap5 = (HBITMAP)SelectObject(MemDC5.m_hDC, hNewBitmap);
int linebytes=0;
int col=0;
int line=(((nImageWidth*24)+31)>>5)<<2;
linebytes = line;
col = nImageHeight;
for(int j=0;j<col;j++)
{
for(int i=0;i<linebytes;i++)
{
*(pNewBits + j*line + i)=(*(pOneBits + j*line + i)*Alpha +
*(pTwoBits+j*line+i)*(255-Alpha))/255;
}
}
BitBlt(pDC->m_hDC, 0, 0, ClientRect.Width(), ClientRect.Height(), MemDC5.m_hDC, 0, 0, SRCCOPY);
//StretchBlt(pDC->m_hDC, 0, 0, m_ClientRect.Width(), m_ClientRect.Height(),
// MemDC5.m_hDC, 0, 0, m_nOneImage_w, m_nOneImage_h, SRCCOPY);
SelectObject(MemDC5.m_hDC,hOldBitmap5);
MemDC5.DeleteDC();
SelectObject(MemDC4.m_hDC,hOldBitmap4);
MemDC4.DeleteDC();
SelectObject(MemDC3.m_hDC,hOldBitmap3);
MemDC3.DeleteDC();
SelectObject(MemDC2.m_hDC,hOldBitmap2);
MemDC2.DeleteDC();
SelectObject(MemDC1.m_hDC,hOldBitmap1);
MemDC1.DeleteDC();
}