VC 스크린샷 및 BMP 파일로 저장

2798 단어
쓸데없는 말 작작 하고 사이사이 코드:
BOOL SaveScreenToBitmap(CString strFileName)
{
    CDC dc;
    dc.CreateDC("DISPLAY", NULL, NULL, NULL);
    CBitmap bm;
    int Width = GetSystemMetrics(SM_CXSCREEN);
    int Height = GetSystemMetrics(SM_CYSCREEN);
    bm.CreateCompatibleBitmap(&dc, Width, Height);
    CDC tdc;
    tdc.CreateCompatibleDC(&dc);
    CBitmap* pOld = tdc.SelectObject(&bm);
    tdc.BitBlt(0, 0, Width, Height, &dc, 0, 0, SRCCOPY);
    tdc.SelectObject(pOld);
    BITMAP btm;
    bm.GetBitmap(&btm);
    DWORD size = btm.bmWidthBytes * btm.bmHeight;
    LPSTR lpData = (LPSTR)GlobalAlloc(GPTR, size);
    
    BITMAPINFOHEADER bih;
    bih.biBitCount = btm.bmBitsPixel;
    bih.biClrImportant = 0;
    bih.biClrUsed = 0;
    bih.biCompression = 0;
    bih.biHeight = btm.bmHeight;
    bih.biPlanes = 1;
    bih.biSize = sizeof(BITMAPINFOHEADER);
    bih.biSizeImage = size;
    bih.biWidth = btm.bmWidth;
    bih.biXPelsPerMeter = 0;
    bih.biYPelsPerMeter = 0;
    
    GetDIBits(dc, bm, 0, bih.biHeight, lpData, (BITMAPINFO *)&bih, DIB_RGB_COLORS);
    
    BITMAPFILEHEADER bfh;
    bfh.bfReserved1 = bfh.bfReserved2 = 0;
    bfh.bfType = ((WORD)('M' << 8) | 'B');
    bfh.bfSize = 54 + size;
    bfh.bfOffBits = 54;
    CFile bf;
    if (bf.Open(strFileName, CFile::modeCreate | CFile::modeWrite))
    {
        bf.WriteHuge(&bfh, sizeof(BITMAPFILEHEADER));
        bf.WriteHuge(&bih, sizeof(BITMAPINFOHEADER));
        bf.WriteHuge(lpData, size);
        bf.Close();
    }
    else
    {
        return FALSE;
    }
    
    GlobalFree(lpData);
    
    return TRUE;
} 

좋은 웹페이지 즐겨찾기