Java 如何生成透明背景色的图片
PHP中文网
PHP中文网 2017-04-17 15:55:30
0
1
408

将一个背景色是透明的图片改变宽和高之后,重新生成 Image 对象,并用 Graphics2D 重新绘制后,透明的背景色变成了白色,这是怎么回事? 我也并没有调用 setColor()方法设置背景色。
主要代码如下:

data = bos.toByteArray();     //bos:原图片的输入流
ImageIcon ii = new ImageIcon(data);
Image i = ii.getImage();

int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);

Image resizedImage = null;
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth,(newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);
}
Image temp = new ImageIcon(resizedImage).getImage();

BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),BufferedImage.TYPE_INT_RGB);

Graphics2D g = bufferedImage.createGraphics();

g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
PHP中文网
PHP中文网

认证0级讲师

reply all(1)
左手右手慢动作

It should add a color to the new background by default! http://snkcxy.iteye.com/blog/1872229 Take a look at this link and see if it inspires you

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!