|
-
Apr 7th, 2003, 08:00 PM
#1
Thread Starter
Fanatic Member
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.
-
Apr 7th, 2003, 09:41 PM
#2
PowerPoster
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.
-
Apr 7th, 2003, 09:49 PM
#3
Thread Starter
Fanatic Member
i would really appreciate it!
-
Apr 7th, 2003, 10:14 PM
#4
PowerPoster
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.HorizontalResolution, pic.Height / pic.VerticalResolution);
float fScale = Math.Min(iWidth / sizef.Width, iHeight / sizef.Height);
sizef.Width *= fScale;
sizef.Height *= fScale;
modifiedPic = pic.GetThumbnailImage((int)sizef.Width,(int)sizef.Height, null,(IntPtr)0);
modifiedPic.Save(newFileNameToSaveTo);
-
Apr 7th, 2003, 10:18 PM
#5
PowerPoster
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
-
Apr 8th, 2003, 10:00 AM
#6
Thread Starter
Fanatic Member
hellswraith, thanks for the reply, that code is exactly what i needed
-
Aug 14th, 2006, 11:21 AM
#7
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|