How can I load a screenshot of the desktop into my HBITMAP var?
Thanks in advance!
How can I load a screenshot of the desktop into my HBITMAP var?
Thanks in advance!
Big thanks to anonytmouse for flat out giving me this awhile back ;):
Code:void Screenshot(HDC hDC, HWND hwnd)
{
/* Get the size of the screen */
SIZE size;
size.cx = GetSystemMetrics(SM_CXSCREEN);
size.cy = GetSystemMetrics(SM_CYSCREEN);
/* Get a device context for the screen */
HDC hdcScreen = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
/* Create a memory bitmap and put it in a memory DC so we can draw on it */
HDC hdcShot = CreateCompatibleDC(hdcScreen);
HBITMAP hBitmapShot = CreateCompatibleBitmap(hdcScreen, size.cx, size.cy);
HBITMAP hBitmapOld = (HBITMAP) SelectObject(hdcShot, hBitmapShot);
/* Copy the screen image to our memory bitmap */
BitBlt(hdcShot, 0, 0, size.cx, size.cy, hdcScreen, 0, 0, SRCCOPY);
/* Save our memory bitmap to file */
Save(hwnd, CreateInfoHeader(hwnd, hBitmapShot), hBitmapShot, hdcShot);
/* Cleanup memory bitmap and DC */
SelectObject(hdcShot, hBitmapOld);
DeleteObject(hBitmapShot);
DeleteDC(hdcShot);
/* Cleanup screen DC */
DeleteDC(hdcScreen);
}
Thanks, anonytmouse and tonto.
I'm sorry I don't understand something here, what is the HDC hDC passed for if it's not even used? And what is the filename?
Can someone explain why are there so many compatibale DCs?
Thats a good question about that "hDC" var.
Now I've got a problem.
I call this function, and use my windows HWND as the argument. But it doesn't print anything at all.Code:void Screenshot(HWND hwnd)
{
/* Get the size of the screen */
SIZE size;
size.cx = GetSystemMetrics(SM_CXSCREEN);
size.cy = GetSystemMetrics(SM_CYSCREEN);
/* Get a device context for the screen */
HDC hdcScreen = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
/* Create a memory bitmap and put it in a memory DC so we can draw on it */
HDC hdcShot = CreateCompatibleDC(hdcScreen);
HBITMAP hBitmapShot = CreateCompatibleBitmap(hdcScreen, size.cx, size.cy);
HBITMAP hBitmapOld = (HBITMAP) SelectObject(hdcShot, hBitmapShot);
/* Copy the screen image to our memory bitmap */
BitBlt(hdcShot, 0, 0, size.cx, size.cy, hdcScreen, 0, 0, SRCCOPY);
/* Save our memory bitmap to file */
//Save(hwnd, CreateInfoHeader(hwnd, hBitmapShot), hBitmapShot, hdcShot);
/* Cleanup memory bitmap and DC */
SelectObject(hdcShot, hBitmapOld);
DeleteObject(hBitmapShot);
DeleteDC(hdcShot);
/* Cleanup screen DC */
DeleteDC(hdcScreen);
HDC hdc = GetDC(hwnd);
HDC hdcMemory;
hdcMemory = CreateCompatibleDC(hdc);
SelectObject(hdcMemory, hBitmapShot);
BitBlt(hdc, 0, 0, size.cx, size.cy, hdcMemory, 0, 0, SRCCOPY);
SelectObject(hdcMemory, hBitmapOld);
BitBlt(hdc, 0, 0, size.cx, size.cy, hdcMemory, 0, 0, SRCCOPY);
DeleteDC(hdcMemory);
Beep(2000,100);
}
What's going on?
Nevermind. I figured it out!
let me guess you need the handle of the desktop, right?
Let me check...
Doesn't look like it.
Even if it did, HWND_DESKTOP works.
In that case what was the problem :DQuote:
Originally Posted by Cool-August
I get CreateInfoHeader undeclared error...
Save and CreateInfoHeader are custom functions for saving a bitmap to file. Tonto may provide them or you can use this MSDN code for storing a bitmap instead.
The same problem maxorator had, so I decided to search google's beta groups.Quote:
Originally Posted by Devil Panther
Quote:
Originally Posted by Devil Panther
HDC is the Direct Context which is used in win32 to write anything to the screen ex. lines or GFX anything like that or simply managing Bitmaps OpenGL uses HDC