如何在CDC上绘制背景透明的图片
2014-06-09 16:59
471 查看
//i give some code,Hope it helps
void CBarcodeBox::DrawTransparentBitmap(CDC* pDC,CBitmap* pBitmap,
short xStart,short yStart,COLORREF cTransparentColor)
{
BITMAP bm;
COLORREF cColor;
CBitmap bmAndBack, bmAndObject, bmAndMem, bmSave;
CBitmap *pbmBackOld, *pbmObjectOld, *pbmMemOld, *pbmSaveOld;
CDC dcMem, dcBack, dcObject, dcTemp, dcSave;
CPoint ptSize;
dcTemp.CreateCompatibleDC(pDC);
dcTemp.SelectObject(pBitmap); // Select the bitmap
pBitmap->GetObject(sizeof(BITMAP),(LPSTR)&bm);
ptSize.x = bm.bmWidth; // Get width of bitmap
ptSize.y = bm.bmHeight; // Get height of bitmap
dcTemp.DPtoLP(&ptSize,1); // Convert from device to logical points
// Create some DCs to hold temporary data.
dcBack.CreateCompatibleDC(pDC);
dcObject.CreateCompatibleDC(pDC);
dcMem.CreateCompatibleDC(pDC);
dcSave.CreateCompatibleDC(pDC);
// Create a bitmap for each DC. DCs are required for a number of
// GDI functions.
// Monochrome DC
bmAndBack.CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);
// Monochrome DC
bmAndObject.CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);
bmAndMem.CreateCompatibleBitmap(pDC,ptSize.x,ptSize.y);
bmSave.CreateCompatibleBitmap(pDC,ptSize.x,ptSize.y);
// Each DC must select a bitmap object to store pixel data.
pbmBackOld = dcBack.SelectObject(&bmAndBack);
pbmObjectOld = dcObject.SelectObject(&bmAndObject);
pbmMemOld = dcMem.SelectObject(&bmAndMem);
pbmSaveOld = dcSave.SelectObject(&bmSave);
// Set proper mapping mode.
dcTemp.SetMapMode(pDC->GetMapMode());
// Save the bitmap sent here, because it will be overwritten.
dcSave.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCCOPY);
// Set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = dcTemp.SetBkColor(cTransparentColor);
// Create the object mask for the bitmap by performing a BitBlt()
// from the source bitmap to a monochrome bitmap.
dcObject.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCCOPY);
// Set the background color of the source DC back to the original color.
dcTemp.SetBkColor(cColor);
// Create the inverse of the object mask.
dcBack.BitBlt(0,0,ptSize.x,ptSize.y,&dcObject,0,0,NOTSRCCOPY);
// Copy the background of the main DC to the destination.
dcMem.BitBlt(0,0,ptSize.x,ptSize.y,pDC,xStart,yStart,SRCCOPY);
// Mask out the places where the bitmap will be placed.
dcMem.BitBlt(0,0,ptSize.x,ptSize.y,&dcObject,0,0,SRCAND);
// Mask out the transparent colored pixels on the bitmap.
dcTemp.BitBlt(0,0,ptSize.x,ptSize.y,&dcBack,0,0,SRCAND);
// XOR the bitmap with the background on the destination DC.
dcMem.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCPAINT);
// Copy the destination to the screen.
pDC->BitBlt(xStart,yStart,ptSize.x,ptSize.y,&dcMem,0,0,SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
dcTemp.BitBlt(0,0,ptSize.x,ptSize.y,&dcSave,0,0,SRCCOPY);
// Delete the memory bitmaps.
(dcBack.SelectObject(pbmBackOld))->DeleteObject();
(dcObject.SelectObject(pbmObjectOld))->DeleteObject();
(dcMem.SelectObject(pbmMemOld))->DeleteObject();
(dcSave.SelectObject(pbmSaveOld))->DeleteObject();
// Delete the memory DCs.
dcMem.DeleteDC();
dcBack.DeleteDC();
dcObject.DeleteDC();
dcSave.DeleteDC();
dcTemp.DeleteDC();
}
void CBarcodeBox::DrawTransparentBitmap(CDC* pDC,CBitmap* pBitmap,
short xStart,short yStart,COLORREF cTransparentColor)
{
BITMAP bm;
COLORREF cColor;
CBitmap bmAndBack, bmAndObject, bmAndMem, bmSave;
CBitmap *pbmBackOld, *pbmObjectOld, *pbmMemOld, *pbmSaveOld;
CDC dcMem, dcBack, dcObject, dcTemp, dcSave;
CPoint ptSize;
dcTemp.CreateCompatibleDC(pDC);
dcTemp.SelectObject(pBitmap); // Select the bitmap
pBitmap->GetObject(sizeof(BITMAP),(LPSTR)&bm);
ptSize.x = bm.bmWidth; // Get width of bitmap
ptSize.y = bm.bmHeight; // Get height of bitmap
dcTemp.DPtoLP(&ptSize,1); // Convert from device to logical points
// Create some DCs to hold temporary data.
dcBack.CreateCompatibleDC(pDC);
dcObject.CreateCompatibleDC(pDC);
dcMem.CreateCompatibleDC(pDC);
dcSave.CreateCompatibleDC(pDC);
// Create a bitmap for each DC. DCs are required for a number of
// GDI functions.
// Monochrome DC
bmAndBack.CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);
// Monochrome DC
bmAndObject.CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);
bmAndMem.CreateCompatibleBitmap(pDC,ptSize.x,ptSize.y);
bmSave.CreateCompatibleBitmap(pDC,ptSize.x,ptSize.y);
// Each DC must select a bitmap object to store pixel data.
pbmBackOld = dcBack.SelectObject(&bmAndBack);
pbmObjectOld = dcObject.SelectObject(&bmAndObject);
pbmMemOld = dcMem.SelectObject(&bmAndMem);
pbmSaveOld = dcSave.SelectObject(&bmSave);
// Set proper mapping mode.
dcTemp.SetMapMode(pDC->GetMapMode());
// Save the bitmap sent here, because it will be overwritten.
dcSave.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCCOPY);
// Set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = dcTemp.SetBkColor(cTransparentColor);
// Create the object mask for the bitmap by performing a BitBlt()
// from the source bitmap to a monochrome bitmap.
dcObject.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCCOPY);
// Set the background color of the source DC back to the original color.
dcTemp.SetBkColor(cColor);
// Create the inverse of the object mask.
dcBack.BitBlt(0,0,ptSize.x,ptSize.y,&dcObject,0,0,NOTSRCCOPY);
// Copy the background of the main DC to the destination.
dcMem.BitBlt(0,0,ptSize.x,ptSize.y,pDC,xStart,yStart,SRCCOPY);
// Mask out the places where the bitmap will be placed.
dcMem.BitBlt(0,0,ptSize.x,ptSize.y,&dcObject,0,0,SRCAND);
// Mask out the transparent colored pixels on the bitmap.
dcTemp.BitBlt(0,0,ptSize.x,ptSize.y,&dcBack,0,0,SRCAND);
// XOR the bitmap with the background on the destination DC.
dcMem.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCPAINT);
// Copy the destination to the screen.
pDC->BitBlt(xStart,yStart,ptSize.x,ptSize.y,&dcMem,0,0,SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
dcTemp.BitBlt(0,0,ptSize.x,ptSize.y,&dcSave,0,0,SRCCOPY);
// Delete the memory bitmaps.
(dcBack.SelectObject(pbmBackOld))->DeleteObject();
(dcObject.SelectObject(pbmObjectOld))->DeleteObject();
(dcMem.SelectObject(pbmMemOld))->DeleteObject();
(dcSave.SelectObject(pbmSaveOld))->DeleteObject();
// Delete the memory DCs.
dcMem.DeleteDC();
dcBack.DeleteDC();
dcObject.DeleteDC();
dcSave.DeleteDC();
dcTemp.DeleteDC();
}
相关文章推荐
- Android开发_如何设置按钮背景透明与半透明_图片背景透明
- CDC TextOut如何使字的背景透明
- Photoshop制作Android UI: 如何将图片背景变为透明
- 用photoshop如何制作背景透明的图片
- Android开发_如何设置按钮背景透明与半透明_图片背景透明
- 如何在页面中使用透明图片和透明背景图片(附demo下载)(转载阿一)
- Android开发_如何设置按钮背景透明与半透明_图片背景透明
- 如何在Matlab中得到透明背景的图片?
- Photoshop制作Android UI: 如何将图片背景变为透明
- 如何用photoshop把图片白色背景变成透明?
- 如何把图片中白色的背景修改为透明的
- 如何使png图片在ie6里实现透明背景
- 网页设计之如何实现图片透明背景
- 如何制作透明背景的图片
- PhotoShop中如何把一张白色背景的图片设为透明效果
- PS做图片,如何使背景透明
- 如何在页面中使用透明图片和透明背景图片(附demo下载)
- 如何拉伸有透明区域的png图片来做UIButton的背景图片
- ps修改gif图片背景透明及如何制作gif图片(图文记录)