Set or Save Transparent Image in C#

Save transparent image with transparency

Issue : Image transparency loss.

Tips : It is necessary to use ColorMode TrueColorWithAlpha to save png with transparency.

Example of conversion transparent png to transparent png

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads";
var inputFile = Path.Combine(templatesFolder, $"template.png");
var outputFile = Path.Combine(templatesFolder, $"output.png");
using (var image = (RasterImage)Aspose.Imaging.Image.Load(inputFile))
{
image.Save(
outputFile,
new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha });
}
File.Delete(outputFile);

Example of conversion transparent tiff to transparent png

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads";
var inputFile = Path.Combine(templatesFolder, $"template.tiff");
var outputFile = Path.Combine(templatesFolder, $"output.png");
using (var image = (RasterImage)Aspose.Imaging.Image.Load(inputFile))
{
image.Save(
outputFile,
new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha });
}
File.Delete(outputFile);

Example of conversion transparent gif to transparent png

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads";
var inputFile = Path.Combine(templatesFolder, $"template.gif");
var outputFile = Path.Combine(templatesFolder, $"output.png");
using (var image = (RasterImage)Aspose.Imaging.Image.Load(inputFile))
{
image.Save(
outputFile,
new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha });
}
File.Delete(outputFile);

Example of conversion transparent webp to transparent png

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads";
var inputFile = Path.Combine(templatesFolder, $"template.webp");
var outputFile = Path.Combine(templatesFolder, $"output.png");
using (var image = (RasterImage)Aspose.Imaging.Image.Load(inputFile))
{
image.Save(
outputFile,
new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha });
}
File.Delete(outputFile);