Results 1 to 5 of 5
  1. #1
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default How to scale an image to desired size?

    Hello all,
    As the heading says, i have a label in which i want to show a picture, i have take the picture as an inputStream from the database and put it as an image icon on a label.

    my code is:
    Java Code:
    /**
    *   Setting icon on label imageLabel
    *
    */
    if (imageInputStream != null){
                try {
                    Image image = ImageIO.read(admin.image);
                    imageLabel.setIcon(new ImageIcon(image));
                } catch (IOException ex) {
                   JOptionPane.showMessageDialog(this, "Error: "+ex.getMessage(), "Error in reading Image", JOptionPane.ERROR_MESSAGE);
                }
            }
    But if i have an image of size larger than my label size than only some part is visible in the viewport of label..
    What i want to do is to automatically scale the image to desired label size without distorting image as much as possible..Also the image taken as input is not of same size..
    How can i achieve this??
    Any help? suggestions? tutorials? Articles?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    First off, if you use an appropriate layout the JLabel will be sized appropriately based on the size of the Icon.

    If due to other constraints you need the image to be displayed at a fixed size, it may be better to achieve this via custom painting on a JComponent or JPanel. Choose a suitable overload of Graphics#drawImage(...).
    Lesson: Performing Custom Painting (The Java Tutorials > Creating a GUI With JFC/Swing)

    db

  3. #3
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    That lesson will help me for other stuffs in my project, for now particularly, i define a method to get sclaed image of whatever image i get from database to desired scale:

    Java Code:
    public static BufferedImage getScaledImage(BufferedImage pImage, int pWidth, int pHeight)
        {
            int type = pImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : pImage.getType();
            BufferedImage resizedImage = new BufferedImage(pWidth, pHeight, type);
            Graphics2D graphics = resizedImage.createGraphics();
            graphics.setComposite(AlphaComposite.Src);
    
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
            graphics.drawImage(pImage, 0, 0, pWidth, pHeight, null);
            graphics.dispose();
    
            return resizedImage;
        }
    Any other suggestions for above method?
    Thanks!

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Java Code:
    Image.getScaledInstance(...);

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

Similar Threads

  1. Error in converting colour image to gray scale
    By LankanSniper in forum Java 2D
    Replies: 9
    Last Post: 12-14-2009, 04:30 PM
  2. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 04:37 PM
  3. Image size in java
    By Sharath in forum New To Java
    Replies: 1
    Last Post: 04-01-2009, 01:06 PM
  4. Replies: 3
    Last Post: 03-21-2009, 09:34 PM
  5. how to set an image size
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •