在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 });
}
}
使用:
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] byData = new byte[fs.Length];
//设置压缩选项
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(byData);
MemoryStream ms = new MemoryStream(compressed); //把那个byte[] 数组传进去, 然后
using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write))
ms.WriteTo(fs);
1