/// <Summary>
/// Image Compression Method
/// </Summary>
/// <Param name = "sfile"> file stream fileupload. postedfile. inputstream </param>
/// <Param name = "dfile"> location to be saved </param>
/// <Param name = "flag"> definition </param>
/// <Returns> </returns>
Public bool getpicthumbnail (Stream sfile, string dfile, int dheight, int dwidth, int flag)
{
// System. drawing. image isource = system. drawing. image. fromfile (sfile); The sfile parameter is the original file path. You can use this method to set the permission, so it is best to use the file stream to read the file.
System. Drawing. Image isource = system. Drawing. image. fromstream (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) // change ** to the OR operation symbol in C #.
{
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 sets 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 ();
}
}