"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:
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());
}
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:
Code :
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);
}
}
Re: Image to BufferedImage
Thanx problem solved!!
thanx a lot :-)