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

时间:2024-05-18 09:35:10
【文件属性】:

文件名称:.net core 使用Pngquant进行PNG图片压缩

文件大小:64KB

文件格式:RAR

更新时间:2024-05-18 09:35:10

Pngquant C# .netcore

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


【文件预览】:
Com.macao.electron.Pngquant
----bin()
--------Debug()
----obj()
--------project.nuget.cache(11KB)
--------Com.macao.electron.Pngquant.csproj.nuget.dgspec.json(3KB)
--------project.assets.json(206KB)
--------Debug()
--------Com.macao.electron.Pngquant.csproj.nuget.g.targets(294B)
--------Com.macao.electron.Pngquant.csproj.nuget.g.props(1KB)
----Compressor.cs(710B)
----PngquantConfig.cs(1KB)
----JpegOptimOptions.cs(2KB)
----Com.macao.electron.Pngquant.csproj(311B)
----PngQuantOptions.cs(3KB)

网友评论