Results 1 to 7 of 7

Thread: Resize Image to a Maximum Size [Resolved]

  1. #1

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551

    Resize Image to a Maximum Size [Resolved]

    Hello,

    This post could easily go in asp.net or here, in vb.net, so i decided to post here... this is my situation: I have an image, coming from an IO.Stream .... i need to take this image, and resize it...

    the stipulations are though, that i only want to resize it if the image is wider than 200 pixels, or taller than 200 pixels... if that is the case, i need to scale the image down to no more than 200 x 200... and of course, i want to keep the image scale, i don't want it stretching....

    once i have this new image, i want to paste it centered on a 200x200 white background image... obviously, i am doing this to maintain a common image size across all images to avoid problems with page content appearance...


    so, to sum it up, i need to:

    1) Scale the input IO.Stream image (JPEG format) down to no more than 200x200 only if it is larger than this to begin with, keeping the picture's scale (no stretching)

    2) paste the resized image onto the center of a new white background 200x200 pixel image.

    3) Save the new picture file to a specified filepath...



    any ideas of how i should go about this? i've been researching ways to achieve this, and there are just sooo many different ways it seems, i'd like to have some input... and possibly code examples... thanks guys~!
    Last edited by Redth; Apr 8th, 2003 at 10:12 AM.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I have code that can do the resizing along with keeping it looking right (not stretching it), but it is in C#. I will try to find it and post it for you if you want.

  3. #3

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    i would really appreciate it!

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Try this out. I have modified it a little to fit your needs and should be real close to what you need. It doesn't solve all your problems, but I think it does solve the harder one of keeping the image in the right proportions when sizing it down.

    PHP Code:
    // Your pixel size you want the image to fit in.
    int iWidth 200;
    int iHeight 200;

    // This just creates a bitmap object from the original image.
    Bitmap pic = new Bitmap(fileName);

    // This creates a new Bitmap to hold the modified one.
    Bitmap modifiedPic;

    // Get the size of the new image so it won't have to be stretched.
    SizeF sizef = new SizeF(pic.Width pic.HorizontalResolutionpic.Height pic.VerticalResolution);

    float fScale Math.Min(iWidth sizef.WidthiHeight sizef.Height);

    sizef.Width *= fScale;
    sizef.Height *= fScale;
                
    modifiedPic pic.GetThumbnailImage((int)sizef.Width,(int)sizef.Heightnull,(IntPtr)0);

    modifiedPic.Save(newFileNameToSaveTo); 

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Just a note, that code will also size up a picture if the picture is smaller than the 200x200. So you might want to check if the picture is bigger first before running through the code.

    Or you may decide to have all images as close to the 200x200 scale by running them all through it. Good luck

  6. #6

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    hellswraith, thanks for the reply, that code is exactly what i needed

  7. #7
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Resize Image to a Maximum Size [Resolved]

    Is there anybody who would be able to change this code into vb.net for me as i'm fairly new to vb.net and am not sure how to do it?
    Thanks for any help

Posting Permissions

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



Click Here to Expand Forum to Full Width