Get a window snapshot and crop it - c++

I have the following code working properly, it takes a snapshot of the active window on my app, puts it in a HBITMAP variable and saves it in a file.
Now I would like to be able to crop the image and save only a portion of it according to given start coordinates and width/height.
An important point is that I have to save the window with the title bar, not just the client area, so it was easy to achieve with PrintWindow() rather than the BitBlt() approach.
I prefer a solution that will use PrintWindow(), because the BitBlt() approach doesn't take the title bar properly (unless you know the way of doing that).
The current code that works properly for the whole window is:
HWND hParentWindow = GetActiveWindow();
RECT rc;
GetWindowRect(hParentWindow, &rc);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
//create
HDC hdcParent = GetDC(NULL);
HDC hdc = CreateCompatibleDC(hdcParent);
HBITMAP hBmp = CreateCompatibleBitmap(hdcParent, width, height);
SelectObject(hdc, hBmp);
//Print to memory hdc
PrintWindow(hParentWindow, hdc, 0);
//copy to clipboard
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBmp);
CloseClipboard();
// Save it in a file:
saveBitmap(ofn.lpstrFile, hBmp);
//release
DeleteDC(hdc);
DeleteObject(hBmp);
ReleaseDC(NULL, hdcParent);
How can I save the bitmap cropped?

Essentially do a BitBlt. Here is a thread discussing this issue with a solution that appears to be adequate for your needs here:
Crop function BitBlt(...)

create another intermediate hdc
print the window to this intermediate hdc.
copy (bitblt) the rect you need from this hdc to your bitmap hdc
relase the intermediate hdc

Related

Loading Windows screenshot into SFML Image

In C++ code, I need to take a screenshot of a given window, store the pixels in memory, and then load them into an SFML Image object. So far, I have a handle to the window I need to screenshot and can create an HBITMAP object of it:
HWND window = FindWindow(TEXT("WindowName"), NULL);
RECT rc;
GetClientRect(window , &rc);
HBITMAP hbmp;
HDC hdcScreen = GetDC(NULL);
HDC hdc = CreateCompatibleDC(hdcScreen);
hbmp = CreateCompatibleBitmap(hdcScreen,
rc.right - rc.left, rc.bottom - rc.top);
SelectObject(hdc, hbmp);
PrintWindow(window, hdc, PW_CLIENTONLY);
GetObject(hbmp, sizeof(BITMAP), bitmap);
Now to be honest, I'm not really much of a Windows guy and I don't understand what's really happening with this WinAPI stuff here, this is just some code I found online to get screenshots. What I need to do is get this into some type of standard format that I can pass into SFML's function:
sf::Image::loadFromMemory(const void * data, std::size_t size)
And preferably something that will also be easy to send over a socket connection later. How can I get a nice, non-Windows-specific format for my screenshot?

White Flicker after CombineRgn function

It seems the flickering is generated by the CombineRgn function, but I really have no idea why this happens, since i've never used regions that much I'm possibly missing some knowledge on the matter.
Some events in the program triggers the addition of little rectangles to the main region, here's the code that handles that:
HRGN ActualRegion = CreateRectRgn(0, 0, 0, 0);
GetWindowRgn(hwnd, ActualRegion);
HRGN AddedRect = CreateRectRgn(//long code that creates a rectangle)
CombineRgn(ActualRegion, ActualRegion, AddedRect, RGN_OR);
SetWindowRgn(hwnd, ActualRegion, FALSE);
InvalidateRect(hwnd, NULL, FALSE);
White Flickering appears only after the invalidation if new regions where combined to the main one.
Here's how I'm implementing double buffering in WM_PAINT:
PLEASE NOTE that on creation i'm enabling the DWM blur behind function with an invalid region (different from the Main one) which means that everything painted with BLACK_BRUSH will result in a 100% "invisible" portion of the program
RECT r; GetClientRect(hwnd, &r);
PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps);
HDC MemDc = CreateCompatibleDC(hdc);
HBITMAP hBmp = CreateCompatibleBitmap(hdc, r.right, r.bottom);
HBITMAP hOld = (HBITMAP)SelectObject(MemDc, hBmp);
//Making sure this dc is filled with "invisible" pixels to display
SelectObject(MemDc, GetStockObject(BLACK_BRUSH));
Rectangle(MemDc, //arbitrary values that matches the entire screen);
BitBlt(hdc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), MemDc, 0, 0, SRCCOPY);
//clean-up
SelectObject(MemDc, hOld);
DeleteObject(hBmp);
DeleteDC(MemDc);
EndPaint(hwnd, &ps);
WM_ERASEBKGND obviously returns TRUE without further handling, the WNDCLASSEX instance of the window has a default BLACK_BRUSH as the hbrBackground field.
I also tried to intercept and return TRUE from WM_NCPAINT message.
I'm doing everything necessary to avoid intermediate drawcalls, everything handled inside the WM_PAINT uses a backbuffer, also i'd like to mention i'm not working with images/bitmaps. Everything is drawn with gdi/gdi+, and in no place i'm actually issuing a "white" redraw that may possibly cause said flicker. I'm a bit lost here
Is it something that i'm possibly missing ? I can't really understand what may be causing white flickering in this scenario
The problem is not the CombineRgn function but the SetWindowRgn function which you call before the system draws the window for the first time. If you call SetWindowRgn after the first draw, no flicker. Unfortunatelly I don't know why. So, a way to counter that is to set the window region after the first draw (take the code that sets window region from WM_CREATE and leave only the DwmEnableBlurBehindWindow):
static int stc = 0;
//in WM_PAINT after the EndPaint(hwnd, &ps); add
HRESULT lr = DefWindowProc(hwnd, message, wParam, lParam);
if( stc == 0 ){
OnlyOnce();
stc++;
}
return lr;
and the OnlyOnce:
void OnlyOnce(void){
int X_Screen = GetSystemMetrics(SM_CXSCREEN);
int Y_Screen = GetSystemMetrics(SM_CYSCREEN);
HRGN ActualRegion = CreateRectRgn(X_Screen - 100, Y_Screen - 100, X_Screen - 100 + 40, Y_Screen - 100 + 40);
SetWindowRgn(hWnd, ActualRegion, true);
return;
}

