4
votes

Am taking screenshot of a particular window(hwnd) using PrintWindow API. This works absolutely fine, it takes screenshot of entire window. My question is : My window height is 742 & width is 653. If i want to take the screenshot somewhere in the middle of the window ( Not from the 0,0 ). How would i specify the x & y axis in PrintWindow. My snipnet code looks like this:

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've tried the alternative methods like BitBlt & StretchBlt where we have an option to specify the x & y axis. But for my project time efficiency is very very important. When i tried with BitBlt & StretchBlt time efficiency is around 25ms. But when i take the screenshot with PrintWindow its time efficiency is 6~8ms. So this would be very suitable to my project.

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

Thank you.

1

1 Answers

0
votes

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

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

You could try specifying an offset using SetWindowOrg/SetViewportOrg.

But those are just hints, you'll need to do the research on your own.