[英]Take foreground application screenshot in Windows using Java without extra edges
I am able to take screenshot of foreground image using below code 我可以使用下面的代码截取前景图像的截图
void startScreencapture(){
RECT dimensionsOfWindow = new RECT();
User32.INSTANCE.GetWindowRect(User32.INSTANCE.GetForegroundWindow(), dimensionsOfWindow );//now in the dimensionsOfWindow you have the dimensions
Robot robot = new Robot();
buf = robot.createScreenCapture( dimensionsOfWindow.toRectangle() );
}
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
HWND GetForegroundWindow(); // add this
int GetWindowTextA(PointerType hWnd, byte[] lpString, int nMaxCount);
public boolean GetWindowRect(HWND hWnd, RECT rect);
}
I am getting foreground screenshots like below 我正在获得如下的前景截图
If you notice, the screenshot has some extra image at the border of the window. 如果您注意到,屏幕截图在窗口的边框处有一些额外的图像。
I do not want extra image part with my screenshot. 我的屏幕截图不需要额外的图像部分。
Is it possible to somehow manipulate 有可能以某种方式操纵
User32.INSTANCE.GetForegroundWindow()
User32.INSTANCE.GetForegroundWindow()
so that I get the screenshot without the extra part? 这样我就可以获得没有额外部分的截图?
I feel like answer in this link should work. 我觉得这个链接的答案应该有效。 What is the difference between GetClientRect and GetWindowRect in WinApi?
WinApi中的GetClientRect和GetWindowRect有什么区别?
But when I replace GetWindowRect with GetClientRect I get below screenshot: 但是当我用GetClientRect替换GetWindowRect时,我得到以下屏幕截图:
Ideally I should have got screenshot of only the foreground application. 理想情况下,我应该只有前台应用程序的截图。
Edit: Daniel Widdis kindly found a similar question for me: getwindowrect-returns-a-size-including-invisible-borders 编辑:丹尼尔Widdis亲切地为我找到了一个类似的问题: getwindowrect-returns-a-size-including-invisible-borders
This has a possible answer ie get the border thickness in Windows 10 and adjust this thickness to get the screenshot I want. 这有一个可能的答案,即在Windows 10中获取边框厚度并调整此厚度以获取我想要的屏幕截图。 But this answer uses
DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT));
但是这个答案使用了
DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT));
which is possibly C code. 这可能是C代码。
If I can find how to find border thickness in Java, it would solve my problem. 如果我能找到如何在Java中找到边框厚度,它将解决我的问题。
GetClientRect : GetClientRect :
The client coordinates specify the upper-left and lower-right corners of the client area.
客户端坐标指定客户区的左上角和右下角。 Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).
因为客户端坐标是相对于窗口客户区左上角的,所以左上角的坐标是(0,0)。
This is a relative coordinate system. 这是一个相对坐标系。
You should also call the ClientToScreen to convert the Client coordinate to screen coordinate. 您还应该调用ClientToScreen将Client坐标转换为屏幕坐标。
Notice that ClientToScreen
only receive the parameter of POINT
(not a RECT
), and you can find the POINT
class here . 请注意,
ClientToScreen
只接收POINT
(不是RECT
)的参数,您可以在此处找到POINT
类。
EDIT: 编辑:
GetWindowRect
will get a "extra" size. GetWindowRect
将获得“额外”大小。 However, GetClientRect
does exactly not include it(and other extra information like title bar, window border, etc). 但是,
GetClientRect
确实不包含它(以及其他额外信息,如标题栏,窗口边框等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.