C# 如何进行图像的压缩

2021-04-18 03:26

阅读:643

标签:eve   oid   style   class   pre   body   uid   cbitmap   long   

从网上找的非常有效。图片3M到500k

   private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

        /// 
        /// 图片压缩(降低质量以减小文件的大小)
        /// 
        /// 传入的Bitmap对象
        /// 压缩后的Stream对象
        /// 压缩等级,0到100,0 最差质量,100 最佳
        public static void Compress(Bitmap srcBitmap, Stream destStream, long level)
        {
            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 has an array of EncoderParameter
            // objects. In this case, there is only one

            // EncoderParameter object in the array.
            myEncoderParameters = new EncoderParameters(1);

            // Save the bitmap as a JPEG file with 给定的 quality level
            myEncoderParameter = new EncoderParameter(myEncoder, level);
            myEncoderParameters.Param[0] = myEncoderParameter;
            srcBitmap.Save(destStream, myImageCodecInfo, myEncoderParameters);
        }

        /// 
        /// 图片压缩(降低质量以减小文件的大小)
        /// 
        /// 传入的Bitmap对象
        /// 压缩后的图片保存路径
        /// 压缩等级,0到100,0 最差质量,100 最佳
        public static void Compress(Bitmap srcBitMap, string destFile, long level)
        {
            Stream s = new FileStream(destFile, FileMode.Create);
            Compress(srcBitMap, s, level);
            s.Close();
        }

 

C# 如何进行图像的压缩

标签:eve   oid   style   class   pre   body   uid   cbitmap   long   

原文地址:https://www.cnblogs.com/sexintercourse/p/8719676.html


评论


亲,登录后才可以留言!