调整图像的大小 [英] Resizing an image in swing

查看:121
本文介绍了调整图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码用于将图像调整为窗帘大小(我想将分辨率更改为200 dpi)。基本上我需要它的原因是因为我想显示用户选择的图像(有点大),然后如果用户同意我想在不同的地方显示相同的图像,但使用较小的分辨率。不幸的是,如果我给它一个大的图像,屏幕上不会显示任何内容。此外,如果我更改

I have snippet of code that I am using for the purpose of resizing an image to a curtain size (I want to change the resolution to something like 200 dpi). Basically the reason I need it is because I want to display the image that the user have picked (somewhat large) and then if the user approves I want to display the same image in a different place but using a smaller resolution. Unfortunately, if I give it a large image nothing appears on the screen. Also, if I change

imageLabel.setIcon(newIcon); 

imageLabel.setIcon(icon); 

我得到要显示的图像,但不是正确的分辨率,这就是我知道自己有问题的原因在这个代码的snipper中,而不是其他地方。

I get the image to display but not in the correct resolution that's how I know that I have a problem inside this snipper of code and not somewhere else.

Image img = icon.getImage();

BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
boolean myBool = g.drawImage(img, 0, 0, 100, 100, null);
System.out.println(myBool);
ImageIcon newIcon = new ImageIcon(bi);
imageLabel.setIcon(newIcon);
submitText.setText(currentImagePath);
imageThirdPanel.add(imageLabel);


推荐答案

这是我的解决方案:

    private BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) throws IOException {  
        BufferedImage resizedImage = new BufferedImage(width, height, type);  
        Graphics2D g = resizedImage.createGraphics();  
        g.drawImage(originalImage, 0, 0, width, height, null);  
        g.dispose();  
        return resizedImage;  
    }  

这篇关于调整图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