public byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
System.Drawing.Image original = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageFile));
int targetH, targetW;
if (original.Height > original.Width)
{
targetH = targetSize;
targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
}
else
{
targetW = targetSize;
targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
}
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageFile));
// Create a new blank canvas. The resized image will be drawn on this canvas.
System.Drawing.Bitmap bmPhoto = new System.Drawing.Bitmap(targetW, targetH, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
grPhoto.DrawImage(imgPhoto, new System.Drawing.Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, System.Drawing.GraphicsUnit.Pixel);
// Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked.
System.IO.MemoryStream mm = new System.IO.MemoryStream();
bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
original.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
return mm.GetBuffer();
}