Write PNG from MemoryImageSource  
Author Message
Simon Andrews





PostPosted: 2006-2-9 21:09:00 Top

java-programmer, Write PNG from MemoryImageSource 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.
 
Simon Andrews





PostPosted: 2006-2-9 21:26:00 Top

java-programmer >> Write PNG from MemoryImageSource Simon Andrews wrote:
> 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;
}
 
Roedy Green





PostPosted: 2006-2-9 21:41:00 Top

java-programmer >> Write PNG from MemoryImageSource On Thu, 09 Feb 2006 13:08:56 +0000, Simon Andrews
<email***@***.com> wrote, quoted or indirectly quoted someone
who said :

>
>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
Stefan Schulz





PostPosted: 2006-2-10 1:39:00 Top

java-programmer >> Write PNG from MemoryImageSource 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.

 
 
Simon Andrews





PostPosted: 2006-2-13 17:43:00 Top

java-programmer >> Write PNG from MemoryImageSource Roedy Green wrote:
> On Thu, 09 Feb 2006 13:08:56 +0000, Simon Andrews
> <email***@***.com> wrote, quoted or indirectly quoted someone
> who said :
>
>
>>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.

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

Simon.
 
 
Simon Andrews





PostPosted: 2006-2-13 17:48:00 Top

java-programmer >> Write PNG from MemoryImageSource Stefan Schulz wrote:
> 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.

 
 
Roedy Green





PostPosted: 2006-2-13 18:24:00 Top

java-programmer >> Write PNG from MemoryImageSource On Mon, 13 Feb 2006 09:42:38 +0000, Simon Andrews
<email***@***.com> wrote, quoted or indirectly quoted someone
who said :

>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?

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
Roedy Green





PostPosted: 2006-2-13 18:26:00 Top

java-programmer >> Write PNG from MemoryImageSource On Mon, 13 Feb 2006 10:24:23 GMT, Roedy Green
<email***@***.com> wrote, quoted or
indirectly quoted someone who said :

>>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?

note the distinction between .class and .getClass(). You want
getClass() to discover the run-time, true type of the Object.


see http://mindprod.com/jgloss/classforname.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
Simon Andrews





PostPosted: 2006-2-14 20:40:00 Top

java-programmer >> Write PNG from MemoryImageSource Roedy Green wrote:
> On Mon, 13 Feb 2006 10:24:23 GMT, Roedy Green
> <email***@***.com> wrote, quoted or
> indirectly quoted someone who said :
>
>
>>>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?

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.