I'm using the following routine to resize images, it's working, however the resulting files are getting a very large size, for example when I resize an image from 1400x700 from 500kb to 1200x600 the new file is getting 1.6 Mb. can be wrong?
Main Routine:
uploadedFile.SaveAs(Server.MapPath(FilePath));
System.Drawing.Image img_original =
System.Drawing.Image.FromFile(Server.MapPath(FilePath));
System.Drawing.Image img_resized = Funcoes.ResizeImage(img_original, new Size(1200, 600), true);
ImageResize function:
public static System.Drawing.Image ResizeImage(System.Drawing.Image image, Size size,
bool preserveAspectRatio = true)
{
int newWidth;
int newHeight;
if (preserveAspectRatio)
{
int originalWidth = image.Width;
int originalHeight = image.Height;
float percentWidth = (float)size.Width / (float)originalWidth;
float percentHeight = (float)size.Height / (float)originalHeight;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
newWidth = (int)(originalWidth * percent);
newHeight = (int)(originalHeight * percent);
}
else
{
newWidth = size.Width;
newHeight = size.Height;
}
System.Drawing.Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}