Displaying HBITMAP

2019-07-19 19:43发布

问题:

I apologize upfront; I'm new to c and I really don't know what I'm doing.

I am trying to capture and display a screen shot using Windows 7 and c. I know it sounds dumb, eventually I will need each of these functions in different programs, the one that captures the screen will send the image to the one that displays it but for now I am just trying to get them both to work in one program. Here is the code I've put together so far:

     #include <windows.h>

bool ScreenCapture(int x, int y, int width, int height){
// get a DC compat. w/ the screen
HDC hDc = CreateCompatibleDC(0);

// make a bmp in memory to store the capture in
HBITMAP hBmp = CreateCompatibleBitmap(GetDC(0), width, height);

// join em up
SelectObject(hDc, hBmp);

// copy from the screen to my bitmap
BitBlt(hDc, 0, 0, width, height, GetDC(0), x, y, SRCCOPY);

HDC myhDc = GetDC(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);

BitBlt(myhDc, 0, 0, width, height, hDc, x, y, SRCCOPY);

//Display bitmap
DrawBitmap(myhDC,0,0,hBmp,SRCCOPY);

//GetDC(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);

// free the bitmap memory
DeleteObject(hBmp);

return 0;
}

int main(){
ScreenCapture(500, 200, 300, 300);
system("pause");
}

I just haven't been able to find any way to display the HBITMAP. How do I display the HBITMAP?

Update: I added DrawBitmap but it isn't working... I am getting the following output and am not sure how to process it:

'win_screenshot.exe': Loaded 'C:\Users\mbrooker\Documents\Visual Studio 2010\Projects\win_screenshot\Debug\win_screenshot.exe', Symbols loaded. 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded. 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'C:\Windows\SysWOW64\apphelp.dll', Cannot find or open the PDB file 'win_screenshot.exe': Loaded 'ImageAtBase0x4ab60000', Loading disabled by Include/Exclude setting. 'win_screenshot.exe': Unloaded 'ImageAtBase0x4ab60000' The program '[1480] win_screenshot.exe: Native' has exited with code 0 (0x0).

回答1:

Displaying a HBITMAP would involve blitting it (e.g. via BitBlt) into another DC. In a way similar to what you are already doing, just to another DC, which might be for example window's DC, see GetDC.

MSDN provides you with sample code: Scaling an Image, see DrawBitmap function.