简体   繁体   English

使用Java在Windows中获取前台应用程序屏幕截图,无需额外边

[英]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.

相关问题 如何截取任何 java 本机应用程序而不将其带到 Java 中的前台? - How to take screenshot of any java native application without bringing it to foreground in Java? 拍摄Java中Windows(.exe)上特定应用程序的屏幕截图 - Take a screenshot of a specific application on Windows (.exe) in java 如何使用 selenium java 进行彩色截图 - how to take color screenshot using selenium java 如何使用IP地址在Java中拍摄屏幕截图 - How to take Screenshot in Java using IP address 如何在Windows(ffmpeg等)中使用Java快速截取桌面? - How to take a screenshot of desktop fast with Java in Windows (ffmpeg, etc.)? Java:确定String是否为URL(没有www或http),获取其屏幕截图 - Java : Determine if String is an URL(without www or http), take its screenshot 使用ADB截屏并在Java中检索它而无需编写文件 - Take screenshot with ADB and retrieve it in java WITHOUT writing a file 如何从Java独立应用程序在前台打开Windows应用程序 - How to open windows application in foreground from Java standalone application 如何在 Windows 中使用 JNA 截取屏幕截图? - How to take a screenshot with JNA in Windows? 在原生 Java 中截取屏幕截图并在 flutter 中显示此屏幕截图 - Take screenshot in native java and display this screenshot in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM