Ex: i have some image with 254pixels width and 150pixels height. I want to resize the image to 150pixels and 100px width. Again i have to notify the user, that changes had done, want to store in which location.
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;
I use the following code to resize an image down to 700(w) x 100(h) px. It preserves the aspect ratio of the original image. The result is saved as a gif, but you can change that in the obvious place. (You will need using SystemDrawing if not already
imported).
using (Image logo = Image.FromFile(uploadedFileName))
{
Single xScale = 1.0F;
Single yScale = 1.0F;
if (logo.Height > 100) yScale = 100F / logo.Height;
if (logo.Width > 700) xScale = 700F / logo.Width;
Single scale = Math.Min(xScale, yScale);
if (scale < 1.0F)
{
Size logoSize = new Size((int)(logo.Width * scale), (int)(logo.Height * scale));
Rectangle logoRect = new Rectangle(new Point(0, 0), logoSize);
Bitmap scaledLogo = new Bitmap(logoSize.Width, logoSize.Height);
Graphics canvas = Graphics.FromImage(scaledLogo);
canvas.FillRectangle(new SolidBrush(Color.White), logoRect);
canvas.DrawImage(logo, new Rectangle(0, 0, (int)(logo.Width * scale), (int)(logo.Height * scale)));
scaledLogo.Save(tmpFile, ImageFormat.Gif);
}
else
{
logo.Save(tmpFile, ImageFormat.Gif);
}
MM_MSDN
Member
26 Points
65 Posts
How to resize image and save - jpeg image
Apr 22, 2012 09:04 AM|LINK
Dear All,
I have a requirement, to resize the image.
Ex: i have some image with 254pixels width and 150pixels height. I want to resize the image to 150pixels and 100px width. Again i have to notify the user, that changes had done, want to store in which location.
BR
MM
abiruban
All-Star
16072 Points
2736 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.
amit.jain
Star
11225 Points
1815 Posts
Re: How to resize image and save - jpeg image
Apr 22, 2012 12:39 PM|LINK
refer http://csharpdotnetfreak.blogspot.com/2010/02/resize-image-upload-ms-sql-database.html
amiT jaiN
ASP.NET C# VB Articles And Code Examples
Paul Linton
Star
13431 Points
2535 Posts
Re: How to resize image and save - jpeg image
Apr 22, 2012 10:58 PM|LINK
I use the following code to resize an image down to 700(w) x 100(h) px. It preserves the aspect ratio of the original image. The result is saved as a gif, but you can change that in the obvious place. (You will need using SystemDrawing if not already imported).
using (Image logo = Image.FromFile(uploadedFileName)) { Single xScale = 1.0F; Single yScale = 1.0F; if (logo.Height > 100) yScale = 100F / logo.Height; if (logo.Width > 700) xScale = 700F / logo.Width; Single scale = Math.Min(xScale, yScale); if (scale < 1.0F) { Size logoSize = new Size((int)(logo.Width * scale), (int)(logo.Height * scale)); Rectangle logoRect = new Rectangle(new Point(0, 0), logoSize); Bitmap scaledLogo = new Bitmap(logoSize.Width, logoSize.Height); Graphics canvas = Graphics.FromImage(scaledLogo); canvas.FillRectangle(new SolidBrush(Color.White), logoRect); canvas.DrawImage(logo, new Rectangle(0, 0, (int)(logo.Width * scale), (int)(logo.Height * scale))); scaledLogo.Save(tmpFile, ImageFormat.Gif); } else { logo.Save(tmpFile, ImageFormat.Gif); }viratsingh2
Member
78 Points
50 Posts
Re: How to resize image and save - jpeg image
Apr 23, 2012 05:29 PM|LINK
Please check following link, it may be helpful to you.