Taking a screenshot (client area) using the PrintWindow API

Takes a screenshot for a specific window (hwnd) using the PrintWindow API. This works absolutely fine; a screenshot of the entire window is required. My question is: My window height is 742 and width is 653. If I want to take a screenshot somewhere in the middle of the window (not from 0,0). As I would point the x and y axes in PrintWindow. My snipnet code is as follows:

void Screenshot() { CImage image; image.Create(imageWidth, imageHeight, 24); CImageDC imageDC(image); HWND hwnd = ::FindWindow(0,"EIML"); PrintWindow(hwnd, imageDC, PW_CLIENTONLY); image.Save("H:\\out.jpg",ImageFormatJPEG); } 

I tried alternative methods like BitBlt and StretchBlt, where we can specify the x and y axes. But for my design efficiency, time is very important. When I tried with BitBlt and StretchBlt, the time efficiency is about 25 ms. But when I take a screenshot with PrintWindow, its time efficiency is 6 ~ 8 ms. Therefore, this would be very useful for my project.

But the PrintWindow API always captures a window image from (0,0). Can someone tell / suggest me how to take a screenshot at the specified location, say, at x = 20 and y = 30.

Thanks.

+4
source share
1 answer

It is not possible to point the rectangle to PrintWindow , obviously, since it does not accept such a parameter.

There may be a workaround. Try specifying a clipping region for the device context using SelectClipRgn . Use CreateRectRgn to create an area from the rectangle. If you're lucky, the WM_PAINT handler will take into account the clipping region.

You can try to specify the offset using SetWindowOrg / SetViewportOrg .

But these are just hints, you will need to conduct the study yourself.

0
source

All Articles