如何拉伸图像

时间:2011-01-19 08:30:49

标签: java image-processing

我想通过使用图形来拉伸图像但是这里不能使用我的代码它显示我想要的图像但不能拉伸图像

void imageload () {
    FileDialog fd = new FileDialog(MainFram.this,"Open", FileDialog.LOAD);
    fd.show();
    if(fd.getFile() == null){
        //Label1.setText("You have not chosen any image files yet");
    }else{
        String d = (fd.getDirectory() + fd.getFile());
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image1 = toolkit.getImage(d);
        saveImage = d;//if user want to save Image
        ImageIcon icon=new ImageIcon(Image1);
        lblImage.setIcon(icon);
        lblImage.setMinimumSize(new Dimension(50, 70));
        lblImage.repaint();
    }
}

2 个答案:

答案 0 :(得分:7)

在创建ImageIcon之前,请致电getScaledInstance()将图片缩放到所需尺寸。您无需在标签上拨打setMinimumSize

Image image = toolkit.getImage("pic.jpg");
Image scaledImage = image.getScaledInstance(50, 70, Image.SCALE_DEFAULT);   
ImageIcon icon=new ImageIcon(scaledImage);

答案 1 :(得分:1)

设置来自filchooser的背景图片

  final JFileChooser fc = new JFileChooser();
  int r = fc.showOpenDialog(this);


  fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

  if (r == JFileChooser.APPROVE_OPTION) {
  String name = fc.getSelectedFile().getAbsolutePath();

          JOptionPane.showMessageDialog(null,"ADDED successfully");
          Toolkit toolkit = Toolkit.getDefaultToolkit();
          Image image = toolkit.getImage(name);
          Image scaledImage = image.getScaledInstance(1366, 768, Image.SCALE_DEFAULT);
          ImageIcon icon=new ImageIcon(scaledImage);
          my.jLabel10.setIcon(icon);}