Recently, I used Java to dynamically generate a picture with a transparent background. I selected the png format from gif and png, and automatically added the link address: http://www.my400800.cn to the pictures on the website. The search results are summarized as follows: 1. Generate png image int width = 400; int hei ...
Recently, I used Java to dynamically generate a picture with a transparent background. I selected the png format from gif and png, and automatically added the link address: http://www.my400800.cn to the pictures on the website. The search results are summarized as follows: 1. Generate png image int width = 400; int height = 300; // Create BufferedImage object BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Get Graphics2D Graphics2D g2d = image .createGraphics(); // draw the image g2d.setColor(new Color(255,0,0)); g2d.setStroke(new BasicStroke(1)); g2d.draw //dispose the object g2d.dispose(); //save File ImageIO.write(image, "png", new File("c:/test.png")); int width = 400; int height = 300; // create a BufferedImage object BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Get Graphics2D Graphics2D g2d = image.createGraphics(); // Drawing g2d.setColor(new Color(255,0,0)); g2d.setStroke( new BasicStroke(1)); g2d.draw // release the object g2d.dispose(); // save the file ImageIO.write(image, "png", new File("c:/test.png")); this is just The code for drawing graphics, its background is black, how can the background be transparent? Continue to search and get no results, but I found the following code, it just sets the graphics drawn by itself to be transparent or semi-transparent, and the background is not transparent, as follows: 2. Draw semi-transparent graphics int width = 400; int height = 300; / / Create BufferedImage object BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Get Graphics2D Graphics2D g2d = image.createGraphics(); // set the transparency g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f)); // 1.0f is transparency, the value is from 0-1.0, and it becomes opaque in turn // drawing g2d.setColor(new Color(255,0,0)); g2d.setStroke(new BasicStroke(1)); g2d.draw //Release the object //End of transparency setting g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER) ); g2d.dispose(); // Save the file ImageIO.write(image, "png", new File("c:/test.png")); The graphics drawn in this way should be said to be transparent in the foreground, and the background is still Black, :( I didn't see any useful code on the Internet. On csdn, one person said that he had implemented it, but he didn't say how to implement it. He had no choice but to explore by himself. It took more than half an hour to almost check BufferedImage and All the methods and properties of Graphics2D, finally found a solution, just add two lines of code, as follows: int width = 400; int height = 300; // Create BufferedImage object BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Get Graphics2D Graphics2D g2d = image.createGraphics(); // ---------- Add the following code so that Background transparent ----------------- image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); g2d.dispose(); g2d = image.createGraphics( ); // ---------- end of background transparency code ----------------- // drawing g2d.setColor(new Color(255,0,0 )); g2d.setStroke(new BasicStroke(1)); g2d.draw //Release the object g2d.dispose(); //Save the file ImageIO.write(image, "png", new File("c:/test. png")); |