网上大部分资料都是将DDB转换为DIB后,得到图像内容的,可直接得到DDB内容的资料并不多,下面我以截屏为例说下怎么得到DDB的内存,一些API是我从网上找到的,根据我的理解使用的,可能用的不很准确,请大家指出.我没用mfc,以下全是API调用。
void TestGetDDB()
{
// 设置截屏大小
LPRECT lpRect=new RECT;
lpRect->top=0;
lpRect->left=0;
lpRect->right=300;
lpRect->bottom=500;
HDC hScrDC, hMemDC;
// 屏幕和内存设备描述表
HBITMAP hBitmap, hOldBitmap;
// 位图句柄
int nX, nY, nX2, nY2;
// 选定区域坐标
int nWidth, nHeight;
// 确保选定区域不为空矩形
if (IsRectEmpty(lpRect))
return ;
//为屏幕创建设备描述表
hScrDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
//为屏幕设备描述表创建兼容的内存设备描述表
hMemDC = CreateCompatibleDC(hScrDC);
// 获得选定区域坐标
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
//确保选定区域是可见的
if (nX < 0)
nX = 0;
if (nY < 0)
nY = 0;
if (nX2 > m_xScreen)
nX2 = m_xScreen;
if (nY2 > m_yScreen)
nY2 = m_yScreen;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
// 创建一个与屏幕设备描述表兼容的位图
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
// 把新位图选到内存设备描述表中
(HBITMAP)SelectObject(hMemDC, hBitmap);
// 把屏幕设备描述表拷贝到内存设备描述表中
BitBlt(hMemDC, 0, 0, nWidth, nHeight,
hScrDC, nX, nY, SRCCOPY);
BITMAP bitmap;
// 得到位图信息
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
// 计算位图大小
DWORD dwSize = bitmap.bmHeight * bitmap.bmWidthBytes;
// new点内存空间
unsigned char* pBits = new unsigned char[dwSize];
// 将位图内容拷贝到指定空间
LONG dl = GetBitmapBits(hBitmap, dwSize, pBits);
//到此pBits中就是DDB内容
//清除
DeleteDC(hScrDC);
DeleteDC(hMemDC);
delete(pBits);
return;
}