Well, let's see. You don't close the ImageOutputStream; not closing a file is always a bad thing. You have empty catch blocks, so that errors will go unreported. And you use a few variables (bw, bh) which aren't defined anywhere.
But I've never used the imageio API, and I saw this as a good opportunity to learn. So I took your code, wrapped it in a minmal class, changed bw and bh to iw and ih, respectively, added the "close" call, put some printStackTraces() into the catch blocks, and tried it. On my first few attempts, I got corrupted PNG files, until I realized I was killing the program before the whole file was written out. I put in a print statement to report when the file writing was done, after the file was closed. I then found that this worked exactly as you'd hope, drawing a white diagonal line across the image.
So I think you're very close; just take care of these details, and it should work for you too.
hi .. thnaks it worked. but one more probkem the original file size is 122 kb. but the png file format is 0.98 mb .. huge file. carnt be sent to a mobile phone. is is due to my code, or is png a heavy format. thanks
author and iconoclast
PNG has lossless compression, while JPEG has lossy compression. In general, a PNG of a given photographic image will always be larger -- often much larger -- than a JPEG.
Joined: Jan 14, 2005
i have to questions: 1) the code above has to run on a web server. is it possible. ( i am confused about the this reference for the JFrame, in the media tracker and image height and width functions).or can i create a class derived from from jcomponent, and invoke that class's object from within my servlet passing the image path to be drawn on. will that work.
2) the picture is then sent to a mobile phone. which can only read png files. so how can i compress that png (or carnt i) . sorry for bothering u .