Set or Save Transparent Image in Java

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

import com.aspose.imaging.Image;
import com.aspose.imaging.fileformats.png.PngColorType;
import com.aspose.imaging.imageoptions.PngOptions;
String templatesFolder = "c:\\Users\\USER\\Downloads\\";
String inputFile = templatesFolder + "template.png";
String outputFile = templatesFolder + "output.png";
try (Image image = Image.load(inputFile))
{
image.save(outputFile, new PngOptions()
{{
setColorType(PngColorType.TruecolorWithAlpha);
}});
}

Example of conversion transparent tiff to transparent png

import com.aspose.imaging.Image;
import com.aspose.imaging.fileformats.png.PngColorType;
import com.aspose.imaging.imageoptions.PngOptions;
String templatesFolder = "c:\\Users\\USER\\Downloads\\";
String inputFile = templatesFolder + "template.tiff";
String outputFile = templatesFolder + "output.png";
try (Image image = Image.load(inputFile))
{
image.save(outputFile, new PngOptions()
{{
setColorType(PngColorType.TruecolorWithAlpha);
}});
}

Example of conversion transparent gif to transparent png

import com.aspose.imaging.Image;
import com.aspose.imaging.fileformats.png.PngColorType;
import com.aspose.imaging.imageoptions.PngOptions;
String templatesFolder = "c:\\Users\\USER\\Downloads\\";
String inputFile = templatesFolder + "template.gif";
String outputFile = templatesFolder + "output.png";
try (Image image = Image.load(inputFile))
{
image.save(outputFile, new PngOptions()
{{
setColorType(PngColorType.TruecolorWithAlpha);
}});
}

Example of conversion transparent webp to transparent png

import com.aspose.imaging.Image;
import com.aspose.imaging.fileformats.png.PngColorType;
import com.aspose.imaging.imageoptions.PngOptions;
String templatesFolder = "c:\\Users\\USER\\Downloads\\";
String inputFile = templatesFolder + "template.webp";
String outputFile = templatesFolder + "output.png";
try (Image image = Image.load(inputFile))
{
image.save(outputFile, new PngOptions()
{{
setColorType(PngColorType.TruecolorWithAlpha);
}});
}