BitBlt拷屏经验一则

 拷屏好像必须要有个Bitmap对象,不然无法复制成功,要么Load一个位图,要么CreateCompatibleBitmap创建一个兼容位图。

另:ReleaseDC 对应与 GetDC
        DeleteDC  对应与     CreateCompatibleDC
其他对象应该有类似对应关系,Get对应Release,Create对应Delete

 

 

void  CMainDlg::DoPaint()
{
    HDC dc 
= GetDC();
    HDC memDC 
= CreateCompatibleDC(dc);

    
//要用BitBlt从内存DC拷贝到设备DC似乎必须要有个Bitmap对象
    HBITMAP hBMP = CreateCompatibleBitmap(memDC, 240320);
    SelectObject(memDC,hBMP);
    RECT rc 
= {00100100};
    
    FillRect( memDC, 
&rc, CreateSolidBrush( RGB(000) ) );
    DrawText(memDC, L
"BitBlt bitmap from memory to screen"-1&rc, DT_CENTER|DT_VCENTER);
    BitBlt(dc, 
00100100, memDC, 00, SRCCOPY);

    DeleteObject(hBMP);
    DeleteDC(memDC);
    ReleaseDC(dc);
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Windows API中的BitBlt函数进行屏幕截图并保存。 具体步骤如下: 1. 获取屏幕的设备上下文(HDC),可以使用GetDC函数获取整个屏幕的设备上下文,也可以使用CreateDC函数获取指定显示器的设备上下文。 2. 创建一个和屏幕设备上下文相同的内存设备上下文(HDC),可以使用CreateCompatibleDC函数创建。 3. 创建一个和屏幕大小相同的位图对象(HBITMAP),可以使用CreateCompatibleBitmap函数创建。 4. 将位图对象选入内存设备上下文中,可以使用SelectObject函数。 5. 使用BitBlt函数将屏幕内容复制到内存设备上下文中的位图对象中。 6. 将位图对象保存为文件,可以使用SaveHBITMAPToFile函数。 7. 释放所有资源,包括设备上下文、位图对象和内存设备上下文,可以使用ReleaseDC函数、DeleteObject函数和DeleteDC函数。 以下是一个示例代码: ``` #include <Windows.h> void SaveHBITMAPToFile(HBITMAP hBitmap, LPCTSTR lpFileName) { BITMAP bmp; GetObject(hBitmap, sizeof(bmp), &bmp); BITMAPFILEHEADER bfh; bfh.bfType = 0x4D42; // "BM" bfh.bfSize = sizeof(bfh) + sizeof(BITMAPINFOHEADER) + bmp.bmWidthBytes * bmp.bmHeight; bfh.bfReserved1 = 0; bfh.bfReserved2 = 0; bfh.bfOffBits = sizeof(bfh) + sizeof(BITMAPINFOHEADER); BITMAPINFOHEADER bih; bih.biSize = sizeof(bih); bih.biWidth = bmp.bmWidth; bih.biHeight = bmp.bmHeight; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = BI_RGB; bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrUsed = 0; bih.biClrImportant = 0; BYTE* pBits = new BYTE[bmp.bmWidthBytes * bmp.bmHeight]; GetBitmapBits(hBitmap, bmp.bmWidthBytes * bmp.bmHeight, pBits); HANDLE hFile = CreateFile(lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile != INVALID_HANDLE_VALUE) { DWORD dwWritten; WriteFile(hFile, &bfh, sizeof(bfh), &dwWritten, NULL); WriteFile(hFile, &bih, sizeof(bih), &dwWritten, NULL); WriteFile(hFile, pBits, bmp.bmWidthBytes * bmp.bmHeight, &dwWritten, NULL); CloseHandle(hFile); } delete[] pBits; } int main() { HDC hdcScreen = GetDC(NULL); HDC hdcMem = CreateCompatibleDC(hdcScreen); HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); SelectObject(hdcMem, hBitmap); BitBlt(hdcMem, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), hdcScreen, 0, 0, SRCCOPY); SaveHBITMAPToFile(hBitmap, TEXT("screenshot.bmp")); ReleaseDC(NULL, hdcScreen); DeleteObject(hBitmap); DeleteDC(hdcMem); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值