How to stretch an image

I want to stretch an image using Graphics, but my code can't here, it shows the image the size I want, but not compressing the image

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(); } } 
+6
java image-processing
source share
2 answers

Call getScaledInstance() to scale the image to the required size before creating the ImageIcon . You do not need to call setMinimumSize on the shortcut.

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

To set the background image from the filter

  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);} 
+1
source share

All Articles