Image to BufferedImage

Discussion in 'Java' started by Martin Thomas, Jun 2, 2004.

  1. Hi,

    simple question and hopefully a simple solution:
    How can i convert an java.awt.Image into a BufferedImage?

    Thanks

    Martin
     
    Martin Thomas, Jun 2, 2004
    #1
    1. Advertising

  2. Martin Thomas

    VisionSet Guest

    "Martin Thomas" <> wrote in message
    news:40bdd448$...
    > Hi,
    >
    > simple question and hopefully a simple solution:
    > How can i convert an java.awt.Image into a BufferedImage?
    >


    Chances are it already is!

    BufferedImage is the single concrete subclass of Image (in current 1.4.2
    API).

    cast it to BufferedImage in a safe manner.

    Other possibility is a VolatileImage from which you can get a BufferedImage
    with getSnapshot().

    --
    Mike W
     
    VisionSet, Jun 2, 2004
    #2
    1. Advertising

  3. Martin Thomas wrote:
    > Hi,
    >
    > simple question and hopefully a simple solution:
    > How can i convert an java.awt.Image into a BufferedImage?


    If you are lucky, a cast will do. If not, if you really have an unknown
    Image subclass, well it's not fun:

    * Get the Image's consumer with Image.getSource().

    * Use a PixelGrabber to read the image data from the consumer into some
    pixel array

    * Create a DataBufferInt from the pixel data

    * Create a WritableRaster using the DataBufferInt

    * Create a BufferedImage using the WritableRaster

    * curse, wait, curse, wait, curse, curse, curse :)


    There might be better ways.

    /Thomas
     
    Thomas Weidenfeller, Jun 2, 2004
    #3
  4. Martin Thomas

    Adam Guest

    > Hi,
    >
    > simple question and hopefully a simple solution:
    > How can i convert an java.awt.Image into a BufferedImage?


    If it is not already a BufferedImage (check with instanceof)
    you can create a new BufferedImage with the same size,
    and bufferedImage.getGraphics().drawImage(image, 0 , 0, null);

    However I don't know what can possibly happen
    if image si not yet fully loaded/decoded.

    Adam
     
    Adam, Jun 3, 2004
    #4
  5. Martin Thomas

    Martin Guest

    Tanks to all of you for your answers, very helpful suggetions out here!
    :)

    Martin
     
    Martin, Jun 3, 2004
    #5
  6. Martin Thomas

    Ex1991

    Joined:
    Mar 24, 2009
    Messages:
    2


    This is great, but how do I do this, I need to resize an image for my HighSchools project and I have tried many methods, but how do you do this?
     
    Ex1991, Mar 24, 2009
    #6
  7. Martin Thomas

    javaNewbie

    Joined:
    Mar 19, 2011
    Messages:
    1

    Thanks. That worked!
     
    javaNewbie, Mar 19, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Szymanski
    Replies:
    0
    Views:
    870
    Peter Szymanski
    Jul 9, 2003
  2. Eel

    Copying BufferedImage

    Eel, Jul 9, 2003, in forum: Java
    Replies:
    2
    Views:
    6,998
  3. Aimee J
    Replies:
    1
    Views:
    4,181
    Knute Johnson
    Jul 18, 2004
  4. MaoXuePeng
    Replies:
    1
    Views:
    1,000
    Andrew Thompson
    Nov 24, 2004
  5. Crono

    Image versus BufferedImage

    Crono, Sep 12, 2006, in forum: Java
    Replies:
    1
    Views:
    2,778
    Knute Johnson
    Sep 12, 2006
Loading...

Share This Page