Just wandering, how to reduce the file size of the image but keeping the quality in C# .net?
I have the following code use to save the image taken from ipad:
Bitmap srcBmp = new Bitmap(file.InputStream);
int w = srcBmp.Width;
int h = srcBmp.Height;
if (srcBmp.Width > 2732 && srcBmp.Height > 2048)
{
w = 2732;
h = 2048;
}
SizeF newSize = new SizeF(w, h);
Bitmap target = new Bitmap(w, h);
var destRect = new Rectangle(0, 0, w, h);
HttpContext.Response.Clear();
HttpContext.Response.ContentType = "image/jpeg";
using (var graphics = Graphics.FromImage(target))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(srcBmp, destRect, 0, 0, w, h, GraphicsUnit.Pixel, wrapMode);
using (FileStream fileStream = new FileStream(fname, FileMode.Create))
{
target.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
The Image been saved is always over 14MB. Anyway I can keep the image quality and reduce the file size below 2MB?