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();
try {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  JPEGImageEncoderImpl j = new JPEGImageEncoderImpl(out);
catch (Exception e) {
0 Comments  (click to add your comment)
Comment and Contribute






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



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.