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.
<div class="geshifilter"> <div class="csharp geshifilter-csharp">privatestaticImage resizeImage(Image imgToResize, Size size) { int sourceWidth = imgToResize.Width; int sourceHeight = imgToResize.Height;
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.
int destWidth =(int)(sourceWidth * nPercent); int destHeight =(int)(sourceHeight * nPercent);</div> </div>
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.
<div class="geshifilter"> <div class="csharp geshifilter-csharp">Bitmap b =newBitmap(destWidth, destHeight); Graphics g =Graphics.FromImage((Image)b);
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
abiruban
All-Star
16002 Points
2731 Posts
Re: How to resize image and save - jpeg image
Apr 22, 2012 09:07 AM|LINK
Resizing
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.
<div class="geshifilter"> <div class="csharp geshifilter-csharp">private static Image resizeImage(Image imgToResize, Size size){
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
}</div> </div>
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.
<div class="geshifilter"> <div class="csharp geshifilter-csharp">nPercentW = ((float)size.Width / (float)sourceWidth);nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);</div> </div>
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.
<div class="geshifilter"> <div class="csharp geshifilter-csharp">Bitmap b = new Bitmap(destWidth, destHeight);Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();</div> </div>
And this gives us the final code.
<div class="geshifilter"> <div class="csharp geshifilter-csharp">private static Image resizeImage(Image imgToResize, Size size){
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}</div> </div>
Here is the source code and a C# VS2005 Express Edition solution with the needed methods and some test code.Click Me for Code
http://www.codeproject.com/Articles/76206/Image-in-C-Save-Resize-and-Convert-to-Binary\
http://kossovsky.net/index.php/2009/06/image-resizing/
thank u
***DON'T FORGET TO CLICK “MARK AS ANSWER” ON THE POST IF HELPED YOU.