Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Write PNG from MemoryImageSource

Thread view: 
Simon Andrews - 09 Feb 2006 13:08 GMT
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 - 09 Feb 2006 13:25 GMT
> I have an application which dynamically generates images using
> MemoryImageSource to produce a java.awt.Image object.  I'd like to be
[quoted text clipped - 5 lines]
>
> 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;
}
Stefan Schulz - 09 Feb 2006 17:39 GMT
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 - 13 Feb 2006 09:48 GMT
> 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 - 09 Feb 2006 13:41 GMT
On Thu, 09 Feb 2006 13:08:56 +0000, Simon Andrews
<simon.andrews@bbsrc.ac.uk> 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.
Signature

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

Simon Andrews - 13 Feb 2006 09:42 GMT
> On Thu, 09 Feb 2006 13:08:56 +0000, Simon Andrews
> <simon.andrews@bbsrc.ac.uk> wrote, quoted or indirectly quoted someone
[quoted text clipped - 7 lines]
>
> If you are lucky, you have a BufferedImage just a cast away.

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

Simon.
Roedy Green - 13 Feb 2006 10:24 GMT
On Mon, 13 Feb 2006 09:42:38 +0000, Simon Andrews
<simon.andrews@bbsrc.ac.uk> 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?

Signature

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

Roedy Green - 13 Feb 2006 10:25 GMT
On Mon, 13 Feb 2006 10:24:23 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> 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
Signature

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

Simon Andrews - 14 Feb 2006 12:40 GMT
> On Mon, 13 Feb 2006 10:24:23 GMT, Roedy Green
> <my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
[quoted text clipped - 4 lines]
>>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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.