C # Picture Lossless compression

Source: Internet
Author: User

        /// <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.