如何拉伸图像 [英] how to stretch image

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

问题描述

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

i want to strech image by using Graphics but unable here is my code it shows image in size that i want but not strach 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();
    }
}


推荐答案

致电 getScaledInstance() 在创建 ImageIcon 之前将图像缩放到所需的大小。您无需在标签上调用 setMinimumSize

Call getScaledInstance() to scale the image to the size you want before you create the ImageIcon. You don't need to call setMinimumSize on the label.

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

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

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