C#进行图片压缩(对jpg压缩效果最好)

2021-01-08 19:31

阅读:466

标签:object   open   ati   直接   ali   target   wpf   图片压缩   throw   

直接上代码

 1     public static class ImageCompress
 2     {
 3         /// 
 4         /// 图片压缩
 5         /// 
 6         /// 图片文件路径
 7         /// 保存文件夹
 8         /// 压缩质量
 9         /// 压缩后的文件名后缀(防止直接覆盖原文件)
10         public static void CompressionImage(string imagePath, string targetFolder, long quality = 100, string fileSuffix = "compress")
11         {
12             if (!File.Exists(imagePath))
13             {
14                 throw new FileNotFoundException();
15             }
16             if (!Directory.Exists(targetFolder))
17             {
18                 Directory.CreateDirectory(targetFolder);
19             }
20             var fileInfo = new FileInfo(imagePath);
21             var fileName = fileInfo.Name.Replace(fileInfo.Extension, "");
22             var fileFullName = Path.Combine($"{targetFolder}", $"{fileName}_{fileSuffix}{fileInfo.Extension}");
23 
24             var imageByte = CompressionImage(imagePath, quality);
25             var ms = new MemoryStream(imageByte);
26             var image = Image.FromStream(ms);
27             image.Save(fileFullName);
28             ms.Close();
29             ms.Dispose();
30             image.Dispose();
31         }
32         private static byte[] CompressionImage(string imagePath, long quality)
33         {
34             using (var fileStream = new FileStream(imagePath, FileMode.Open))
35             {
36                 using (var img =Image.FromStream(fileStream))
37                 {
38                     using (var bitmap = new Bitmap(img))
39                     {
40                         var codecInfo = GetEncoder(img.RawFormat);
41                         var myEncoder = Encoder.Quality;
42                         var myEncoderParameters = new EncoderParameters(1);
43                         var myEncoderParameter = new EncoderParameter(myEncoder, quality);
44                         myEncoderParameters.Param[0] = myEncoderParameter;
45                         using (var ms = new MemoryStream())
46                         {
47                             bitmap.Save(ms, codecInfo, myEncoderParameters);
48                             myEncoderParameters.Dispose();
49                             myEncoderParameter.Dispose();
50                             return ms.ToArray();
51                         }
52                     }
53                 }
54             }
55         }
56 
57         private static ImageCodecInfo GetEncoder(ImageFormat format)
58         {
59             var codecs = ImageCodecInfo.GetImageDecoders();
60             return codecs.FirstOrDefault(codec => codec.FormatID == format.Guid);
61         }
62     }

调用方法(WPF为例)

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var fileDialog = new OpenFileDialog();
            var fileSelect = "";
            if (fileDialog.ShowDialog(this)==true)
            {
                fileSelect = fileDialog.FileName;
            }
            if(string.IsNullOrWhiteSpace(fileSelect)) return;
            
            ImageCompress.CompressionImage(fileSelect,@"d:\",50);

        }

 

C#进行图片压缩(对jpg压缩效果最好)

标签:object   open   ati   直接   ali   target   wpf   图片压缩   throw   

原文地址:https://www.cnblogs.com/wdw984/p/13112621.html


评论


亲,登录后才可以留言!