I have inherited a code which resizes images in width and height. What I also observe is it also decreases the file size even when the image height and width increase. Here is the resize code and calling function code which saves image
for example which might better explain.I have original image which is 709*653 pixels and file size of 670 kb
Upon resizing, it is 1000*921 which is expected but it's size is 176 kb
Image orgImage = Image.FromFile(originalimagepath);
//resizes image using below function
transformedImage = ImageUtils.resizeImage(orgImage, Settings.MaxLargeImage);
memstrImage = new MemoryStream();
//saves image to memorystream which is in turn saved in destination as resized image.
transformedImage.Save(memstrImage,ImageFormat.Jpeg);
public static Image resizeImage(Image imgToResize, int resizeType)
{
//resizetype is maximum resize width I want image to take.
int height = imgToResize.Height;
int width = imgToResize.Width;
float ratio = 0;
Size size = new Size();
if (height >= width)
{
ratio = ((float)resizeType / (float)height);
}
else
{
ratio = ((float)resizeType / (float)width);
}
size.Height = Convert.ToInt32((float)ratio * (float)imgToResize.Height);
size.Width = Convert.ToInt32((float)ratio * (float)imgToResize.Width);
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 destHeight = Convert.ToInt32(sourceHeight * nPercent);
int destWidth = Convert.ToInt32(sourceWidth * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
I assume, and could see aspect ratios arent compromised but Is it suggested to keep this as it is, or Is it logical that ideally file size should increase , when image height and width increase
For the question, I want to increase file size when height and width increase but best practice suggestions are welcome.
Any suggestions?