想在用户上传图片的时候 将这张图片按一定比例缩小后读到内存中  然后在JLable 上面画出来 ,不希望在做缩略的时候生成一张冗余图片  我对swing的画图机制不怎么了解  请教一下具体怎么画   public void resize(int w, int h) throws IOException {
    BufferedImage _image = new BufferedImage(w, h,
                                             BufferedImage.TYPE_INT_RGB);
    _image.getGraphics().drawImage(img, 0, 0, w, h, null); //绘制缩小后的图
    if((!(this.destFilePath==null))&&!"".equals(destFilePath))
    {
    FileOutputStream out = new FileOutputStream(this.destFilePath+destFile); //输出到文件流
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(_image); //近JPEG编码
    out.close();
    }
    else
    {
     throw new IOException("必须设置要保存的目标文件的路径");
    }
  }这个是图片缩略 方法

解决方案 »

  1.   

    Graphics2D.drawRenderedImage(BufferedImage, AffineTransform);
    AffineTransform.getScaleInstance(0.5f, 0.5f),长宽都是0.5比例
      

  2.   

    已解决  
    final JLabel label = new JLabel(){
    public void paintComponent(Graphics g) {
    File file = new File(picture.getFilePath());
    Image img = null;
    try {
    img = javax.imageio.ImageIO.read(file);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    //ImageIcon icon = new ImageIcon("images/direct1.jpg");
    // 图片随窗体大小而变化
    g.drawImage(img, 0, 0, this.getSize().width, this
    .getSize().height, this);
    }
    };谢谢楼上的  
      

  3.   

    可以迭代,每次检查文件大小,如果>500K,缩放0.8,再检查,直到<=500K