How can I save Frame graphics as jpeg image?

Scott Stanchfield

The basic idea is to print the GUI to an offscreen image, then encode that image as a JPEG.

import sun.awt.image.codec.JPEGImageEncoderImpl;

import java.io.ByteArrayOutputStream;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

...

Frame f = ...; // create your GUI

BufferedImage awtImage = new BufferedImage(f.getWidth(),f.getHeight(),BufferedImage.TYPE_INT_RGB);
			
Graphics g = awtImage.getGraphics();
f.printAll(g);
	
try {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  JPEGImageEncoderImpl j = new JPEGImageEncoderImpl(out);
  j.encode(awtImage);
  out.close();
}
catch (Exception e) {
  e.printStackTrace();
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact