我用BufferedImage读入一张GIF图片,该图片没有底色,然后我g2d.drawImage,再保存为png,发现一个问题 我用BufferedImage读入一张GIF图片,该图片没有底色,然后我g2d.drawImage,再保存为png,发现一个问题即java自动把生成的png图片变成了底色为黑,我希望生成的底色是“无”该怎么办? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 问题可能初在drawImage或保存的地方,看看是不是存在默认参数背景为黑的情况 试试看这种方法读你的gif图象 import java.applet.*;import java.awt.*;import java.awt.event.*;public class Example22_5 extends Applet implements Runnable{final int number=10;int count=1;Thread mythread;Image[] pic=new Image[number];public void init() {for(int i=1;i<number;i++) {pic[i]=getImage(getCodeBase(),"T"+i+".gif");} } public void start() {mythread=new Thread(this); mythread.start(); } public void stop() {mythread=null; } public void run() {while(true) {if(count>10) count=1; repaint(); count++; try{mythread.sleep(150);} catch(InterruptedException e){} } } public void paint(Graphics g) {if((pic[count])!=null) g.drawImage(pic[count],10,10,pic[count].getWidth(this),pic[count].getHeight(this),this); }} 我的代码主要就是这样ChartGraphics chartGraphics = new ChartGraphics();String gif_file = "test.gif"; //该图片没有底色String outfile = "test.png"; //要保存的图片文件名File f1 = new File(gif_file);BufferedImage read_image = ImageIO.read(f1);int width = read_image.getWidth();int height = read_image.getHeight();chartGraphics.image = new BufferedImage(width-1, height, BufferedImage.TYPE_INT_RGB);Graphics2D g2d = (Graphics2D) chartGraphics.image.getGraphics();g2d.drawImage(read_image, 0, 0, null);chartGraphics.createImage(outfile); 改变BufferedImage的颜色模型为索引色。 使用BufferedImage.TYPE_INT_ARGBchartGraphics.image = new BufferedImage(width-1, height, BufferedImage.TYPE_INT_ARGB); 关于一个类型转换的问题 求教! 列名 'kshj' 无效 请教一个SQL的除法过程----在线等 MVC怎么样去理解,唉每一次知道MVC影响着程序编程的质量。 JTree的节点可以存储一些额外的数据吗? 怎么用java登陆一个有用户名和密码的网页? 请问哪里有sdk1.4下载,除了sun的网站外,太大了100多M. scjp求助,这道题应该选什么 怎样防止JAVA应用程序重复启动? 急!!高人帮忙呀!!!一个关于获取JPanel尺寸的问题!!!抢分呀!! 有没有做SWT/JFace,认识一下。 有谁用java开发过类似组态软件的应用软件?不知道用java开发难度大吗?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example22_5 extends Applet implements Runnable
{final int number=10;int count=1;
Thread mythread;
Image[] pic=new Image[number];
public void init()
{for(int i=1;i<number;i++)
{pic[i]=getImage(getCodeBase(),"T"+i+".gif");}
}
public void start()
{mythread=new Thread(this);
mythread.start();
}
public void stop()
{mythread=null;
}
public void run()
{while(true)
{if(count>10)
count=1;
repaint();
count++;
try{mythread.sleep(150);}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{if((pic[count])!=null)
g.drawImage(pic[count],10,10,pic[count].getWidth(this),pic[count].getHeight(this),this);
}
}
ChartGraphics chartGraphics = new ChartGraphics();
String gif_file = "test.gif"; //该图片没有底色
String outfile = "test.png"; //要保存的图片文件名
File f1 = new File(gif_file);
BufferedImage read_image = ImageIO.read(f1);
int width = read_image.getWidth();
int height = read_image.getHeight();
chartGraphics.image = new BufferedImage(width-1, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) chartGraphics.image.getGraphics();
g2d.drawImage(read_image, 0, 0, null);
chartGraphics.createImage(outfile);
BufferedImage.TYPE_INT_ARGBchartGraphics.image = new BufferedImage(width-1, height, BufferedImage.TYPE_INT_ARGB);