zoukankan      html  css  js  c++  java
  • C#进行图片压缩

    https://www.cnblogs.com/wdw984/p/13112621.html感谢老哥 我是代码搬运工

    直接上代码

    public static class ImageCompress
        {
            /// <summary>
            /// 图片压缩
            /// </summary>
            /// <param name="imagePath">图片文件路径</param>
            /// <param name="targetFolder">保存文件夹</param>
            /// <param name="quality">压缩质量</param>
            /// <param name="fileSuffix">压缩后的文件名后缀(防止直接覆盖原文件)</param>
            public static void CompressionImage(string imagePath, string targetFolder, long quality = 100, string fileSuffix = "compress")
            {
                if (!File.Exists(imagePath))
                {
                    throw new FileNotFoundException();
                }
                if (!Directory.Exists(targetFolder))
                {
                    Directory.CreateDirectory(targetFolder);
                }
                var fileInfo = new FileInfo(imagePath);
                var fileName = fileInfo.Name.Replace(fileInfo.Extension, "");
                var fileFullName = Path.Combine($"{targetFolder}", $"{fileName}_{fileSuffix}{fileInfo.Extension}");
    
                var imageByte = CompressionImage(imagePath, quality);
                var ms = new MemoryStream(imageByte);
                var image = Image.FromStream(ms);
                image.Save(fileFullName);
                ms.Close();
                ms.Dispose();
                image.Dispose();
            }
            private static byte[] CompressionImage(string imagePath, long quality)
            {
                using (var fileStream = new FileStream(imagePath, FileMode.Open))
                {
                    using (var img =Image.FromStream(fileStream))
                    {
                        using (var bitmap = new Bitmap(img))
                        {
                            var codecInfo = GetEncoder(img.RawFormat);
                            var myEncoder = Encoder.Quality;
                            var myEncoderParameters = new EncoderParameters(1);
                            var myEncoderParameter = new EncoderParameter(myEncoder, quality);
                            myEncoderParameters.Param[0] = myEncoderParameter;
                            using (var ms = new MemoryStream())
                            {
                                bitmap.Save(ms, codecInfo, myEncoderParameters);
                                myEncoderParameters.Dispose();
                                myEncoderParameter.Dispose();
                                return ms.ToArray();
                            }
                        }
                    }
                }
            }
    
            private static ImageCodecInfo GetEncoder(ImageFormat format)
            {
                var codecs = ImageCodecInfo.GetImageDecoders();
                return codecs.FirstOrDefault(codec => codec.FormatID == format.Guid);
            }
        }

    调用方法(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);
    
            }
  • 相关阅读:
    Leveldb Advanced
    loadrunner生成随机数
    用strtok函数分割字符串
    loadrunner关联及web_reg_save_param方法浅析
    Linux中find用法
    在LoadRunner中从数组类型的参数随机取值的方法
    LoadRunner可以把关联取值当作检查点来使用
    Oracle特殊字符转义:&amp;和&#39;
    LoadRunner 11 error:Cannot initialize driver dll
    用SecureCRT在windows和CentOS间上传下载文件
  • 原文地址:https://www.cnblogs.com/ning-xiaowo/p/13153594.html
Copyright © 2011-2022 走看看