Getting a Bitmap Object of a screenshot in VC++

I'm trying to write a program that grabs a screen shot of the current active window and then does some basic computer vision with the result. The current code I have is as follows:
RECT rc;
HWND hwnd = GetForegroundWindow(); //the window can't be min
if(hwnd == NULL) {
cerr << "it can't find any 'note' window" << endl;
system ("pause");
return 0;
}
GetClientRect(hwnd, &rc);
//create
HDC hdcScreen = GetDC(NULL);
HDC hdc = CreateCompatibleDC(hdcScreen);
HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, rc.right - rc.left, rc.bottom - rc.top);
SelectObject(hdc, hbmp);
//Print to memory hdc
PrintWindow(hwnd, hdc, PW_CLIENTONLY);
//Get the bitmap
BITMAP bm;
GetObject (hbmp, sizeof(bm), &bm);
//release
DeleteDC(hdc);
DeleteObject(hbmp);
ReleaseDC(NULL, hdcScreen);
After everything's said an done, bm.bmBits is NULL. This is especially strange because all of bm's other fields are correct. Obviously I can't do very much without bm.bmBits. Could anyone tell me what I'm doing wrong?
This code is mostly copied from the answer to another question, but in that code handle to a bitmap was never transferred into a BITMAP struct, but instead was used to add the bitmap to the clipboard. Since I want to process the screenshot immediately, I'd rather extract the data within the handle immediately and process it.
bm.bmBits is NULL because you don't own the memory referenced by the bitmap. You need to have the kernel copy them for you, using GetDIBits or the like.

WM_PAINT handler doesn't perform as it should, flicker-like effect occurs

INTRODUCTION:
I have decided to make a test project in MS Visual Studio 2008, in C++ to test a small program in pure WIN32, regarding painting a bitmap as window's background.
PROBLEM:
Window should have gray brush and a bitmap picture stretched over its client area.
In my code for WM_PAINT, if I try to paint gray brush for window, without bitmap, everything seems to work well.
Same goes if I just try to put a bitmap as a background.
Yet, when I combine these two, so I can get a bitmap picture stretched, and gray background behind bitmap, this is what happens:
Bitmap picture appears to "stands still", but gray brush appears over entire window for a second, then disappears entirely, so only stretched bitmap is seen, and then appears again, and so on.
It seems as if it is drawn from top going to the bottom, and it seems as if application is doing it all over again.
This is how I see it when I start my program.
RELEVANT INFORMATION:
The program was made by choosing option File->New, and then choosing Win32 project from the options.
Window class was set automatically, and the following members were set like this:
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
I have added static global variable to store bitmap handle:
static HBITMAP bmp;
In the window procedure, made by the wizard, I have initialized it with following code:
case WM_CREATE:
bmp = LoadBitmap( hInst, MAKEINTRESOURCE(IDB_BITMAP1) );
return 0;
break;
In the window procedure, made by the wizard, I have added WM_PAINT handler, with following code:
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
RECT r;
GetClientRect( hWnd, &r );
// TODO: Add any drawing code here...
// fill client area with gray brush, just to test
FillRect( hdc, &r, (HBRUSH)GetStockObject( GRAY_BRUSH ) );
// memory DC for double buffering
HDC MemDC = CreateCompatibleDC( hdc );
// select our bitmap into memory DC
HBITMAP old = (HBITMAP)SelectObject( MemDC, bmp );
// get bitmap's width and height so we can stretch it
BITMAP b;
GetObject( bmp, sizeof(BITMAP), &b );
// stretch our bitmap
StretchBlt( hdc, 0, 0, r.right - r.left, r.bottom - r.top,
MemDC, 0, 0, b.bmWidth, b.bmHeight, SRCCOPY );
// perform proper cleanup
SelectObject( MemDC, old );
DeleteDC(MemDC);
EndPaint(hWnd, &ps);
}
return 0L;
break;
I have also invalidated client area when window is resized, or erasing of background happens, like this:
case WM_ERASEBKGND:
InvalidateRect( hWnd, NULL, TRUE );
return 1L;
break;
case WM_SIZE:
InvalidateRect( hWnd, NULL, TRUE );
return 0L;
Bitmap is destroyed like this:
case WM_DESTROY:
DeleteObject( bmp );
PostQuitMessage(0);
break;
IMPORTANT NOTE:
Even if I comment out handlers for WM_SIZE and WM_ERASEBKGND, the effect still occurs.
I do not have much experience with double buffering, but this is simple thing to do.
I just fail to see the mistake, so I ask more experienced and skillfull colleagues to help.
If additional source code is required, ask for it and I will post it, but until then I will omit it to keep the question brief.
You should not call InvalidateRect in WM_ERASEBKGND. That's just going to force an endless series of paint cycles.
The job of WM_ERASEBKGND is to paint the background, and that's all you should ever do. If your WM_PAINT is going to paint the entire window, then there's no need to paint any background. In which case, and I think this is your scenario, you should do nothing in WM_ERASEBKGND.

