Results 1 to 3 of 3
  1. #1
    Nixil is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Question resize image dimesion without affecting its RGB values

    Hi all,

    I want to change the image dimension without affecting its RGB values.
    example:- as my original image dimensions are 660X650, and i want to change it into
    100x150.

    thanks in advance
    Nixil

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Here are the steps:

    1) Load your 660X650 image into a BufferedImage
    2) Create your new smaller image as a compatible BufferedImage in order to retain the same color and image settings
    3) Get the Graphics object of the new image
    4) Scale graphics object in order to shrink the image
    5) Draw the old image to the new image

    Here is a rough untested code:

    Java Code:
    int newWidth = 100;
    int newHeight = 150;
    
    //image is your 660x650 BuferedImage
    Graphics2D g2d = (Graphics2D)image.getGraphics();
    
    BufferedImage newImage = g2d.getDeviceConfiguration().createCompatibleImage(
                         newWidth , newHeight, java.awt.Transparency.OPAQUE);
    
    g2d = (Graphics2D)newImage.getGraphics();
    
    //i didn't test this
    g2d.scale(newWidth/660.0, newHeight/650.0);
    
    g2d.drawImage(image, 0, 0, null);
    My Hobby Project: LegacyClone

  3. #3
    Nixil is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Post

    Quote Originally Posted by mrmatt1111 View Post
    Here are the steps:

    1) Load your 660X650 image into a BufferedImage
    2) Create your new smaller image as a compatible BufferedImage in order to retain the same color and image settings
    3) Get the Graphics object of the new image
    4) Scale graphics object in order to shrink the image
    5) Draw the old image to the new image

    Here is a rough untested code:

    Java Code:
    int newWidth = 100;
    int newHeight = 150;
    
    //image is your 660x650 BuferedImage
    Graphics2D g2d = (Graphics2D)image.getGraphics();
    
    BufferedImage newImage = g2d.getDeviceConfiguration().createCompatibleImage(
                         newWidth , newHeight, java.awt.Transparency.OPAQUE);
    
    g2d = (Graphics2D)newImage.getGraphics();
    
    //i didn't test this
    g2d.scale(newWidth/660.0, newHeight/650.0);
    
    g2d.drawImage(image, 0, 0, null);
    Thanks mrmatt1111
    but the problem not solved, it shows me blank frame.
    here is the code
    Java Code:
    //My resetImage class
    class resetImage extends JFrame   {
        
        scanPixel sp = new scanPixel();
        //Getting BufferedImage 640 x 600
        BufferedImage resetimage = sp.getBufferedImage();
    
        //reset into new width and height
        int newWidth = 400;
        int newHeight = 450;
    
    
        public resetImage() {
    
            setSize(newWidth,newHeight);
            setVisible(true);
        }
    
        public void paint(Graphics g)   {
            //your code
            
            Image img = (Image)resetimage;
            //Graphics2D g2d = (Graphics2D)g;
            Graphics2D g2d = (Graphics2D)resetimage.getGraphics();
    
            BufferedImage newImage = g2d.getDeviceConfiguration().createCompatibleImage(
                            newWidth , newHeight, java.awt.Transparency.OPAQUE);
    
            g2d = (Graphics2D)newImage.getGraphics();
            
            //i didn't test this
            g2d.scale(newWidth/sp.width, newHeight/sp.height);
            //g2d.translate(x,y); //you might need to move it to make it fit
    
            g2d.drawImage(newImage, 0, 0, null);
    
        }
    
    }
    But when i redraw the image with new drawImage and new width and height it works, but image getting is not clear.

    thanks :)

Similar Threads

  1. how to resize an image
    By marodia in forum AWT / Swing
    Replies: 2
    Last Post: 08-21-2009, 04:10 AM
  2. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  3. Image Resize
    By jithan in forum New To Java
    Replies: 4
    Last Post: 11-07-2008, 09:26 AM
  4. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  5. Replies: 4
    Last Post: 04-15-2008, 01:04 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
  •