Thread: Adjustment Image

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    58

    Adjustment Image

    hi, everybody.
    i tried to load image and create picture box with 152*152 size.
    but loading image is larger than this box size.
    so i need to adjust loaded image and show it in picture box.
    here are some codes.
    Code:
    DialogResult dr = ofd_Finger.ShowDialog();
    
    if (dr == DialogResult.OK)
                {
                    Image loadimg = Image.FromFile(ofd_Finger.FileName);
                    //pb_UserPhoto.Height = loadimg.Height;
                    //pb_UserPhoto.Width = loadimg.Width;
                    pb_UserPhoto.Image = loadimg;
                }
    ...........
    can u help me?
    best wishes.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    I would create a second Bitmap and size the original image onto the new Bitmap. Example:

    Code:
    DialogResult dr = ofd_Finger.ShowDialog();
    
    if (dr == DialogResult.OK)
    {
        Image loadimg = Image.FromFile(ofd_Finger.FileName);
        pb_UserPhoto.Image = AdjustImageSize(loadimg, pb_UserPhoto.Width, pb_UserPhoto.Height);
    }
    And the AdjustImageSize code:

    Code:
    private static Image AdjustImageSize(Image source_image, int desired_width, int desired_height)
    {
        Bitmap b = new Bitmap(desired_width, desired_height);
        Graphics g = Graphics.FromImage(b);
        g.DrawImage(source_image, new RectangleF(0, 0, desired_width, desired_height));
        return (Image)b;
    }

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I'd change 2 things with that code:

    1. Call g.Dispose() before you leave the function; this will free up the resources used by the Graphics object. It will happen eventually, but garbage collection can take a while.

    2. The default interpolation isn't very good. Setting

    Code:
    g.InterpolationMode = InterpolationMode.Bicubic;
    (Or one of the other options if preferred) would be better.

    Here's a piece of my code that does this. I pass the image by reference because I really don't care about the original (and I don't have any other references to the original apart from the one I pass in), so I trash the original and return the new image. If you cared about keeping the original image, you could of course do it your way, where it takes an image as a parameter and returns a new image as a return value.

    Mine also has a preserve aspect ratio option -- that is, it will never return an image wider than w or taller than h, but it will keep the aspect ratio fixed if you set the fourth parameter to true. So e.g. a 300x600 image that was set to resize to 200x200 with aspect ratio preserved would actually become 100x200.

    Code:
            private void ResizeImage(ref Image i, int w, int h, bool preserveAspectRatio, InterpolationMode mode)
            {
                int realWidth = w;
                int realHeight = h;
                if (preserveAspectRatio)
                {
                    double hscale = (double)w / (double)i.Width;
                    double vscale = (double)h / (double)i.Height;
    
                    double scale = (hscale < vscale) ? hscale : vscale;
    
                    // Rounded to nearest pixel
                    realWidth = (int)(i.Width * scale + 0.5);
                    realHeight = (int)(i.Height * scale + 0.5);
    
                }
    
                // Since copying or scaling a bitmap will use a crappy quality,
                // we need to make a new bitmap of the correct size, get the associated Graphic Context,
                // set the quality, and make a copy:
    
                Bitmap j = new Bitmap(realWidth, realHeight);  // New image
                Graphics g = Graphics.FromImage(j);
                g.InterpolationMode = mode;
                g.DrawImage(i, new Rectangle(0, 0, realWidth, realHeight));
                i.Dispose();  // Destroy the old image
                g.Dispose();
                i = j;        // Return the new one in our reference parameter.
            }
    Last edited by Cat; 04-17-2009 at 11:14 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    I'd never looked at InterpolationMode before. Thanks for pointing that one out. Learn something new everyday...

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If you need a quick and easy solution, you can set the SizeMode property of the picturebox to Stretch.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  3. Simple Image Processing
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 12:10 PM
  4. Replies: 4
    Last Post: 03-02-2003, 09:12 AM
  5. How to change a mouse cursor in console mode
    By GaPe in forum Windows Programming
    Replies: 10
    Last Post: 07-03-2002, 07:42 AM