Hi Eduardo,


This source sample illustrates saving a JLabel icon to a jpeg file :
    
    
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;

public class saveLabelAsJPEG {

    static void main(String[] args) {
        try {
            JLabel label = new JLabel(new ImageIcon("foo.gif"));
            Image image = ((ImageIcon) label.getIcon()).getImage();
            int w = image.getWidth(null);
            int h = image.getHeight(null);
            BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_
RGB);
            bimg.createGraphics().drawImage(image, 0, 0, null);
            FileOutputStream out = new FileOutputStream(new File("foo.jpg"));
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimg);
            param.setQuality(1.0f, false);
            encoder.setJPEGEncodeParam(param);
            encoder.encode(bimg);
        } catch (Exception ex) { ex.printStackTrace(); }
    }
}


Other Java2D samples can be found at :
        
http://java.sun.com/products/java-media/2D/samples/index.html


Brian



> X-Authentication-Warning: capra.eng.sun.com: amith set sender to 
[EMAIL PROTECTED] using -f
> Date: Tue, 8 Jun 1999 15:21:16 -0300 (EST)
> From: Eduardo da Fonseca Melo <[EMAIL PROTECTED]>
> X-Sender: efm@paulista
> To: [EMAIL PROTECTED]
> Subject: [java2d] Saving an image to a jpeg file
> MIME-Version: 1.0
> 
> 
> Greetings everyone.
> 
> I want to save an image that appears in an JLabel component to a gif/jpeg
> file. Does anyone know a way to implement this?
> 
> Any suggestion would be appreciated greatly.
> 
> Eduardo Melo.
> 
> =====================================================================
> To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
> Java 2D Home Page: http://java.sun.com/products/java-media/2D/

=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 2D Home Page: http://java.sun.com/products/java-media/2D/

Reply via email to