0
I am using the following routine below to resize images, it is 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. What could 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;
}
I also imagined that it could be the format, I added the parameter with the format Imageformat.Jpeg but at first I had no result, after doing several tests, I realized that other image files were getting correct, Then I did some more tests using the same image, saving with a different name was getting correct, I believe there was some kind of cache when saved with the same name. To conclude, I think the problem was the lack of the parameter with the same format.
– Luciano
@Lucianobeguelini If you think the answer really solves your problem, please mark it as accepted.
– Daniel