我用BufferedImage读入一张GIF图片,该图片没有底色,然后我g2d.drawImage,再保存为png,发现一个问题
即java自动把生成的png图片变成了底色为黑,我希望生成的底色是“无”该怎么办?

解决方案 »

  1.   

    问题可能初在drawImage或保存的地方,看看是不是存在默认参数背景为黑的情况
      

  2.   

    试试看这种方法读你的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);
    }
    }
      

  3.   

    我的代码主要就是这样
    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);
      

  4.   

    改变BufferedImage的颜色模型为索引色。
      

  5.   

    使用
    BufferedImage.TYPE_INT_ARGBchartGraphics.image = new BufferedImage(width-1, height, BufferedImage.TYPE_INT_ARGB);