Tuesday, August 13, 2013

Take a screen shot in java

package e_n_d;//name of the package
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
//import com.sun.media.sound.Toolkit;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author ISURU
 */
public class shot extends javax.swing.JFrame {

    public shot() {
        initComponents();
    }

    public Image IsurusImage(Image img, int w,int h){
        BufferedImage resizedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImage.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(img,0,0,w,h,null);
        g2.dispose();
        return resizedImage;
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
        try {
                Rectangle s = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
                BufferedImage c = new Robot().createScreenCapture(s);
                //IsurusImage(c,jLabel1.getWidth(),jLabel1.getHeight()));
                ImageIcon ICON = new ImageIcon(c);
                jLabel1.setIcon(ICON);
                ImageIO.write(c, "png", new File("C:\\Users\\ISURU\\Desktop\\ashot.png") );
     
        } catch (Exception ex) {
            JOptionPane.showMessageDialog( null,ex);
        }
    }                                      

 
    // Variables declaration - do not modify                  
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                
}

No comments:

Post a Comment