C # Generating thumbnails based on the source Image

Source: Internet
Author: User

/// <Summary>
/// Generate a thumbnail
/// </Summary>
/// <Param name = "imgBuffer"> source image byte [] </param>
/// <Param name = "width"> width of the generated thumbnail </param>
/// <Param name = "height"> height of the generated thumbnail </param>
/// <Returns> </returns>
Private byte [] GenerateThumbImg (byte [] imgBuffer, int width, int height)
{
MemoryStream imgStream = null;
MemoryStream thumbStream = new MemoryStream ();;
System. Drawing. Image img = null;
System. Drawing. Image thumbImg = null;
System. Drawing. Graphics g = null;
Try
{
ImgStream = new MemoryStream (imgBuffer );
Img = System. Drawing. Image. FromStream (imgStream );
ThumbImg = new System. Drawing. Bitmap (img, width, height );
G = System. Drawing. Graphics. FromImage (thumbImg );
// Set the painting quality of the canvas
G. CompositingQuality = System. Drawing. Drawing2D. CompositingQuality. HighQuality;
G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. HighQualityBicubic;
G. DrawImage (thumbImg, 0, 0, width, height );
/* G. DrawImage (img, new System. Drawing. Rectangle (0, 0, width, height ),
0, 0, width, height, System. Drawing. GraphicsUnit. Pixel );*/
ThumbImg. Save (thumbStream, System. Drawing. Imaging. ImageFormat. Jpeg );
Return thumbStream. ToArray ();
}
Catch (Exception ex)
{
Return null;
}
Finally
{
If (g! = Null)
G. Dispose ();
If (thumbImg! = Null)
ThumbImg. Dispose ();
If (img! = Null)
Img. Dispose ();
If (thumbStream! = Null)
ThumbStream. Close ();
If (imgStream! = Null)
ImgStream. Close ();
}
}

Related Article

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.