// Your browser instance.
org.cef.browser.CefBrowser browser = ...
// Obtain the component that you want to capture in a screenshot.
java.awt.Component component = browser.getUIComponent();
// Determine what area of the entire screen is covered by the component.
java.awt.Point p = new java.awt.Point(0, 0);
javax.swing.SwingUtilities.convertPointToScreen(p, component);
java.awt.Rectangle region = component.getBounds();
region.x = p.x;
region.y = p.y;
// Store the selected area from the screen in a image buffer.
java.awt.image.BufferedImage image = new java.awt.Robot().createScreenCapture( region );
# 1 楼答案
据我所知,CefBrowser是基于AWT的。要创建此类组件的屏幕截图,您可以(必须?)创建整个屏幕的捕获,仅限于组件覆盖的区域
像这样的方法会奏效:
要将“保存到缓冲区”保存到文件中,请创建一个
File
实例(如果希望向用户呈现一个漂亮的“另存为”对话框,请使用JFileChooser
),然后使用javax.imageio.ImageIO#write(RenderedImage, String, File)
将图像存储到文件中。第二个参数指的是要使用的文件格式(png、bmp等)如果有人能提供一个直接存储组件的代码示例,而不将其捕获为大屏幕的一部分(这也将捕获您感兴趣的其他组件),我将非常感兴趣