Ke leyi's Image Compression class. This class can generate thumbnails Based on the specified width and height and compress files at the same time. If the specified height is the same as the source image, only the size of the image is compressed without changing the size, you can also specify the compression quality.
Winform example: http://download.keleyi.com/source/keleyithumbnail.rar
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. Drawing. imaging;
Namespace keleyi. com. Framework
{
Public class keleyithumbnail
{
# Region savekeleyithumbnail
/// <Summary>
/// Lossless Image Compression Method
/// </Summary>
/// <Param name = "sfile"> full name of the original image </param>
/// <Param name = "dfile"> the full name and suffix of the stored file. jpg </param>
/// <Param name = "dheight"> width after compression </param>
/// <Param name = "dwidth"> height after compression </param>
/// <Param name = "flag"> Compression Quality: 1-100 </param>
/// <Returns> Successful </returns>
Public static bool savekeleyithumbnail (string sfile, string dfile, int dwidth, int dheight, int flag)
{
Image isource = image. fromfile (sfile );
Imageformat tformat = isource. rawformat;
Int Sw = 0, SH = 0;
// Proportional Scaling
Size tem_size = new size (isource. Width, isource. Height );
If (tem_size.width> dheight | tem_size.width> dwidth)
{
If (tem_size.width * dheight)> (tem_size.height * dwidth ))
{
Sw = dwidth;
SH = (dwidth * tem_size.height)/tem_size.width;
}
Else
{
SH = dheight;
Sw = (tem_size.width * dheight)/tem_size.height;
}
}
Else
{
Sw = tem_size.width;
SH = tem_size.height;
}
Bitmap Ob = new Bitmap (dwidth, dheight );
Graphics G = graphics. fromimage (OB );
G. Clear (color. whitesmoke );
G. compositingquality = compositingquality. highquality;
G. smoothingmode = smoothingmode. highquality;
G. interpolationmode = interpolationmode. highqualitybicubic;
G. drawimage (isource, new rectangle (dwidth-SW)/2, (dheight-SH)/2, SW, SH), 0, 0, isource. width, isource. height, graphicsunit. pixel );
G. Dispose ();
// The following Code Set the compression quality when saving the image
Encoderparameters Ep = new encoderparameters ();
Long [] Qy = new long [1];
QY [0] = flag; // set the compression ratio to 1-100.
Encoderparameter eparam = new encoderparameter (system. Drawing. imaging. encoder. Quality, QY );
Ep. Param [0] = eparam;
Try
{
Imagecodecinfo [] arrayici = imagecodecinfo. getimageencoders ();
Imagecodecinfo policiciinfo = NULL;
For (INT x = 0; x <arrayici. length; X ++)
{
If (arrayici [X]. formatdescription. Equals ("Jpeg "))
{
Required iciinfo = arrayici [x];
Break;
}
}
If (policiciinfo! = NULL)
{
Ob. Save (dfile, jpegiciinfo, EP); // dfile Is The New Compressed path.
}
Else
{
Ob. Save (dfile, tformat );
}
Return true;
}
Catch
{
Return false;
}
Finally
{
Isource. Dispose ();
Ob. Dispose ();
}
}
# Endregion
}
}
This article reposted from Ke leyi http://www.keleyi.com/a/bjac/c8e988b2eb520f4b.htm