1

我对 Windows API/GDI 很陌生。我想要在这段代码中做的就是在我的内存缓冲区中截取一个完整的屏幕截图。

int __cdecl main(void) 
{
    int width = GetSystemMetrics( SM_CXSCREEN );
    int height = GetSystemMetrics( SM_CYSCREEN );
    char *bmpBuf;
    FILE* bmpFile;
    HDC dcScreen = GetDC( NULL );
    HDC dcCapt = CreateCompatibleDC( dcScreen );
    HBITMAP hbmpCapt = CreateCompatibleBitmap( dcScreen, width, height );
    BITMAP bmpCapt;
    BITMAPINFOHEADER bi;
    size_t bmpSize;
    SelectObject( dcCapt, hbmpCapt );
    BitBlt( dcCapt, 0,0, width, height, dcScreen, 0,0, SRCCOPY | CAPTUREBLT );
    GetObject( hbmpCapt, sizeof(BITMAP), &bmpCapt );
    bmpSize = ((bmpCapt.bmWidth*32+31)/32)*4*bmpCapt.bmHeight;
    bmpBuf = (char*)malloc( bmpSize );
    GetDIBits( dcCapt, hbmpCapt, 0, (UINT)bmpCapt.bmHeight, bmpBuf, (LPBITMAPINFO)&bi, DIB_RGB_COLORS );

    bmpFile = fopen( "screenshot.raw", "w" );
    if( !bmpFile )
        printf( "Can't open file\n" );
    else {
        fwrite( bmpBuf, 1, bmpSize, bmpFile );
        fclose( bmpFile );
    }
    printf( "Image( %d x %d ) written\n", bmpCapt.bmWidth, bmpCapt.bmHeight );

    DeleteDC( dcCapt );
    DeleteObject( hbmpCapt );

    return 0;
} 

我设法将我的内存缓冲区保存到一个 .raw 文件中。但是,当我在 Photoshop 中打开这个 .raw 文件时(我告诉 PS 它的大小和格式),我看到全黑。所以我的代码一定有问题。我只是找不到错误!

4

0 回答 0