posts - 38,  comments - 22,  trackbacks - 0

/**
     * 将某个组件上的图形或组件另存为文件
     * @param comp
     * @param filename
     * @return
     */
    public static boolean saveToJPEG(Component comp) {
        boolean result = true;
        try {
            int w = comp.getWidth();
            int h = comp.getHeight();
            BufferedImage
                image = (BufferedImage) comp.createImage(w, h);
            Graphics2D g2 = image.createGraphics();
            comp.print(g2);

            String filename = LocalAccess.getSaveFileName(comp,"JavaComponent.jpg");
            if(filename==null)return false;
            BufferedOutputStream bos =
                new BufferedOutputStream(new FileOutputStream(filename));
            com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(bos);
            com.sun.image.codec.jpeg.JPEGEncodeParam jep = encoder.getDefaultJPEGEncodeParam(image);
            jep.setQuality(1.0f, false);
            encoder.setJPEGEncodeParam(jep);
            encoder.encode(image);
            bos.close();
        }
        catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

posted on 2007-01-15 13:51 aaabbb 阅读(250) 评论(0)  编辑  收藏 所属分类: Swing

只有注册用户登录后才能发表评论。


网站导航: