how to download and save transparent png file

A

Alex

Hi all,

I'm trying to download and to save a transparent png file.
I succeed to download and save it, but in losing the transparent
background!

My source code is:
String url = "http://..../test.png";
URLConnection connection = new URL(url).openConnection();
Image image =
Toolkit.getDefaultToolkit().createImage((ImageProducer)connection.getContent());
ImageIO.write(toBufferedImage(image), extension, new
File(filename).getCanonicalFile());

with the function:

private BufferedImage toBufferedImage(Image image) {
if( image instanceof BufferedImage ) {
return ((BufferedImage)image);
} else {
image = new ImageIcon(image).getImage();
BufferedImage bufferedImage = new
BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB );
Graphics g = bufferedImage.createGraphics();
g.drawImage(image,0,0,null);
g.dispose();
return (bufferedImage);
}
}


If someone have an idea for me?

(Is it possible to download and to save the getcontent() directly to a
file without transform it into Image?)

Thanks
 
S

Steve W. Jackson

Alex said:
Hi all,

I'm trying to download and to save a transparent png file.
I succeed to download and save it, but in losing the transparent
background!

My source code is:
String url = "http://..../test.png";
URLConnection connection = new URL(url).openConnection();
Image image =
Toolkit.getDefaultToolkit().createImage((ImageProducer)connection.getContent()
);
ImageIO.write(toBufferedImage(image), extension, new
File(filename).getCanonicalFile());

with the function:

private BufferedImage toBufferedImage(Image image) {
if( image instanceof BufferedImage ) {
return ((BufferedImage)image);
} else {
image = new ImageIcon(image).getImage();
BufferedImage bufferedImage = new
BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB );
Graphics g = bufferedImage.createGraphics();
g.drawImage(image,0,0,null);
g.dispose();
return (bufferedImage);
}
}


If someone have an idea for me?

(Is it possible to download and to save the getcontent() directly to a
file without transform it into Image?)

Thanks

Since URLConnection includes a getInputStream method that returns an
input stream for reading from the connection, it seems logical that you
might test whether you can simply read byte-by-byte and then just write
the result to a local file via an output stream.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top