Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 23, 2012 05:29 PM by viratsingh2
Apr 22, 2012 09:04 AM|LINK
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.
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
Apr 22, 2012 12:39 PM|LINK
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
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)));
Apr 23, 2012 05:29 PM|LINK
Please check following link, it may be helpful to you.