How to debug code in WM_PAINT in c++?

I have the following code in the WM_PAINT message handler of the main window :
void BossController::paint ( HWND hwnd, HBITMAP skin )
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint ( hwnd, &ps );
HDC dcSkin = CreateCompatibleDC ( hdc ); // memory dc for skin
HDC hMemDc = CreateCompatibleDC ( hdc ); // memory dc for painting
HBITMAP hmemBmp = CreateCompatibleBitmap ( hdc, width, height ); // Create bitmap to draw on
HBITMAP hOldMemBmp = (HBITMAP)SelectObject ( hMemDc, hmemBmp ); // select memory bitmap in memory dc
HBITMAP hOldSkinBmp = (HBITMAP)SelectObject ( dcSkin, skin ); //select skin bitmap in skin memory dc
BitBlt ( hMemDc, 0, 0, width, height, dcSkin, 0, 0, SRCCOPY ); // Paint Skin on Memory DC
BitBlt ( hdc, 0, 0, width, height, hMemDc, 0, 0, SRCCOPY ); // Paint Skin on Window DC
DeleteObject ( hOldSkinBmp );
DeleteObject ( hOldMemBmp );
DeleteObject( hmemBmp );
DeleteDC ( hMemDc );
DeleteDC ( dcSkin );
EndPaint ( hwnd, &ps );
};
I will be painting text on the skin aswell, that's why I am BitBlt ing on memory DC with a memory bitmap, I have tried with straight painting(directly to hdc) as well, but none worked, and I am not sure how to debug it. The only thing I could check was to check the skin against NULL in LoadBitmap function's return value and also in the void BossController::paint ( HWND hwnd, HBITMAP skin ). And BitBlt's return value.
It always shows a rectangle with the background color I chose while creating the window. (window is a custom skinned one so, no title bar etc is there.
Can someone point out the errors if any or the potential pitfalls or maybe how to debug it ?
It looks like nobody ever answered this.
First off, I don't know what framework you're using, but there are normally a few checks before one starts painting. A typical paint handler begins something like:
RECT r;
if (GetUpdateRect(&r) == 0)
{
// Nothing to paint, exit function.
return 0;
}
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
if (hdc == 0)
{
// No display device context available, exit function.
return 0;
}
Second, I don't know what 'skin' is (an HBITMAP, but I don't know anything about how it was created, where it come from, what its dimensions and bit depth are).
Third, your cleanup is incorrect. Always call SelectObject to restore the previous selected bitmap, pen, brush, etc. Then call DeleteObject on whatever you created.
As for how to debug, if something's broken in a paint handler, you should always strip it to the bare minimum, verify functionality, and start adding in things until it breaks. In this case, I would replace all of your existing code with a simple filled rectangle in a weird color and see if that works. Then I'd do a BitBlt directly from the 'skin' bitmap on top of the funky color, and see if that works.
The most common errors with GDI programming are caused by things like using the wrong coordinates (e.g. Window coordinates instead of Client coordinates, or giving the wrong offset into your source in a BitBlt call), failure to correctly create resources (e.g. CreateCompatibleDC, then calling CreateCompatibleBitmap using the new DC, which results in a monochrome bitmap), or failure to properly clean up (e.g. the way you didn't select the old resources before disposing of your newly-created ones). It's also common to not even initiate a repaint correctly, such that your WM_PAINT handler isn't even getting hit. The first step is always a breakpoint to make sure your code is even executing!