云服务器

商务合作:179001057@qq.com

[原创]截屏,不含windows任务条

xiaoxiao2021-11-25  14


某平台价值19860元的编程课程资料免费领取【点我领取】


参考了网上的一些资料。 package test;import java.awt.AWTException;import java.awt.Dimension;import java.awt.GraphicsConfiguration;import java.awt.GraphicsEnvironment;import java.awt.Insets;import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;/** * 截屏,不含任务条,暂无实用效果,以后改进。 * @author liwenliang */public class TestImage { public static void main(String[] args) { testImage(); } public static void testImage() { try { // 获取屏幕的大小 Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension screenSize = toolkit.getScreenSize(); // 获取屏幕特征 GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); // 获得屏幕的 insets Insets screenInsets = toolkit.getScreenInsets(gc); Rectangle desktopBounds = new Rectangle( screenInsets.left, screenInsets.top, screenSize.width - screenInsets.left - screenInsets.right, screenSize.height - screenInsets.top - screenInsets.bottom); BufferedImage bImage = new Robot().createScreenCapture( new Rectangle(desktopBounds)); // 文件保存地址,jpg格式也可以,其他还没验证 String name = "e:\\image.png"; File f = new File(name); ImageIO.write(bImage, "png", f); System.out.println("copy screen ok.........."); } catch (AWTException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}