Lossless scaling of images (for beginners, if there is no loss at a low level, you still need advice from someone else)

Source: Internet
Author: User
Using System;
Using System. IO;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Drawing. Drawing2D;

Namespace ZHT. Utility. Share
{
Public class Thumbnail
{
Public Thumbnail ()
{

}

/// <SUMMARY>
/// Resize the image
/// </SUMMARY>
/// <PARAM name = "sourceFile"> Image Source Path </PARAM>
/// <PARAM name = "destFile"> scaled image output path </PARAM>
/// <PARAM name = "destHeight"> scaled image height </PARAM>
/// <PARAM name = "destWidth"> scaled image width </PARAM>
/// <RETURNS> </RETURNS>
Public static bool GetThumbnail (string sourceFile, string destFile, int destHeight, int destWidth)
{
System. Drawing. Image imgSource = System. Drawing. Image. FromFile (sourceFile );
System. Drawing. Imaging. ImageFormat thisFormat = imgSource. RawFormat;
Int sW = 0, sH = 0;
// Proportional Scaling
Int sWidth = imgSource. Width;
Int sHeight = imgSource. Height;

If (sHeight> destHeight | sWidth> destWidth)
{
If (sWidth * destHeight)> (sHeight * destWidth ))
{
SW = destWidth;
SH = (destWidth * sHeight)/sWidth;
}
Else
{
SH = destHeight;
SW = (sWidth * destHeight)/sHeight;
}
}
Else
{
SW = sWidth;
SH = sHeight;
}

Bitmap outBmp = new Bitmap (destWidth, destHeight );
Graphics g = Graphics. FromImage (outBmp );
G. Clear (Color. WhiteSmoke );

// Set the painting quality of the canvas
G. CompositingQuality = CompositingQuality. HighQuality;
G. SmoothingMode = SmoothingMode. HighQuality;
G. InterpolationMode = InterpolationMode. HighQualityBicubic;

G. drawImage (imgSource, new Rectangle (destWidth-sW)/2, (destHeight-sH)/2, sW, sH), 0, imgSource. width, imgSource. height, GraphicsUnit. pixel );
G. Dispose ();

// The following Code sets the compression quality when saving the image
EncoderParameters encoderParams = new EncoderParameters ();
Long [] quality = new long [1];
Quality [0] = 100;

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

Try
{
// Obtain the ImageCodecInfo object that contains information about the built-in image decoder.
ImageCodecInfo [] arrayICI = ImageCodecInfo. GetImageEncoders ();
ImageCodecInfo policici = null;
For (int x = 0; x <arrayICI. Length; x ++)
{
If (arrayICI [x]. FormatDescription. Equals ("JPEG "))
{
JpegICI = arrayICI [x]; // sets the JPEG encoding.
Break;
}
}

If (policici! = Null)
{
OutBmp. Save (destFile, jpegICI, encoderParams );
}
Else
{
OutBmp. Save (destFile, thisFormat );
}

Return true;
}
Catch
{
Return false;
}
Finally
{
ImgSource. Dispose ();
OutBmp. Dispose ();
}
}
}
}

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.