以下是我为了拍摄活动窗口的截图而编写的代码。但是,我不确定为什么图像周围有黑色边框。请帮忙告诉我我的代码是否有任何错误。
Robot robot; //create robot instance
robot = new Robot(); //gets and saves a reference to a new Robot
robot.delay(3000); //delay robot for 3 seconds
//get dimensions of the bounding rectangle of the specified window
WinDef.HWND hwnd = User32.INSTANCE.GetForegroundWindow();
RECT WindowDimensions = new RECT();
//get screen coordinates of upper-left and lower-right corners of the window in dimensionsOfWindow
User32.INSTANCE.GetWindowRect(hwnd, WindowDimensions);
//capture image of only active window
BufferedImage screenFullImage = robot.createScreenCapture(WindowDimensions.toRectangle());
//write buffered image to file
try{
//File f = new File(dir + "\\screenshot " + createTimeStampStr() + ".png"); //file path of output
File f = new File("D:\\screenshot " + createTimeStampStr() + ".png"); //file path of output
ImageIO.write(screenFullImage, "png", f);
}catch(IOException e){
System.out.println("Error: " + e);
}