int width = 400;
int height = 300;
// 創建BufferedImage對象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 獲取Graphics2D
Graphics2D 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"));
20110827
利用Graphics 生成函透明背景的Image
一般的情況下Image雖然會保有透明資訊,但是經過Graphics.drawImage得到Image卻會使來源Image失去透明資訊,透過以下方法可以保留來源圖的透明資訊。這樣的好處是可以把此Image當成其他Graphics的來原圖
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言