Java中将组件保存成图片

import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
public void saveComponentToJPEG(Component c,String fileName ) {
      try {
          int w=c.getWidth();
          int h=c.getHeight();
          BufferedImage image=(BufferedImage)c.createImage(w,h);
          Graphics2D g2=image.createGraphics();
          c.print(g2);
          BufferedOutputStream bos =
            new BufferedOutputStream( new FileOutputStream( fileName ));
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( bos );
          JPEGEncodeParam jep = encoder.getDefaultJPEGEncodeParam(image);
          jep.setQuality( 1.0f, false );
          encoder.setJPEGEncodeParam( jep );
          encoder.encode( image );
          bos.close();
      }
      catch ( Exception e ) {
          e.printStackTrace();
      }
  }

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页