How to Create Image thumbnails

Source: Internet
Author: User

There are multiple ways to create a thumbnail. The simplest method is to use the. net method to create a thumbnail, or use a re-painting test. If compression is required, you can use JPEG compression.
Below I tested it in several ways and used it. the built-in method of net can ensure the quality of creating thumbnails, while the quality of creating thumbnails in the re-painting method is slightly poor, especially when processing small images, such as portraits, and local details become blurred. redraw is relative.. net thumbnails can be a little smaller, but the quality is really stubborn.

Code
/// <Summary>
/// Use the thumbnail extraction method provided by. net to create a thumbnail
/// </Summary>
/// <Param name = "imageStream"> image input stream </param>
/// <Param name = "thumbWi"> specify the thumbnail width </param>
/// <Param name = "thumbHi"> specify the thumbnail height </param>
/// <Returns> returns the thumbnail byte data </returns>
Private byte [] getImageByteData (Stream imageStream, int thumbWi, int thumbHi)
{
System. Drawing. Image image = System. Drawing. Image. FromStream (imageStream );
Int height = thumbHi;
Int width = thumbWi;
If (image. Height> height | image. Width> width)
{
// Change the thumbnail
System. Drawing. Image. GetThumbnailImageAbort callb = null;
// Extract the thumbnail with the specified size
System. Drawing. Image thumbnailImage = image. GetThumbnailImage (thumbWi, thumbHi, callb, new System. IntPtr ());
Image = thumbnailImage;
}

MemoryStream Stream = new MemoryStream ();
Image. Save (Stream, System. Drawing. Imaging. ImageFormat. Jpeg );
Byte [] byteData = Stream. ToArray ();
Return byteData;
}

/// <Summary>
/// Create a thumbnail using the redrawing Method
/// </Summary>
/// <Param name = "source"> source image </param>
/// <Param name = "thumbWi"> specify the thumbnail width </param>
/// <Param name = "thumbHi"> specify the thumbnail height </param>
/// <Param name = "maintainAspect"> whether to maintain the length/width ratio </param>
/// <Returns> </returns>
Private byte [] getImageByteData (Bitmap source, int thumbWi, int thumbHi, bool maintainAspect)
{
Byte [] ret = null;
// Call to create a thumbnail and create a thumbnail of the specified size based on the source Image
Bitmap thumbnail = CreateThumbnail (source, thumbWi, thumbHi, maintainAspect );

# Configure JPEG compression serving for region, which can be compressed by more than 90%, with the same quality as the source Image

// Configure the encoderParameters parameter required to compress the thumbnail
EncoderParameters encoderParams = new EncoderParameters ();

Long [] quality = new long [1];
Quality [0] = 75; // compression quality, range: 0 ~ 100, the compression quality is inversely proportional to the size

EncoderParameter encoderParam = new EncoderParameter (System. Drawing. Imaging. Encoder. Quality, quality );
EncoderParams. Param [0] = encoderParam;

// Obtain the array of image encoder objects
ImageCodecInfo [] arrayICI = ImageCodecInfo. GetImageEncoders ();

ImageCodecInfo policici = null;
// Obtain the JPEG encoding information in the array of encoding objects.
For (int x = 0; x <arrayICI. Length; x ++)
{
If (arrayICI [x]. FormatDescription. Equals ("JPEG "))
{
JpegICI = arrayICI [x];
Break;
}
}
# Endregion

MemoryStream MS = new MemoryStream ();
// Save the specified JPEG parameter to the memory stream
Thumbnail. Save (MS, javasici, encoderParams );
Ret = ms. ToArray ();
Thumbnail. Dispose ();

Return ret;
}

/// <Summary>
/// Create a thumbnail
/// </Summary>
/// <Param name = "source"> source image </param>
/// <Param name = "thumbWi"> thumbnail width </param>
/// <Param name = "thumbHi"> thumbnail height </param>
/// <Param name = "maintainAspect"> whether to maintain the length/width ratio </param>
/// <Returns> </returns>
Private Bitmap CreateThumbnail (Bitmap source, int thumbWi, int thumbHi, bool maintainAspect)
{
// If the source image height width is smaller than the scaled height width, the source image is directly returned.
If (source. Width <= thumbWi & source. Height <= thumbHi) return source;

System. Drawing. Bitmap ret = null;
Try
{
Int wi, hi;

Wi = thumbWi;
Hi = thumbHi;

If (maintainAspect)
{
// Maintain the aspect ratio regardless of the thumbnail size parameter
If (source. Width> source. Height)
{
Wi = thumbWi;
Hi = (int) (source. Height * (decimal) thumbWi/source. Width ));
}
Else
{
Hi = thumbHi;
Wi = (int) (source. Width * (decimal) thumbHi/source. Height ));
}
}

// Original code that creates lousy thumbnails
// System. Drawing. Image ret = source. GetThumbnailImage (wi, hi, null, IntPtr. Zero );
Ret = new Bitmap (wi, hi );
Using (Graphics g = Graphics. FromImage (ret ))
{
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. HighQualityBicubic;
G. FillRectangle (Brushes. White, 0, 0, wi, hi );
G. DrawImage (source, 0, 0, wi, hi );
}
}
Catch
{
Ret = null;
}

Return ret;
}

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.