怎么获得指定CRect的图片
我有一个CRect,里面保存了父窗口某一个矩形的坐标,
怎么才能获得这块矩形里的图像呢?
[解决办法]
HBITMAP CopyDCToBitmap(HDC hScrDC, LPRECT lpRect)
{
HDC hMemDC; // 屏幕和内存设备描述表
HBITMAP hBitmap,hOldBitmap; // 位图句柄
int nX, nY, nX2, nY2; // 选定区域坐标
int nWidth, nHeight; // 位图宽度和高度
if (IsRectEmpty(lpRect)) // 确保选定区域不为空矩形
return NULL;
// 获得选定区域坐标
nX = lpRect-> left;
nY = lpRect-> top;
nX2 = lpRect-> right;
nY2 = lpRect-> bottom;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
hMemDC = CreateCompatibleDC(hScrDC);//为屏幕设备描述表创建兼容的内存设备描述表
// 创建一个与屏幕设备描述表兼容的位图
float factor = 1.0f;
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
while(!hBitmap)
{
factor -= 0.05f;
hBitmap = CreateCompatibleBitmap(hScrDC, (int)(nWidth*factor), (int)(nHeight*factor));
}
// 把新位图选到内存设备描述表中
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
// 把屏幕设备描述表拷贝到内存设备描述表中
StretchBlt(hMemDC,0,0,nWidth,nHeight,hScrDC,nX,nY,nWidth,nHeight,SRCCOPY);
//BitBlt(hMemDC, 0, 0, nWidth, nHeight,hScrDC, nX, nY, SRCCOPY);
//得到屏幕位图的句柄
hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
//清除
// DeleteDC(hScrDC);
DeleteDC(hMemDC);
DeleteObject(hOldBitmap);
// 返回位图句柄
return hBitmap;
}