I had faced it while i was trying to add some image to desktop Applications.
and found many ways by buffered image and loading images itself , here is an easy-way to handle it
1st option:
by load the image and re-size it .
- Image image = new ImageIcon(this.getClass().getResource("/image/myimage.jpg")).getImage();
- image = image.getScaledInstance(jLabel1.getWidth(), jLabel1.getHeight(), Image.SCALE_SMOOTH);
- ImageIcon icon = new ImageIcon(image);
- jLabel1.setIcon(icon );
-----------------------------------------------------------------------------------------------------------------
2nd option:
- File image = new File("c:/myImage.jpg");
- width = 120;
- height = 120;
- BufferedImage originalImage = ImageIO.read(image);
- BufferedImage resizedImage = ImageResizeUtil.resizeImage(originalImage, BufferedImage.TYPE_INT_ARGB, width , height)
- Image myImage = ImageResizeUtil.toImage(resizedImage );
and add this util class ImageResizeUtil
- public class ImageResizeUtil {
- public static Image toImage(BufferedImage bufferedImage) {
- return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
- }
- public static BufferedImage resizeImage(BufferedImage originalImage, int type , int width , int height) {
- BufferedImage resizedImage = new BufferedImage(width, height, type);
- Graphics2D g = resizedImage.createGraphics();
- g.drawImage(originalImage, 0, 0, width, height, null);
- g.dispose();
- return resizedImage;
- }
- }
Good luck.
No comments:
Post a Comment