Asp.net (C #) code for image shrinking (from the author of classKit in asp. netStartKit)

Source: Internet
Author: User
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 ();
}
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.