how to crop image in Java?

Hello,

I'm trying to crop an image which is saved in the hardrive. But when I run the program it gives me the following error message:

java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0

I checked the original image and its width= 1280 and Hight=960. It seems that the problem is when Java try to create the new cropped image!!

The following is the code:

Image image =new ImageIcon(s).getImage();

t=image.getImage();

Toolkit tk = Toolkit.getDefaultToolkit();

ImageProducer source = image.getSource();

ImageFilter extractFilter=new CropImageFilter(0,0,100,100);

Image image2 = tk.createImage(new FilteredImageSource(source,extractFilter));

Any help please.

[858 byte] By [eyadoa] at [2007-11-27 9:34:46]
# 1

You forgot to add a MediaTracker. You get the error when you operate on image which loading wasn't finished.

MediaTracker m = new MediaTracker(this); // 'this' is for example a JPanel, sth that waits for the image, not everything matchs...

Image im= Toolkit.getDefaultToolkit().getImage(string);

m.addImage(im, 0);

try {

tracker.waitForID(0);

} catch (InterruptedException e) {}

Sth like this :-) I had same problem months ago.

plika at 2007-7-12 22:59:48 > top of Java-index,Security,Cryptography...