Reduce file size in png
Contents
[
Hide
]
Reduce file size in png
Issue : Reduce file size in png.
Tips : To reduce file size in png there can be used compression level property and setting of pallette.
Example :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Emf; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Masking; | |
using Aspose.Imaging.Masking.Options; | |
using Aspose.Imaging.Masking.Result; | |
using Aspose.Imaging.Sources; | |
using Aspose.Imaging; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads"; | |
CompressPng(); | |
void CompressPng() | |
{ | |
var inputFile = Path.Combine(templatesFolder, $"template.png"); | |
var outputFile = Path.Combine(templatesFolder, $"compressed_png.png"); | |
using (var image = Image.Load(inputFile)) | |
{ | |
image.Save(outputFile, new PngOptions | |
{ | |
CompressionLevel = 9, | |
Progressive = true, | |
ColorType = PngColorType.IndexedColor, | |
Palette = ColorPaletteHelper.GetCloseImagePalette((RasterImage)image, 1 << 5) | |
}); | |
} | |
File.Delete(outputFile); | |
} |