public static void fun1() throws IOException { String path = "C:\\Users"; File imgFile = new File( path + "\\img.png"); File imgFile2 = new File(path + "\\img2.png"); BufferedImage srcimg = ImageIO.read(imgFile); Graphics2D g2d = srcimg.createGraphics(); g2d.drawImage(srcimg, 0, 0, null); Stroke dash = new BasicStroke(2.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3.5f, new float[]{15, 10,}, 0f); //虚线 g2d.setStroke(dash); g2d.setColor(Color.RED); g2d.drawRect(56, 64, 80, 80); g2d.dispose(); ImageIO.write(srcimg, "JPG", imgFile2); }