Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Apr 22, 2012 09:07 AM|LINK
This next set of code is a slightly longer and more complex. The main reason this code is longer is because this resize function will keep the height and width proportional.
To start with we see that the input parameters are the image to resize (System.Drawing.Image) and the size (System.Drawing.Size). Also in this set of code are a few variables we use. The first two are the source height and width which is used later. And
there are 3 other variables to calculate the proportion information.
The next step is to actually figure out what the size of the resized image should be. The first step is to calculate the percentages of the new size compared to the original. Next we need to decide which percentage is smaller because this is the percent
of the original image we will use for both height and width. And now we calculate the number of height and width pixels for the destination image.
The final thing to do is create the bitmap (System.Drawing.Bitmap) which we will draw the resized image on using a Graphics (System.Drawing.Graphics) object. I also set the interpolation mode, which is the algorithm used to resize the image. I prefer HighQualityBicubic,
which from my testing seems to return the highest quality results. And just to clean up a little I dispose the Graphics object.
And this gives us the final code.
Here is the source code and a C# VS2005 Express Edition solution with the needed methods and some test code.Click Me for Code