Private voidCompressedimage (stringFileName,Longquality) {FileStream FS=NewFileStream (FileName, FileMode.Open, FileAccess.Read); Byte[] Bytepic=NewByte[fs. Length]; Fs. Read (Bytepic,0, bytepic.length); MemoryStream Stream=NewMemoryStream (bytepic); Bitmap BMP=(Bitmap) Image.fromstream (stream); ImageCodecInfo Myimagecodecinfo= Imagecodecinfo.getimageencoders () [1];//If the following traversal does not have this image format, itdefault to JPEGimagecodecinfo[] Encoders =imagecodecinfo.getimageencoders (); for(intj =0; J < Encoders. Length; J + +) { if(Encoders[j]. MimeType = ="Image/jpeg") {Myimagecodecinfo=Encoders[j]; Break; }} System.Drawing.Imaging.Encoder Myencoder= System.Drawing.Imaging.Encoder.Quality;//the quality to be manipulatedEncoderParameters myencoderparameters =NewEncoderParameters (1);//a member, dealing only with qualityEncoderparameter Myencoderparameter =NewEncoderparameter (myencoder, quality);//0 is the worst quality, 100 is the best, note is a long typemyencoderparameters.param[0] =Myencoderparameter; Size s=NewSize (BMP. Width, BMP. Height); Bitmap newbmp=NewBitmap (BMP, S); MemoryStream Ms=NewMemoryStream (); Newbmp.save (MS, Myimagecodecinfo, myencoderparameters); //the compressed stream is saved to Ms//restore a picture from a streamImage image =Image.fromstream (MS); stringCurdirectory = Path.getdirectoryname (assembly.getcallingassembly (). GetModules () [0]. FullyQualifiedName) +"\\"; //Save PictureImage. Save (Curdirectory +"pic.jpg"); Fs. Dispose (); Stream. Dispose (); Newbmp.dispose (); Ms. Dispose (); }
Demo
. NET compressed picture quality (demo included)