/// <summary> ///image thumbnail Processing/// </summary> /// <param name= "bytes" >Image source Data</param> /// <param name= "Compression" >Compression Quality 1-100</param> /// <param name= "Thumbwidth" >the width of the thumbnail image</param> /// <param name= "Thumbheight" >the height of the thumbnail image</param> /// <returns></returns> Public Static byte[] Converttothumbnail (byte[] Bytes,intCompression = -,intThumbwidth =0,intThumbheight =0) { byte[] bs =NULL; Try { if(Bytes! =NULL) { using(MemoryStream ms =NewMemoryStream (bytes)) { using(Bitmap srcimg =NewBitmap (MS)) { if(Thumbwidth = =0&& Thumbheight = =0) {Thumbwidth=srcimg. Width; Thumbheight=srcimg. Height; } using(Bitmap dstimg =NewBitmap (Thumbwidth, Thumbheight))//Picture Compression Quality { //Create a System.Drawing.Graphics object from bitmap to draw a high-quality zoomed-out diagram. using(Graphics gr =graphics.fromimage (dstimg)) { //draw the original image into a zoomed-out diagram above the set widthRectangle rectdestination =NewRectangle (0,0, Thumbwidth, thumbheight); Gr. Clear (Color.whitesmoke); Gr.compositingquality=compositingquality.highquality; Gr. SmoothingMode=smoothingmode.highquality; Gr. Interpolationmode=interpolationmode.highqualitybicubic; Gr. DrawImage (srcimg, Rectdestination,0,0, Srcimg. Width, Srcimg. Height, GraphicsUnit.Pixel); EncoderParameters EP=NewEncoderParameters (1); Ep. param[0] =NewEncoderparameter (System.Drawing.Imaging.Encoder.Quality, compression);//set the scale of the compression 1-100imagecodecinfo[] Arrayici =imagecodecinfo.getimageencoders (); ImageCodecInfo Jpegiciinfo= Arrayici.firstordefault (t = = T.formatid = =System.Drawing.Imaging.ImageFormat.Png.Guid); using(MemoryStream Dstms =NewMemoryStream ()) { if(Jpegiciinfo! =NULL) {dstimg. Save (Dstms, Jpegiciinfo, EP); } Else{dstimg. Save (Dstms, System.Drawing.Imaging.ImageFormat.Png);//Save in memory} BS=NewByte[dstms. Length]; Dstms. Position=0; Dstms. Read (BS,0, BS. Length); } } } } } } } Catch(Exception ex) {LogManager.DefaultLogger.Error (Logconvert.parsewebex (ex),string. Concat ("converttothumbnail error.", Thumbwidth," ", Thumbheight)); } returnBS; }
C # Picture Lossless compression