Main page > Java > Image FAQ

How can I save a GUI component (e.g. Canvas) into a file?

<< Previous question: Determine image file width / height

Next question: Access image pixels >>

You can grab pixels from the screen using java.awt.Robot's createScreenCapture method if you are using Java 1.3 or higher (demo code for screen capturing in Java). However, a cleaner approach is to

See this Sun article for some code. The actual image saving is not covered by that article, refer to the corresponding section in this FAQ. Note that unless a canvas only stores a photo, JPEG is not the appropriate file format to store the image. Use PNG or some other format with lossless compression instead.