将桌面显示获取并保存到bmp中

15 篇文章 1 订阅
14 篇文章 0 订阅

代码来自网络,在此分享:

//GrabScreenToBitmap.h
#ifndef GRABSCREENTOBITMAP_H
#define GRABSCREENTOBITMAP_H

HBITMAP GrabScrToBmp(LPRECT lpRect);

BOOL CopyBmpToClipBoard(const HBITMAP hBmp);

BOOL SaveBmpToFile(const HBITMAP hBmp, const LPCSTR lpFileName);

#endif

//GrabScreenToBitmap.cpp
HBITMAP GrabScrToBmp(LPRECT lpRect) //lpRect 代表选定区域 
{
    if (IsRectEmpty(lpRect))
    {
        return NULL;
    }
    
    HDC hScrDC;    // 屏幕设备描述表句柄 
    HDC hMemDC;    //内存设备描述表句柄 
    HBITMAP hBmp;  // 位图句柄 
    HBITMAP hOldBmp;
    int nX, nY, nX2, nY2; // 选定区域坐标
    int nWidth, nHeight; // 位图宽度和高度
    int xScrn, yScrn;  // 屏幕分辨率 
    
    hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
    hMemDC = CreateCompatibleDC(hScrDC);
    
    nX = lpRect->left;
    nY = lpRect->top;
    nX2 = lpRect->right;
    nY2 = lpRect->bottom;
    
    xScrn = GetDeviceCaps(hScrDC, HORZRES);
    yScrn = GetDeviceCaps(hScrDC, VERTRES);
    
    if (nX < 0)
    {
        nX = 0;
    }
    
    if (nY < 0)
    {
        nY = 0;
    }
    
    if (nX2 > xScrn)
    {
        nX2 = xScrn;
    }
    
    if (nY2 > yScrn)
    {
        nY2 = yScrn;
    }
    
    nWidth = nX2 - nX;
    nHeight = nY2 - nY;
    
    hBmp = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
    hOldBmp = (HBITMAP) SelectObject(hMemDC, hBmp);
    
    BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
    
    hBmp = (HBITMAP) SelectObject(hMemDC, hOldBmp);
    
    DeleteDC(hScrDC);
    DeleteDC(hMemDC);
    
    return hBmp;
    
}

BOOL CopyBmpToClipBoard(const HBITMAP hBmp)
{
    if (OpenClipboard(GetActiveWindow()))
    {
        EmptyClipboard();
        
        SetClipboardData(CF_BITMAP, hBmp);
        
        CloseClipboard();
        
        return true;
    }
    
    return false;
}

BOOL SaveBmpToFile(const HBITMAP hBmp, const LPCSTR lpFileName)
{
    HDC hDC;
    int nBits;
    WORD wBitCount;
    DWORD dwPaletteSize = 0;
    DWORD dwBmpBitSize;
    DWORD dwWritten;
    BITMAP bmp;
    BITMAPFILEHEADER bmpFileHdr;
    BITMAPINFO bmpInfo;
    LPBITMAPINFO lpBmpInfo;
    HANDLE hFile, hDib, hPal, hOldPal = NULL;
    
    hDC = CreateDC("Display", NULL, NULL, NULL);
    nBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
    DeleteDC(hDC);
    
    if (nBits <= 1)
    {
        wBitCount = 1;
    }
    else if (nBits <= 4)
    {
        wBitCount =4;
    }
    else if (nBits <= 8)
    {
        wBitCount = 8;
    }
    else if (nBits <= 16)
    {
        wBitCount = 16;
    }
    else if (nBits <= 24)
    {
        wBitCount = 24;
    }
    else
    {
        wBitCount = 32;
    }
    
    if (wBitCount <= 8)
    {
        dwPaletteSize = 1 << wBitCount * sizeof(RGBQUAD);
    }   
    
    GetObject(hBmp, sizeof(BITMAP), &bmp);
    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biWidth = bmp.bmWidth;
    bmpInfo.bmiHeader.biHeight = bmp.bmHeight;
    bmpInfo.bmiHeader.biPlanes = bmp.bmPlanes;
    bmpInfo.bmiHeader.biBitCount = wBitCount;
    bmpInfo.bmiHeader.biCompression = BI_RGB;
    bmpInfo.bmiHeader.biSizeImage = 0;
    bmpInfo.bmiHeader.biXPelsPerMeter = 0;
    bmpInfo.bmiHeader.biYPelsPerMeter = 0;
    bmpInfo.bmiHeader.biClrUsed = 0;
    bmpInfo.bmiHeader.biClrImportant = 0;
    
    dwBmpBitSize = (bmp.bmWidth * wBitCount + 31) / 32 * 4 * bmp.bmHeight;
    
    hDib = GlobalAlloc(GHND, sizeof(BITMAPINFOHEADER) + dwBmpBitSize + dwPaletteSize);
    lpBmpInfo = (LPBITMAPINFO) GlobalLock(hDib);
    *lpBmpInfo = bmpInfo;
    
    hPal = GetStockObject(DEFAULT_PALETTE);
    if (hPal)
    {
        hDC = GetDC(GetActiveWindow());
        hOldPal = SelectPalette(hDC, (HPALETTE) hPal, FALSE);
        RealizePalette(hDC);
    }
    
    GetDIBits(hDC, hBmp, 0, (UINT)bmp.bmHeight,
        (LPSTR)lpBmpInfo + sizeof(BITMAPINFOHEADER) + dwPaletteSize, (LPBITMAPINFO) lpBmpInfo, DIB_RGB_COLORS);
    
    if (hOldPal)
    {
        SelectPalette(hDC, (HPALETTE) hOldPal, TRUE);
        ReleaseDC(GetActiveWindow(), hDC);
        RealizePalette(hDC);
    }
    
    hFile = CreateFile(lpFileName,
        GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        return FALSE;
    }
    
    bmpFileHdr.bfType = 0x4d42;
    bmpFileHdr.bfSize = sizeof(BITMAPFILEHEADER)
        + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmpBitSize;
    bmpFileHdr.bfReserved1 = 0;
    bmpFileHdr.bfReserved2 = 0;
    bmpFileHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) 
        + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
    
    WriteFile(hFile, (LPSTR)&bmpFileHdr, 
        (DWORD)sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
    
    WriteFile(hFile, (LPSTR)lpBmpInfo, 
        (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmpBitSize, &dwWritten, NULL);
    
    GlobalUnlock(hDib);
    GlobalFree(hDib);
    CloseHandle(hFile);
    
    return TRUE;
    
}
#endif GRABSCREENTOBITMAP_H


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值