Write PNG from MemoryImageSource

S

Simon Andrews

I have an application which dynamically generates images using
MemoryImageSource to produce a java.awt.Image object. I'd like to be
able to write these images out as pngs.

I've found the javax.imageio class which will write out pngs, but only
from a BufferedImage and not from a basic Image. I've not been able to
find a way to create a BufferedImage from an Image.

Can anyone suggest how I can write out these images?

Cheers

Simon.
 
S

Simon Andrews

Simon said:
I have an application which dynamically generates images using
MemoryImageSource to produce a java.awt.Image object. I'd like to be
able to write these images out as pngs.

I've found the javax.imageio class which will write out pngs, but only
from a BufferedImage and not from a basic Image. I've not been able to
find a way to create a BufferedImage from an Image.

Can anyone suggest how I can write out these images?

Sorry to reply to myself but I've got an answer which works. The code
below puts the raw image into a Frame, scales the original image in it
and captures the output. It appears that the Image produced is actually
a BufferedImage and so can be written out.

I'd still like to know if there's an easier way that this (which seems a
trifle contrived).

private static Image createWritableImage(Image img)
{
int width = img.getWidth(null);
int height = img.getHeight(null);
if (width == -1 || height == -1)
{
return null;
}

Frame f = new Frame();
f.addNotify();

// Returning offscreen produces an unclear background.
Image offscreen = f.createImage( width,height);
Graphics g = offscreen.getGraphics();

g.drawImage(img.getScaledInstance(width,height,Image.SCALE_DEFAULT),0,0,
null);

// Clean up

g.dispose();

f.removeNotify();
return offscreen;
}
 
R

Roedy Green

I've found the javax.imageio class which will write out pngs, but only
from a BufferedImage and not from a basic Image.

try System.out.println( image.getClass() );

If you are lucky, you have a BufferedImage just a cast away.
 
S

Stefan Schulz

You need not even create a Frame, you can create the image simply by
using the BufferedImage constructor, and then using its getGraphics()
method, drawing, and then returning the buffered variant.
 
S

Simon Andrews

Roedy said:
try System.out.println( image.getClass() );

If you are lucky, you have a BufferedImage just a cast away.

Tried that - no dice. It really is just an Image.

Simon.
 
S

Simon Andrews

Stefan said:
You need not even create a Frame, you can create the image simply by
using the BufferedImage constructor, and then using its getGraphics()
method, drawing, and then returning the buffered variant.

That's much cleaner (and makes sense)!

I've now reduced my export down to:


BufferedImage b = new BufferedImage(600,600,BufferedImage.TYPE_INT_RGB);
Graphics g = b.getGraphics();

g.drawImage(mainPanel.getImage().getScaledInstance(600,600,Image.SCALE_DEFAULT),0,0,null);
ImageIO.write(b,"PNG",chooser.getSelectedFile());


Cheers

Simon.
 
R

Roedy Green

Tried that - no dice. It really is just an Image.

How could it be? Image is an abstract class. The actual Image Object
has to be some concrete class doesn't it?
 
S

Simon Andrews

Sorry, what I should have said was that I'd tried a direct cast to
BufferedImage and got a ClassCastException.

It actually appears to be a sun.awt.image.ToolkitImage object.

Simon.
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top