Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: "java.lang.IllegalArgumentException" error while converting an Image to Buffered Image

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "java.lang.IllegalArgumentException" error while converting an Image to Buffered Image

    Hello Everyone...

    i am trying to convert an Image to BufferedImage
    the image is created from a url (this step works just fine)
    and then trying to convert the image to bufferedimage

    for converting Image to BufferedImage i followed the code from this site:
    Convert java.awt.image.BufferedImage to java.awt.Image : Image2D Graphics GUIJava

    but when i try to convert the image to BufferedImage i get:
    java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0

    thanx alot for your time and help!

    here is my code:

           URL url = null;
            BufferedImage img = null;
            Image ximg = null;
            Graphics g = null;
     
            try
            {
              url = new URL("http://www.smileyicon.net/smiley-icon-5.jpg");
     
     
            }
            catch(Exception e)
            {
               JOptionPane.showMessageDialog(null, e.toString());
            }
            ximg    =  java.awt.Toolkit.getDefaultToolkit().createImage(url);
            try
            {
             img = new BufferedImage(ximg.getWidth(null),ximg.getHeight(null),BufferedImage.TYPE_INT_ARGB);
             g = img.getGraphics();
             g.drawImage(ximg, 0, 0, null);
            }
            catch(Exception e)
            {
     
                JOptionPane.showMessageDialog(null, e.toString());
     
            }
    Last edited by Deep_4; November 8th, 2012 at 01:17 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Image to BufferedImage

    Hello TheLaz.

    Welcome to the Java Programming Forums.

    I tried playing with your example and the code from that site but could not get it to work.
    You are looking to convert an image to bufferedimage but that tutorial is to convert bufferedimage to image.

    I found another example. I'm not sure if it works but it doesn't error. Try this:

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.awt.image.PixelGrabber;
    import javax.swing.ImageIcon;
     
    public class Pictures {
     
        public static BufferedImage toBufferedImage(Image image) {
     
            if (image instanceof BufferedImage) {
                return (BufferedImage) image;
            }
     
            // This code ensures that all the pixels in the image are loaded
            image = new ImageIcon(image).getImage();
     
            // Determine if the image has transparent pixels
            boolean hasAlpha = hasAlpha(image);
     
            // Create a buffered image with a format that's compatible with the
            // screen
            BufferedImage bimage = null;
            GraphicsEnvironment ge = GraphicsEnvironment
                    .getLocalGraphicsEnvironment();
            try {
                // Determine the type of transparency of the new buffered image
                int transparency = Transparency.OPAQUE;
                if (hasAlpha == true) {
                    transparency = Transparency.BITMASK;
                }
     
                // Create the buffered image
                GraphicsDevice gs = ge.getDefaultScreenDevice();
                GraphicsConfiguration gc = gs.getDefaultConfiguration();
                bimage = gc.createCompatibleImage(image.getWidth(null), image
                        .getHeight(null), transparency);
            } catch (HeadlessException e) {
            } // No screen
     
            if (bimage == null) {
                // Create a buffered image using the default color model
                int type = BufferedImage.TYPE_INT_RGB;
                if (hasAlpha == true) {
                    type = BufferedImage.TYPE_INT_ARGB;
                }
                bimage = new BufferedImage(image.getWidth(null), image
                        .getHeight(null), type);
            }
     
            // Copy image to buffered image
            Graphics g = bimage.createGraphics();
     
            // Paint the image onto the buffered image
            g.drawImage(image, 0, 0, null);
            g.dispose();
     
            return bimage;
        }
     
        public static boolean hasAlpha(Image image) {
            // If buffered image, the color model is readily available
            if (image instanceof BufferedImage) {
                return ((BufferedImage) image).getColorModel().hasAlpha();
            }
     
            // Use a pixel grabber to retrieve the image's color model;
            // grabbing a single pixel is usually sufficient
            PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
            try {
                pg.grabPixels();
            } catch (InterruptedException e) {
            }
     
            // Get the image's color model
            return pg.getColorModel().hasAlpha();
        }
     
     
        public static void main(String[] args) {
     
            String myImage = "smiley-icon-5.jpg";
            Image image = java.awt.Toolkit.getDefaultToolkit().createImage(myImage);
     
            Pictures p = new Pictures();
            p.toBufferedImage(image);
     
        }    
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image to BufferedImage

    Thanx problem solved!!

    thanx a lot :-)

Similar Threads

  1. GUI problem with image in java
    By Koâk in forum AWT / Java Swing
    Replies: 6
    Last Post: May 17th, 2009, 04:17 AM
  2. How images stores in awt.Image class?
    By BharatT in forum AWT / Java Swing
    Replies: 0
    Last Post: February 24th, 2009, 05:10 AM
  3. How to read character from image area(jpg image)?
    By sundarjothi in forum Java Theory & Questions
    Replies: 5
    Last Post: August 6th, 2008, 02:08 AM
  4. How to import an .tiff image in JSP?
    By jazz2k8 in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: May 12th, 2008, 05:55 AM