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 ();
}
}
}
}