【问题标题】:take snapshot of full JFrame and Jframe only [duplicate]仅拍摄完整 JFrame 和 Jframe 的快照 [重复]
【发布时间】:2015-07-31 21:08:42
【问题描述】:

大家好,我已经开发了一个代码来截取整个屏幕的屏幕截图,但我希望它只截取我的 Jframe 中的内容。顺便说一句,稍后我会用它来打印图像。主要问题之一是,鼠标也进入快照内部。我不想要鼠标或底部的两个按钮。我可以更改按钮的可见性,但是对于鼠标和仅在 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());
            }

【问题讨论】:

    标签: java swing awt awtrobot


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢!顺便说一句,这是一个更清晰和更好的答案,所以这个不应该是重复的:|谢谢本!
    猜你喜欢
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    相关资源
    最近更新 更多