.net图片压缩

1.png很难进行压缩,一般压缩时间较长。

2.jpg图片压缩方法:

 #region  图片压缩【之压缩jpg】

        public static void JpgImgZip(Image img,string filePath)

        {

             //以下代码为保存图片时,设置压缩质量

            EncoderParameters ep = new EncoderParameters();

            long[] qy = new long[1];

            qy[0] =60;//设置压缩的比例1-100  

            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);

            ep.Param[0] = eParam;

            try

            {

                ImageCodecInfo jpegICIinfo = GetEncoderInfo("JPG");

                if (jpegICIinfo != null)

                {

                    img.Save(filePath, jpegICIinfo, ep);//dFile是压缩后的新路径 

                }

                else

                {

                    img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                }

            }

            catch(Exception)

            {

                img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

            }

        }

        public static ImageCodecInfo GetEncoderInfo(String fileExtenUper)

        {

            int j;

            ImageCodecInfo[] encoders;

            encoders = ImageCodecInfo.GetImageEncoders();

            for (j = 0; j < encoders.Length; ++j)

            {

                if (encoders[j].FilenameExtension.Contains(fileExtenUper))

                {

                    return encoders[j];

                }

            }

            return null;

        }

        #endregion

  

 

你可能感兴趣的:(.net)