现有一张单色的黑白单象素(相邻象素的颜色均不同)相间的位图一张,导入到资源当中.资源标识符为IDB_PATTERN.用gdi实现半透明了红色半透明的效果.我现在想用GDI+来实现目前的GDI功能,不知道如何去做,请高手指教!GDI实现的源码如下:
CDC *pMDC;
pMDC = GetDC();
CRect rect(10, 10, 100, 100);
CBitmap bmp;
bmp.LoadBitmap(IDB_PATTERN); CBrush brhBmpBrush, brhRedBrush;
CBrush *pOldBrush;
brhBmpBrush.CreatePatternBrush(&bmp);
brhRedBrush.CreateSolidBrush(RGB(0xFF, 0x0, 0x0));
pOldBrush = (CBrush *)pMDC->SelectObject(&brhRedBrush);
pMDC->SetBkColor(RGB(0, 0, 0));
pMDC->SetROP2(R2_XORPEN);
pMDC->Rectangle(&rect); pMDC->SelectObject(brhBmpBrush);
pMDC->SetBkColor(RGB(0xFF, 0xFF, 0xFF));
pMDC->SetROP2(R2_MASKPEN);
pMDC->Rectangle(&rect); pMDC->SelectObject(brhRedBrush);
pMDC->SetBkColor(RGB(0, 0, 0));
pMDC->SetROP2(R2_XORPEN);
pMDC->Rectangle(&rect); pMDC->SelectObject(pOldBrush);
CDC *pMDC;
pMDC = GetDC();
CRect rect(10, 10, 100, 100);
CBitmap bmp;
bmp.LoadBitmap(IDB_PATTERN); CBrush brhBmpBrush, brhRedBrush;
CBrush *pOldBrush;
brhBmpBrush.CreatePatternBrush(&bmp);
brhRedBrush.CreateSolidBrush(RGB(0xFF, 0x0, 0x0));
pOldBrush = (CBrush *)pMDC->SelectObject(&brhRedBrush);
pMDC->SetBkColor(RGB(0, 0, 0));
pMDC->SetROP2(R2_XORPEN);
pMDC->Rectangle(&rect); pMDC->SelectObject(brhBmpBrush);
pMDC->SetBkColor(RGB(0xFF, 0xFF, 0xFF));
pMDC->SetROP2(R2_MASKPEN);
pMDC->Rectangle(&rect); pMDC->SelectObject(brhRedBrush);
pMDC->SetBkColor(RGB(0, 0, 0));
pMDC->SetROP2(R2_XORPEN);
pMDC->Rectangle(&rect); pMDC->SelectObject(pOldBrush);
CDC* pSrcDC, int xSrc, int ySrc)
{
CDC dcCompatible;
CBitmap *pBitmapOld;
CBitmap bm; dcCompatible.CreateCompatibleDC(pDstDC);
bm.CreateCompatibleBitmap(pDstDC, nWidth, nHeight);
pBitmapOld = dcCompatible.SelectObject(&bm);
dcCompatible.FillSolidRect(CRect(0, 0, nWidth, nHeight), RGB(0x7F, 0x7F, 0x7F));
pDstDC->BitBlt(x, y, nWidth, nHeight, &dcCompatible, 0, 0, SRCAND);
dcCompatible.SelectObject(pBitmapOld); pDstDC->BitBlt(x, y, nWidth, nHeight, pSrcDC, 0, 0, SRCPAINT);
}这也是GDI的!
graphics.TranslateTransform(50, 50);
Bitmap background(L"background.jpg");
graphics.DrawImage(&background, 0, 0, 200, 200);
Bitmap forground(L"foreground.jpg");
graphics.DrawImage(&forground, 250, 0, 200, 200);
graphics.DrawImage(&background, 500, 0, 200, 200);
int width = forground.GetWidth();
int height = forground.GetHeight();
//分别设置前景图中的每一个像素的透明度
Color color, colorTemp;
for(int iRow = 0; iRow < height; iRow++)
{
for(int iColumn = 0; iColumn < width; iColumn++)
{
forground.GetPixel(iColumn, iRow, &color);
colorTemp.SetValue(color.MakeARGB(
125, color.GetRed(), color.GetGreen(), color.GetBlue()));
forground.SetPixel(iColumn, iRow, colorTemp);
}
}
graphics.DrawImage(&forground, 500, 0, 200, 200);
另外GDI+是没有ROP操作的
但现在的关键问题就是我知道他要进行下面这个操作.
pOldBrush = (CBrush *)pMDC->SelectObject(&brhRedBrush);
pMDC->SetBkColor(RGB(0, 0, 0));
pMDC->SetROP2(R2_XORPEN);
pMDC->Rectangle(&rect);可我不知道他还要进行
pMDC->SelectObject(brhBmpBrush);
pMDC->SetBkColor(RGB(0xFF, 0xFF, 0xFF));
pMDC->SetROP2(R2_MASKPEN);
pMDC->Rectangle(&rect);
和
pMDC->SelectObject(brhRedBrush);
pMDC->SetBkColor(RGB(0, 0, 0));
pMDC->SetROP2(R2_XORPEN);
pMDC->Rectangle(&rect);
这两个操作.
不知道我说的是否清楚.
再和大家说一下我目前的解决办法吧!
我从当前的Graphics中提取一个DC(记为pDC吧),并将目标所在区域的DC图象BitBlt到另外一个pMemDC中,然后将所要进行的当前绘图操作都在pDC中完成,然后用相应的ROP操作将pMemDC再BitBlt到pDC中.这样就完成了不用Graphics来进行ROP操作.目前不知道是否所有的ROP都可以这样来实现,正在对打印过程中调用某些特殊ROP而GDIPlus无法实现的情况进行测试.
谢谢大家的回复!!!
以前看过例子的,应该能保证效率另外broadoceans(broadoceans) 的方法显示缓慢,你可采用invalidate(FALSE)
不要使用TRUE,也就是不要擦除,重写覆盖即可。