侧边栏壁纸
博主头像
分享你我博主等级

行动起来,活在当下

  • 累计撰写 107 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

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

管理员
2021-04-16 / 0 评论 / 0 点赞 / 5 阅读 / 7031 字

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);


0

评论区