捕获没有边框的屏幕截图

时间:2014-10-15 23:11:58

标签: java swing screenshot

我正在寻找我的程序中当前正在发生的事情的屏幕截图,以便在暂停菜​​单的背景中使用。我找到了代码:

BufferedImage x = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

它确实采取截图并且效果很好,事情是它还包括我的窗口边框,绝对不应该包括在内。我已经在网络上偷窥了,我还没有看到一个功能,只能拍摄边框内的屏幕截图。或者,找到顶部,底部和侧面边框的厚度也可以,但我没有找到任何相关信息。

最好的办法是什么?

2 个答案:

答案 0 :(得分:2)

您应该尝试捕获内容,而不是捕获帧边界,例如......

JRootPane rootPane = frame.getRootPane();
Rectangle bounds = new Rectangle(rootPane.getSize());
bounds.setLocation(rootPane.getLocationOnScreen());
BufferedImage contentsImage = bot.createScreenCapture(bounds);

这也将捕获菜单栏,如果您只需要物理内容,则应使用frame.getContentPane()代替frame.getRootPane()

例如......

原始框架Original

捕获结果(完整框架/根窗格)
Capture

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ScreenShot {

    public static void main(String[] args) {
        new ScreenShot();
    }

    public ScreenShot() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());

                JButton capture = new JButton("Snap shot");
                capture.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            Robot bot = new Robot();
                            BufferedImage frameImage = bot.createScreenCapture(frame.getBounds());
                            JRootPane rootPane = frame.getRootPane();
                            Rectangle bounds = new Rectangle(rootPane.getSize());
                            bounds.setLocation(rootPane.getLocationOnScreen());
                            BufferedImage contentsImage = bot.createScreenCapture(bounds);

                            JPanel panel = new JPanel(new GridLayout(1, 2));
                            panel.add(new JLabel(new ImageIcon(frameImage)));
                            panel.add(new JLabel(new ImageIcon(contentsImage)));

                            JOptionPane.showMessageDialog(frame, panel);

                        } catch (AWTException ex) {
                            ex.printStackTrace();
                        }
                    }
                });

                frame.add(capture, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            setLayout(new BorderLayout());

            try {
                BufferedImage img = ImageIO.read(new File("C:\\Users\\shane\\Dropbox\\Ponies\\sillydash-small.png"));
                JLabel label = new JLabel(new ImageIcon(img));
                add(label);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }

    }

}

奇怪的是,您可以将此方法用于您想要的任何组件......

答案 1 :(得分:0)

您可以尝试制作自己的功能:

public static final void makeScreenshot(JFrame f) {
    Rectangle rec = f.getBounds();
    BufferedImage bufferedImage = new BufferedImage(rec.width, rec.height,
            BufferedImage.TYPE_INT_ARGB);
    f.paint(bufferedImage.getGraphics());

    try {
        // Create temp file
        File temp = File.createTempFile("screenshot", ".png");

        ImageIO.write(bufferedImage, "png", temp);

        // Delete temp file on exit
        temp.deleteOnExit();
    } catch (IOException ioe) {
        LOGGER.debug(ioe.toString());
    } 
} 

这不包括框架本身或标题栏,因此它应该适合您的需要。

修改 编辑包括保存镜头。