简体   繁体   English

使用PrintWindow API获取屏幕截图(客户端区域)

[英]Taking Screenshot ( client area ) using PrintWindow API

Am taking screenshot of a particular window(hwnd) using PrintWindow API. 我正在使用PrintWindow API截取特定窗口(hwnd)的屏幕截图。 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 ). 我的问题是:我的窗口高度为742,宽度为653.如果我想在窗口中间某处拍摄屏幕截图(不是从0,0开始)。 How would i specify the x & y axis in PrintWindow. 如何在PrintWindow中指定x和y轴。 My snipnet code looks like this: 我的snipnet代码如下所示:

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. 我尝试过像BitBlt和StretchBlt这样的替代方法,我们可以选择指定x和y轴。 But for my project time efficiency is very very important. 但对于我的项目来说,时间效率非常重要。 When i tried with BitBlt & StretchBlt time efficiency is around 25ms. 当我尝试使用BitBlt和StretchBlt时,效率大约是25ms。 But when i take the screenshot with PrintWindow its time efficiency is 6~8ms. 但是当我用PrintWindow拍摄截图时,其时间效率为6~8ms。 So this would be very suitable to my project. 所以这对我的项目来说非常合适。

But PrintWindow API always captures the window image from (0,0). 但PrintWindow API始终从(0,0)捕获窗口图像。 Can someone tellme/suggest me how to take the screenshot of the window at specified location say at x=20 & y=30. 有人可以告诉/建议我如何在指定位置拍摄窗口的屏幕截图,例如x = 20&y = 30。

Thank you. 谢谢。

It is not possible to specify a rectangle to PrintWindow , obviously, since it does not accept such a parameter. 显然,不可能为PrintWindow指定一个矩形,因为它不接受这样的参数。

There might be a workaround. 可能有一种解决方法。 Try specifying the clipping region for the device context, using SelectClipRgn . 尝试使用SelectClipRgn指定设备上下文的剪切区域。 Use CreateRectRgn to create a region from a rectangle. 使用CreateRectRgn从矩形创建区域。 If you're lucky, the WM_PAINT handler will take the clipping region into account. 如果幸运的话,WM_PAINT处理程序会考虑剪切区域。

You could try specifying an offset using SetWindowOrg / SetViewportOrg . 您可以尝试使用SetWindowOrg / SetViewportOrg指定偏移量。

But those are just hints, you'll need to do the research on your own. 但这些只是提示,你需要自己做研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM