Public static byte [] ResizeImageFile (byte [] imageFile, PhotoSize size)
{
Using (System. Drawing. Image original = System. Drawing. Image. FromStream (new MemoryStream (imageFile )))
{
Int targetH, targetW;
If (size = PhotoSize. Small | size = PhotoSize. Medium)
{
// Regardless of orientation,
// The * height * is constant for thumbnail images (UI constraint)
If (original. Height> original. Width)
{
If (size = PhotoSize. Small)
TargetH = DefaultValues. FixedSmallImageHeight;
Else
TargetH = DefaultValues. FixedMediumImageHeight;
TargetW = (int) (original. Width * (float) targetH/(float) original. Height ));
}
Else
{
If (size = PhotoSize. Small)
TargetW = DefaultValues. FixedSmallImageWidth;
Else
TargetW = DefaultValues. FixedMediumImageWidth;
TargetH = (int) (original. Height * (float) targetW/(float) original. Width ));
}
}
Else
{
// For full preview, we scale proportionally according to orienation
If (original. Height> original. Width)
{
TargetH = Math. Min (original. Height, DefaultValues. MaxFullImageSize );
TargetW = (int) (original. Width * (float) targetH/(float) original. Height ));
}
Else
{
TargetW = Math. Min (original. Width, DefaultValues. MaxFullImageSize );
TargetH = (int) (original. Height * (float) targetW/(float) original. Width ));
}
}
Using (System. Drawing. Image imgPhoto = System. Drawing. Image. FromStream (new MemoryStream (imageFile )))
{
// Create a new blank canvas. The resized image will be drawn on this canvas.
Using (Bitmap BMP hoto = new Bitmap (targetW, targetH, PixelFormat. Format24bppRgb ))
{
BMP hoto. SetResolution (72, 72 );
Using (Graphics grPhoto = Graphics. FromImage (BMP hoto ))
{
GrPhoto. SmoothingMode = SmoothingMode. AntiAlias;
GrPhoto. InterpolationMode = InterpolationMode. HighQualityBicubic;
GrPhoto. PixelOffsetMode = PixelOffsetMode. HighQuality;
GrPhoto. DrawImage (imgPhoto, new Rectangle (0, 0, targetW, targetH), 0, 0, original. Width, original. Height, GraphicsUnit. Pixel );
MemoryStream mm = new MemoryStream ();
BMP hoto. Save (mm, System. Drawing. Imaging. ImageFormat. Jpeg );
Return mm. GetBuffer ();
}
}
}
}
}