我是Windows API / GDI的新手。我想在这段代码中做的就是将一个FULL-screenshot截取到我的内存缓冲区中。
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它的大小和格式),我看到了所有黑色。所以我的代码肯定有问题。我只是找不到错误!