在Windows中使用Java拍摄前台应用程序屏幕快照,无需额外的边缘 [英] Take foreground application screenshot in Windows using Java without extra edges

查看:38
本文介绍了在Windows中使用Java拍摄前台应用程序屏幕快照,无需额外的边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码拍摄前景图像的屏幕截图

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?

我觉得此链接中的答案应该有效. WinApi中的GetClientRect和GetWindowRect有什么区别?

I feel like answer in this link should work. What is the difference between GetClientRect and GetWindowRect in WinApi?

但是当我用GetClientRect替换GetWindowRect时,我得到以下屏幕截图:

But when I replace GetWindowRect with GetClientRect I get below screenshot:

理想情况下,我应该只拥有前景应用程序的屏幕截图.

Ideally I should have got screenshot of only the foreground application.

修改: 丹尼尔·威迪斯(Daniel Widdis)为我找到了一个类似的问题: getwindowrect-returns-a-size-包括无形边界

Daniel Widdis kindly found a similar question for me: getwindowrect-returns-a-size-including-invisible-borders

这可能是一个答案,即在Windows 10中获取边框粗细并调整该粗细以获取所需的屏幕截图.但是这个答案用 DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT));,可能是C代码.

This has a possible answer i.e. get the border thickness in Windows 10 and adjust this thickness to get the screenshot I want. But this answer uses DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT)); which is possibly C code.

如果我能找到如何在Java中查找边框粗细,那就可以解决我的问题.

If I can find how to find border thickness in Java, it would solve my problem.

推荐答案

GetClientRect :

客户坐标指定左上角和右下角 客户区.因为客户坐标是相对于 窗口客户区的左上角, 左上角是(0,0).

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).

这是一个相对坐标系.

您还应该调用 ClientToScreen 将客户坐标转换为屏幕坐标.

You should also call the ClientToScreen to convert the Client coordinate to screen coordinate.

请注意,ClientToScreen仅接收POINT的参数(而不是RECT),并且您可以找到POINT

Notice that ClientToScreen only receive the parameter of POINT(not a RECT), and you can find the POINT class here.

GetWindowRect的大小将为额外".但是,GetClientRect完全不包含它(以及标题栏,窗口边框等其他信息).

GetWindowRect will get a "extra" size. However, GetClientRect does exactly not include it(and other extra information like title bar, window border, etc).

这篇关于在Windows中使用Java拍摄前台应用程序屏幕快照,无需额外的边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