gpt4 book ai didi

java - 拍摄完整 JFrame 和仅 Jframe 的快照

转载 作者:行者123 更新时间:2023-11-30 08:11:27 25 4
gpt4 key购买 nike

嘿伙计们,我已经开发了一个代码来截取整个屏幕的屏幕截图,但我希望它只截取 Jframe 内内容的屏幕截图。顺便说一下,我稍后会用它来打印图像。主要问题之一是,鼠标也出现在快照中。我不需要鼠标或底部的两个按钮。我可以更改按钮的visi,但是对于鼠标和仅在Jframe 内拍摄应该做什么?这是我的代码,它截取整个屏幕的屏幕截图。

                try{
Thread.sleep(1000);
Toolkit tk = Toolkit.getDefaultToolkit(); //Toolkit class returns the default toolkit
Dimension d = tk.getScreenSize();

//Dimension class object stores width & height of the toolkit screen
// toolkit.getScreenSize() determines the size of the screen

Rectangle rec = new Rectangle(0, 0, d.width, d.height);
//Creates a Rectangle with screen dimensions,

Robot ro = new Robot(); //to capture the screen image
BufferedImage img = ro.createScreenCapture(rec);



File f;
f = new File("myimage.jpg"); // File class is used to write the above generated buffered image to a file
ImageIO.write(img, "jpg", f);

} catch (Exception ex) {
System.out.println(ex.getMessage());
}

最佳答案

恕我直言,最好制作组件的图像(JFrame也是一个组件):

BufferedImage img = new BufferedImage(yourComponent.getWidth(), yourComponent.getHeight(), BufferedImage.TYPE_INT_RGB);
yourComponent.paint(img.getGraphics());
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);

关于java - 拍摄完整 JFrame 和仅 Jframe 的快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30335787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com