Method of Use: This method is the same as the size of the image, because the internal uniform setting dheight,dwidth and the original picture's length and width are consistent.
Flag: Compression ratio, where only pixels are compressed, without changing the appearance size of the image (both long and wide)
Lossless compression Picture
<param name= "SFile" > Original picture </param>
<param name= "Dfile" > Save location after compression </param>
<param name= "Dheight" > Height </param>
<param name= "Dwidth" ></param>
<param name= "Flag" > Compression quality (the smaller the number the higher the compression rate) 1-100</param>
<returns></returns>
public static bool Getpicthumbnail (Stream sFile, string dfile, int dheight, int dwidth, int flag)
{
Image ISource = Image.fromstream (sFile);
ImageFormat Tformat = Isource.rawformat;
int SW = 0, SH = 0;
Note the following two lines of code if you need to adjust the length and width of the picture
Dheight = Isource.height;
Dwidth = Isource.width;
Scale by 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. Width * 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 the picture is saved
EncoderParameters EP = new EncoderParameters ();
long[] qy = new Long[1];
Qy[0] = flag;//Set the scale of the compression 1-100
Encoderparameter Eparam = new Encoderparameter (System.Drawing.Imaging.Encoder.Quality, QY);
Ep. Param[0] = Eparam;
Try
{
imagecodecinfo[] Arrayici = Imagecodecinfo.getimageencoders ();
ImageCodecInfo jpegiciinfo = null;
for (int x = 0; x < arrayici.length; × x + +)
{
if (Arrayici[x]. Formatdescription.equals ("JPEG"))
{
Jpegiciinfo = Arrayici[x];
Break
}
}
if (jpegiciinfo! = null)
{
Ob. Save (Dfile, Jpegiciinfo, EP);//dfile is the new path after compression
}
Else
{
Ob. Save (Dfile, Tformat);
}
return true;
}
Catch
{
return false;
}
Finally
{
Isource.dispose ();
Ob. Dispose ();
}
C # Picture Lossless compression