-1
我想使用BufferedImage()獲取捕獲的截圖(圖像數據)中的JFrame組件的周邊邊框。如何在BufferedImage數據輸出中獲取JFrame組件的默認邊框?
目前我正在這樣做;
import java.awt.Component;
import java.awt.image.BufferedImage;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.io.File;
class getframeImageExample {
public static BufferedImage getImageData(
Component component) {
BufferedImage image = new BufferedImage(
component.getWidth(),
component.getHeight(),
BufferedImage.TYPE_INT_RGB
);
component.printAll(image.getGraphics());
return image;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
final JFrame f = new JFrame("JFrame Border");
f.setUndecorated(false);
f.setLocation(500,300);
f.setSize(560, 420);
JMenuItem screenshot =
new JMenuItem("Take Snapshot");
screenshot.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae) {
BufferedImage imageOutput = getImageData(f.getRootPane());
try {
// write the image as a PNG
ImageIO.write(
imageOutput,
"png",
new File("CapturedImage.png"));
} catch(Exception e) {
e.printStackTrace();
}
}
});
JMenu menu = new JMenu("Menu");
menu.add(screenshot);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
f.setJMenuBar(menuBar);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
請參見附件PNG文件的更多細節。
Original JFrame window; Captured Image
適用於我。什麼OS? – copeg
這不是很明顯的照片嗎?贏7(最壞的情況下Vista; - ])。 –
我正在使用Windows 7 Enterprise(64位)。 – avp