Windows 内存绘图导出RGB
1, MFC 创建内存绘图显示设备
1)
CDC MemDC;
CBitmap MemBitmap;
CRect rect;
m_gl.GetClientRect(rect);
CDC *pDc = m_gl.GetDC();
MemDC.CreateCompatibleDC(pDc);
MemBitmap.CreateCompatibleBitmap(pDc, rect.Width(), rect.Height());
CBitmap *pOldBit = MemDC.SelectObject(&MemBitmap);
2)获取内存数据,窗口兼容的CDC一般是32位深度,RGBA格式
BITMAP bitmap;
LONG size = ::GetObject(MemBitmap, sizeof(BITMAP),&bitmap);
DWORD dwSize = bitmap.bmHeight * bitmap.bmWidthBytes;
unsigned char * ptr = new unsigned char[dwSize];
MemBitmap.GetBitmapBits(dwSize, ptr);
2, Win32
1)
HDC hDC = ::GetDC(m_windowWnd);
HDC hDCMem;
HBITMAP hBmpMem, hPreBmp;
/* 创建双缓冲区 */
// 创建与当前DC兼容的内存DC
hDCMem = CreateCompatibleDC(hDC);
// 创建一块指定大小的位图
//hBmpMem = ::CreateBitmap(iWidth, iHeight, 1, 32, pBmpData); hBmpMem = CreateCompatibleBitmap(hDC, 64, 64); // 将该位图选入到内存DC中,默认是全黑色的 hPreBmp = (HBITMAP)::SelectObject(hDCMem, hBmpMem);
2)
HBITMAP hbmp = hBmpMem;
if(!::GetObject(hbmp, sizeof(BITMAP), (LPBYTE)&bmptemp))
return 0;
BYTE imgSize = bmptemp.bmBitsPixel / 8; //兼容24bit和32bit位图
BITMAPINFO bi = { 0 };
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = bmptemp.bmWidth;
bi.bmiHeader.biHeight = bmptemp.bmHeight;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = bmptemp.bmBitsPixel;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = bmptemp.bmWidth * imgSize * bmptemp.bmHeight;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
// 获取位图数据
BYTE* lpvBits = new BYTE[bi.bmiHeader.biSizeImage];
::ZeroMemory(lpvBits, bi.bmiHeader.biSizeImage);
if(!::GetDIBits(hDCMem, hBmpMem, 0, bmptemp.bmHeight, lpvBits, &bi, DIB_RGB_COLORS)) {
delete[] lpvBits;
lpvBits = NULL;
}
3,GDI,以上两种方法背景默认黑色,可是设置填充掩码颜色,实现透明效果
<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html">Bitmap bmp(bitmap.bmWidth, bitmap.bmHeight, PixelFormat32bppARGB);
Graphics GDIplusObj(&bmp);
Color aa(200, 200, 200, 0);
Color bb = aa; Pen pen(aa, 3);
// aa.MakeARGB(100, 255, 0, 0);
SolidBrush brush(bb);
SolidBrush brush2(Color(100,255, 0, 0));
GDIplusObj.FillRectangle(&brush2, 100, 100, 100, 100);
Graphics GDIplusObj2(m_gl.GetDC()->GetSafeHdc());
GDIplusObj2.DrawImage(&bmp, Rect(0, 0, rect.Width(), rect.Height()));
//获取内存数据
//将Bitmap中的图像数据Buffer导出//用GDI的GetDIBits方法,用GDI+的LockBits方法