Set or Save Transparent Image in Java
Contents
[
Hide
]
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
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
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
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
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
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
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
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
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); | |
}}); | |
} |