【轉】關於Java生成背景透明的png圖片
阿新 • • 發佈:2019-02-14
int width =400;
int height =300;
// 建立BufferedImage物件BufferedImage image =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 獲取Graphics2DGraphics2D g2d = image.createGraphics();
// ---------- 增加下面的程式碼使得背景透明 -----------------image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
// ---------- 背景透明程式碼結束 -----------------
// 畫圖g2d.setColor(new Color(255,0,0));
g2d.setStroke(new BasicStroke(1));
g2d.draw
//釋放物件g2d.dispose();
// 儲存檔案 ImageIO.write(image, "png", new File("c:/test.png"));
int height =300;
// 建立BufferedImage物件BufferedImage image =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 獲取Graphics2DGraphics2D g2d = image.createGraphics();
// ---------- 增加下面的程式碼使得背景透明 -----------------image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2d.dispose();
g2d
// ---------- 背景透明程式碼結束 -----------------
// 畫圖g2d.setColor(new Color(255,0,0));
g2d.setStroke(new BasicStroke(1));
g2d.draw
//釋放物件g2d.dispose();
// 儲存檔案 ImageIO.write(image, "png", new File("c:/test.png"));