.net core 使用Pngquant进行PNG图片压缩

Com.macao.electron.Pngquant.rar

在Startup.cs文件中配置Pngquant所在路径

  public void ConfigureServices(IServiceCollection services)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
        
                var pngquant = Configuration.GetSection("PngquantPathLinux").Value;
                PngquantConfig.Configure(new PngquantOptions { BinaryFolder = pngquant });
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
                var pngquant = Configuration.GetSection("PngquantPathWin").Value;
                PngquantConfig.Configure(new PngquantOptions { BinaryFolder = pngquant });
            }
        }

使用:

 byte[] stream = await page.ScreenshotDataAsync(
                                      new ScreenshotOptions
                                      {
                                          Type = ScreenshotType.Png,
                                      }
                                  );
                              //设置压缩选项
                              var options = new PngQuantOptions()
                              {
                                  QualityMinMax = (65, 80), //Minimum = 65, Maximum = 80. Default null
                                  Speed = 1, //Value between 1 and 11. default 3. 
                                  IEBug = false, //Attempt to fix iebug. default false.
                                  Bit = 256 //bit-rate. default 256
                              };

                              ///Invoke the compressor
                              Compressor pngQuant = new PngQuant(options);

                              //Compress bytes
                              byte[] compressed = await pngQuant.Compress(stream);
                              MemoryStream ms = new MemoryStream(compressed); //把那个byte[] 数组传进去, 然后
                              using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write))
                                  ms.WriteTo(fs);


本文作者:admin

本文链接:https://www.javalc.com/post/61.html

版权声明:本篇文章于2021-04-16,由admin发表,转载请注明出处:分享你我。如有疑问,请联系我们

js数据类型判断

发表评论

取消
扫码支持
登录
用户名
密码
注册
用户名
密码(至少8位)
确认密码
昵称
邮箱(请填写常用邮箱)
获取邀请码
邀请码
验证码
找回密码
用户名
邮箱
※ 重置链接将发送到邮箱