How to compress images in C #

Source: Internet
Author: User

The search from the Internet is very effective. Picture 3M to 500k

   Private Staticimagecodecinfo getencoderinfo (String mimeType) {intJ;            Imagecodecinfo[] encoders; Encoders=imagecodecinfo.getimageencoders ();  for(j =0; J < Encoders. Length; ++j) {if(Encoders[j]. MimeType = =MimeType)returnEncoders[j]; }            return NULL; }        /// <summary>        ///picture compression (reduces the quality to reduce the size of the file)/// </summary>        /// <param name= "Srcbitmap" >the incoming Bitmap object</param>        /// <param name= "Deststream" >the compressed Stream object</param>        /// <param name= "level" >compression level, 0 to 100, 0 worst quality, 100 best</param>         Public Static voidCompress (Bitmap srcbitmap, Stream Deststream,LongLevel )            {ImageCodecInfo myimagecodecinfo;            Encoder Myencoder;            Encoderparameter Myencoderparameter;            EncoderParameters myencoderparameters; //Get An ImageCodecInfo object that represents the JPEG codec.Myimagecodecinfo = Getencoderinfo ("Image/jpeg"); //Create an Encoder object based on the GUID//For the quality parameter category.Myencoder =encoder.quality; //Create an EncoderParameters object. //An EncoderParameters object have an array of encoderparameter//objects. In this case, there are only one//Encoderparameter object in the array.Myencoderparameters =NewEncoderParameters (1); //Save the bitmap as a JPEG file with a given quality levelMyencoderparameter =NewEncoderparameter (Myencoder, level); myencoderparameters.param[0] =Myencoderparameter;        Srcbitmap.save (Deststream, Myimagecodecinfo, myencoderparameters); }        /// <summary>        ///picture compression (reduces the quality to reduce the size of the file)/// </summary>        /// <param name= "Srcbitmap" >the incoming Bitmap object</param>        /// <param name= "DestFile" >compressed picture save path</param>        /// <param name= "level" >compression level, 0 to 100, 0 worst quality, 100 best</param>         Public Static voidCompress (Bitmap Srcbitmap,stringDestFile,LongLevel ) {Stream s=NewFileStream (DestFile, FileMode.Create);            Compress (Srcbitmap, S, level);        S.close (); }

How to compress images in C #

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.