public class PngImage extends RasterCachedImage implements com.aspose.fileformats.core.interfaces.IInterlaced
Manipulate Portable Network Graphics (PNG) raster images with our versatile API, featuring support for compression levels and various color depths including Grayscale, Indexed Color, TrueColor, and alpha channels. Seamlessly process XMP metadata, enabling comprehensive image metadata management, while easily loading PNG images, performing diverse manipulations, applying filters, and converting images to other file formats for optimal versatility and customization.
This example shows how to load a PNG image from a file.
String dir = "c:\\temp\\";
// Load a PNG image from a file.
com.aspose.imaging.fileformats.png.PngImage pngImage = new com.aspose.imaging.fileformats.png.PngImage(dir + "sample.png");
try {
// Transform the image to grayscale representation
pngImage.grayscale();
// Save to a file.
pngImage.save(dir + "sample.grayscale.png");
} finally {
pngImage.dispose();
}
| Constructor and Description |
|---|
PngImage(InputStream stream)
Creates a new instance of the
PngImage class by initializing it
with a stream. |
PngImage(int width,
int height)
Initialize a new object of the
PngImage class by providing the
width and height parameters. |
PngImage(int width,
int height,
int colorType)
Instantiate a fresh instance of the
PngImage class, specifying
the desired width, height, and color type parameters. |
PngImage(PngOptions pngOptions,
int width,
int height)
Initialize a new instance of the
PngImage class, incorporating
PNG options alongside width and height parameters. |
PngImage(RasterImage rasterImage)
Creates a new instance of the
PngImage class by providing a raster
image as a parameter. |
PngImage(RasterImage rasterImage,
int colorType)
Creates a new instance of the
PngImage class by specifying a
raster image and a color type. |
PngImage(String path)
Constructs a new instance of the
PngImage class using the path
parameter to specify the location of the image file to load. |
PngImage(String path,
int colorType)
Initializes a new instance of the
PngImage class by specifying the
path to the image file and the color type. |
| Modifier and Type | Method and Description |
|---|---|
Color |
getBackgroundColor()
Retrieves the background color of the image, if one is specified.
|
int |
getBitsPerPixel()
Retrieve the bits per pixel value for the image.
|
ImageOptionsBase |
getDefaultOptions(Object[] args)
Gets the default options.
|
long |
getFileFormat()
Retrieves the format of the file associated with the image instance.
|
int |
getHeight()
Obtain the height of the image in pixels.
|
double |
getHorizontalResolution()
Retrieve or modify the horizontal resolution of the image.
|
boolean |
getInterlaced()
Retrieves a boolean value indicating whether the
PngImage is
interlaced, which determines if the image data is stored in a progressive
manner for faster loading or transmission. |
ImageOptionsBase |
getOriginalOptions()
Gets the options based on the original file settings.
|
PixelDataFormat |
getRawDataFormat()
Accesses the raw data format of the image.
|
Color |
getTransparentColor()
Retrieves the transparent color of the image, if it exists.
|
double |
getVerticalResolution()
Provides access to the vertical resolution of the image.
|
int |
getWidth()
Allows retrieval of the width of the image in pixels, providing essential information
about its dimensions.
|
XmpPacketWrapper |
getXmpData()
Accesses or modifies the Extensible Metadata Platform (XMP) metadata associated
with the image.
|
boolean |
hasAlpha()
Returns a boolean value indicating whether the image has an alpha channel,
which determines its transparency.
|
boolean |
hasBackgroundColor()
Retrieves a boolean value indicating whether the image has a background color.
|
boolean |
hasTransparentColor()
Provides a boolean value indicating whether the image contains a transparent
color.
|
boolean |
isInterlaced()
Gets a value indicating whether this image instance is interlaced.
|
void |
setBackgroundColor(boolean value)
Retrieves a boolean value indicating whether the image has a background color.
|
void |
setBackgroundColor(Color value)
Retrieves the background color of the image, if one is specified.
|
void |
setHorizontalResolution(double value)
Retrieve or modify the horizontal resolution of the image.
|
void |
setTransparentColor(boolean value)
Provides a boolean value indicating whether the image contains a transparent
color.
|
void |
setTransparentColor(Color value)
Modifies the transparent color of the image, if it exists.
|
void |
setVerticalResolution(double value)
Provides access to the vertical resolution of the image.
|
void |
setXmpData(XmpPacketWrapper value)
Accesses or modifies the Extensible Metadata Platform (XMP) metadata associated
with the image.
|
adjustBrightness, adjustContrast, adjustGamma, adjustGamma, analyzePercentageDigitalSignature, autoBrightnessContrast, binarizeBradley, binarizeBradley, binarizeFixed, binarizeOtsu, blend, cacheData, crop, dither, embedDigitalSignature, grayscale, isCached, isDigitalSigned, normalizeHistogram, resize, resize, rotate, rotateFlipblend, blend, blend, dither, filter, getArgb32Pixel, getDefaultArgb32Pixels, getDefaultPixels, getDefaultRawData, getDefaultRawData, getImageOpacity, getModifyDate, getPixel, getPremultiplyComponents, getRawCustomColorConverter, getRawDataSettings, getRawFallbackIndex, getRawIndexedColorConverter, getRawLineSize, getSkewAngle, getUpdateXmpData, getUseRawData, isDigitalSigned, isRawDataAvailable, isUsePalette, loadArgb32Pixels, loadArgb64Pixels, loadCmyk32Pixels, loadCmykPixels, loadPartialArgb32Pixels, loadPartialArgb64Pixels, loadPartialPixels, loadPixels, loadRawData, loadRawData, normalizeAngle, normalizeAngle, readArgb32ScanLine, readScanLine, removeMetadata, replaceColor, replaceColor, replaceNonTransparentColors, replaceNonTransparentColors, rotate, saveArgb32Pixels, saveCmyk32Pixels, saveCmykPixels, savePixels, saveRawData, setArgb32Pixel, setPalette, setPixel, setPremultiplyComponents, setRawCustomColorConverter, setRawFallbackIndex, setRawIndexedColorConverter, setResolution, setUpdateXmpData, setUseRawData, toBitmap, writeArgb32ScanLine, writeScanLinecanLoad, canLoad, canLoad, canLoad, canSave, create, create, create, create, create, create, crop, getBounds, getBufferSizeHint, getContainer, getFileFormat, getFileFormat, getFittingRectangle, getFittingRectangle, getInterruptMonitor, getIProgressEventHandler, getPalette, getProgressEventHandlerInfo, getProportionalHeight, getProportionalWidth, getSerializedStream, getSize, isAutoAdjustPalette, load, load, load, load, load, load, resize, resizeHeightProportionally, resizeHeightProportionally, resizeHeightProportionally, resizeWidthProportionally, resizeWidthProportionally, resizeWidthProportionally, save, save, save, save, save, save, save, save, setAutoAdjustPalette, setBufferSizeHint, setInterruptMonitor, setPalettegetDataStreamContainer, save, save, saveclose, dispose, getDisposedpublic PngImage(int width,
int height)
Initialize a new object of the PngImage class by providing the
width and height parameters. This constructor simplifies the creation of PNG
images by allowing developers to specify the dimensions directly, facilitating
efficient management of PNG image data within their applications.
width - The width.height - The height.This example shows how to create a PNG image of the specified size, fill it with a solid color and save it to a file.
String dir = "c:\\temp\\";
// Create a PNG image of 100x100 px.
com.aspose.imaging.fileformats.png.PngImage pngImage = new com.aspose.imaging.fileformats.png.PngImage(100, 100);
{
// Do some image processing, e.g. fill the entire image in red.
com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(pngImage);
com.aspose.imaging.brushes.SolidBrush brush = new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed());
graphics.fillRectangle(brush, pngImage.getBounds());
// Save to a file.
pngImage.save(dir + "output.png");
}
public PngImage(String path)
Constructs a new instance of the PngImage class using the path
parameter to specify the location of the image file to load. This constructor
enables developers to conveniently create PNG images by loading them from a file,
simplifying the process of working with PNG images in their applications.
path - The path to load an image.This example shows how to load a PNG image from a file.
String dir = "c:\\temp\\";
// Load a PNG image from a file.
com.aspose.imaging.fileformats.png.PngImage pngImage = new com.aspose.imaging.fileformats.png.PngImage(dir + "sample.png");
try {
// Transform the image to grayscale representation
pngImage.grayscale();
// Save to a file.
pngImage.save(dir + "sample.grayscale.png");
} finally {
pngImage.dispose();
}
public PngImage(RasterImage rasterImage)
Creates a new instance of the PngImage class by providing a raster
image as a parameter. This constructor allows developers to directly initialize a
PNG image object using an existing raster image, streamlining the process of
working with PNG images in their applications.
rasterImage - The raster image.This example shows how to load PNG image from a BMP image.
String dir = "c:\\temp\\";
// Load a TrueColor PNG image from a BMP image.
// First, create a temporal BMP image that will be a foundation for building a PNG image.
// You can also load BMP image from a file or use an image of any other raster format.
com.aspose.imaging.fileformats.bmp.BmpImage bmpImage = new com.aspose.imaging.fileformats.bmp.BmpImage(100, 100);
try {
// Fill the entire BMP image in red.
com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(bmpImage);
com.aspose.imaging.brushes.SolidBrush brush = new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed());
gr.fillRectangle(brush, bmpImage.getBounds());
com.aspose.imaging.fileformats.png.PngImage pngImage = new com.aspose.imaging.fileformats.png.PngImage(bmpImage);
try {
System.out.println("The PNG color type: " + pngImage.getOriginalOptions());
pngImage.save(dir + "output.png");
} finally {
pngImage.dispose();
}
} finally {
bmpImage.dispose();
}
public PngImage(String path, int colorType)
Initializes a new instance of the PngImage class by specifying the
path to the image file and the color type. This constructor allows for convenient
creation of PNG images from files with different color types, providing
flexibility in handling various image formats.
path - The path to load an image.colorType - The color type.com.aspose.ms.System.ArgumentNullException - if path is nullThis example shows how to load a PNG image from a file with the specified color type.
String dir = "c:\\temp\\";
// Load a PNG image from a file.
// Note that the colorful image will be converted to grayscale automatically.
com.aspose.imaging.fileformats.png.PngImage pngImage = new com.aspose.imaging.fileformats.png.PngImage(
dir + "sample.png",
com.aspose.imaging.fileformats.png.PngColorType.Grayscale);
try {
// Save to a file.
pngImage.save(dir + "sample.grayscale.png");
} finally {
pngImage.dispose();
}
public PngImage(RasterImage rasterImage, int colorType)
Creates a new instance of the PngImage class by specifying a
raster image and a color type. This constructor enables developers to directly
convert raster images into PNG format while specifying the desired color type,
offering flexibility in color representation.
rasterImage - The raster image.colorType - The color type.PngColorTypeThis example shows how to load PNG image from a BMP image with the specified color type.
String dir = "c:\\temp\\";
// Load a grayscale PNG image from a colored BMP image.
// First, create a temporal BMP image that will be a foundation for building a PNG image.
// You can also load BMP image from a file or use an image of any other raster format.
com.aspose.imaging.fileformats.bmp.BmpImage bmpImage = new com.aspose.imaging.fileformats.bmp.BmpImage(100, 100);
try {
// Fill the entire BMP image in red.
com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(bmpImage);
com.aspose.imaging.brushes.SolidBrush brush = new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed());
gr.fillRectangle(brush, bmpImage.getBounds());
// The colors of the image pixels will be converted to their grayscale counterparts.
com.aspose.imaging.fileformats.png.PngImage pngImage = new com.aspose.imaging.fileformats.png.PngImage(
bmpImage,
com.aspose.imaging.fileformats.png.PngColorType.Grayscale);
try {
pngImage.save(dir + "output.grayscale.png");
} finally {
pngImage.dispose();
}
} finally {
bmpImage.dispose();
}
public PngImage(InputStream stream)
Creates a new instance of the PngImage class by initializing it
with a stream. This constructor allows developers to load PNG images directly
from a stream, providing flexibility in image retrieval from different sources.
stream - The stream to load an image.public PngImage(int width,
int height,
int colorType)
Instantiate a fresh instance of the PngImage class, specifying
the desired width, height, and color type parameters. This constructor enables
swift creation of PNG images with tailored dimensions and color configurations,
facilitating streamlined image generation for various applications and workflows.
width - The width.height - The height.colorType - The color type.PngColorTypeThis example shows how to create a PNG image of the specified size with the specified color type, fill it with a solid color and save it to a file.
String dir = "c:\\temp\\";
// Create a grayscale PNG image of 100x100 px.
// All colors will be automatically converted to the grayscale format.
com.aspose.imaging.fileformats.png.PngImage pngImage = new com.aspose.imaging.fileformats.png.PngImage(100, 100, com.aspose.imaging.fileformats.png.PngColorType.Grayscale);
try {
// Do some image processing, e.g. fill the entire image in red.
com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(pngImage);
com.aspose.imaging.brushes.SolidBrush brush = new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed());
graphics.fillRectangle(brush, pngImage.getBounds());
// Save to a file.
pngImage.save(dir + "output.grayscale.png");
} finally {
pngImage.dispose();
}
public PngImage(PngOptions pngOptions, int width, int height)
Initialize a new instance of the PngImage class, incorporating
PNG options alongside width and height parameters. This constructor empowers
developers to create PNG images with customizable settings and dimensions,
offering flexibility in image generation for diverse use cases.
pngOptions - The png options.width - The width.height - The height.This example shows how to create a PNG image with the specified options, fill it with a linear gradient colors and save it to a file.
String dir = "c:\\temp\\";
com.aspose.imaging.imageoptions.PngOptions createOptions = new com.aspose.imaging.imageoptions.PngOptions();
// The number of bits per color channel
createOptions.setBitDepth((byte) 8);
// Each pixel is a (red, green, blue) triple followed by the alpha component.
createOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.TruecolorWithAlpha);
// The maximum level of compression.
createOptions.setCompressionLevel(9);
// Usage of filters allows to compress continuous tonal images more effectively.
createOptions.setFilterType(com.aspose.imaging.fileformats.png.PngFilterType.Sub);
// Use progressive loading
createOptions.setProgressive(true);
// Create a PNG image with custom parameters.
com.aspose.imaging.fileformats.png.PngImage pngImage = new com.aspose.imaging.fileformats.png.PngImage(createOptions, 100, 100);
try {
com.aspose.imaging.brushes.LinearGradientBrush gradientBrush = new com.aspose.imaging.brushes.LinearGradientBrush(
new com.aspose.imaging.Point(0, 0),
new com.aspose.imaging.Point(pngImage.getWidth(), pngImage.getHeight()),
com.aspose.imaging.Color.getBlue(),
com.aspose.imaging.Color.getTransparent());
com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(pngImage);
// Fill the image with a semi-transparent gradient.
graphics.fillRectangle(gradientBrush, pngImage.getBounds());
// Save to a file.
pngImage.save(dir + "output.explicitoptions.png");
} finally {
pngImage.dispose();
}
public int getBitsPerPixel()
Retrieve the bits per pixel value for the image. This property provides crucial information regarding the color depth of the image, enabling developers to understand the level of detail and color accuracy present in the image data.
getBitsPerPixel in class Imagepublic int getHeight()
Obtain the height of the image in pixels. This property returns the vertical dimension of the image, allowing developers to determine its size in pixels along the vertical axis.
getHeight in interface IObjectWithBoundsgetHeight in class Imagepublic double getHorizontalResolution()
Retrieve or modify the horizontal resolution of the image. This property represents the number of pixels per inch along the horizontal axis of the image. Adjusting this resolution can affect the physical size of the image when printed or displayed.
getHorizontalResolution in class RasterImageNote by default this value is always 96 since different platforms cannot return the screen resolution. You may consider using the SetResolution method for updating both resolution values in single call.
The following example shows how to set horizontal/vertical resolution of a PNG image.
String dir = "c:\\temp\\";
com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
try {
com.aspose.imaging.fileformats.png.PngImage pngImage = (com.aspose.imaging.fileformats.png.PngImage) image;
// Get horizontal and vertical resolution of the PngImage.
double horizontalResolution = pngImage.getHorizontalResolution();
double verticalResolution = pngImage.getVerticalResolution();
System.out.println("The horizontal resolution, in pixels per inch: " + horizontalResolution);
System.out.println("The vertical resolution, in pixels per inch: " + verticalResolution);
if (horizontalResolution != 96.0 || verticalResolution != 96.0) {
// Use the SetResolution method for updating both resolution values in a single call.
System.out.println("Set resolution values to 96 dpi");
pngImage.setResolution(96.0, 96.0);
System.out.println("The horizontal resolution, in pixels per inch: " + pngImage.getHorizontalResolution());
System.out.println("The vertical resolution, in pixels per inch: " + pngImage.getVerticalResolution());
}
} finally {
image.dispose();
}
//The output may look like this:
//The horizontal resolution, in pixels per inch: 96.0
//The vertical resolution, in pixels per inch: 96.0
public void setHorizontalResolution(double value)
Retrieve or modify the horizontal resolution of the image. This property represents the number of pixels per inch along the horizontal axis of the image. Adjusting this resolution can affect the physical size of the image when printed or displayed.
setHorizontalResolution in class RasterImagevalue - The horizontal resolution.
Note by default this value is always 96 since different platforms cannot return the screen resolution. You may consider using the SetResolution method for updating both resolution values in single call.
public long getFileFormat()
Retrieves the format of the file associated with the image instance. This property provides essential information regarding the file type, enabling efficient handling and processing based on the specific format requirements.
getFileFormat in class ImageFileFormatpublic PixelDataFormat getRawDataFormat()
Accesses the raw data format of the image. This property provides insight into how the image data is structured internally, which can be useful for advanced image processing tasks or format conversion.
getRawDataFormat in class RasterImagePixelDataFormatThe following example loads PNG images and prints information about raw data format and alpha channel.
// The PNG images to load.
String[] fileNames = new String[]
{
"c:\\temp\\sample.png",
"c:\\temp\\alpha.png",
};
for (String fileName : fileNames) {
com.aspose.imaging.Image image = com.aspose.imaging.Image.load(fileName);
try {
com.aspose.imaging.fileformats.png.PngImage pngImage = (com.aspose.imaging.fileformats.png.PngImage) image;
System.out.printf("ImageFile=%s, FileFormat=%s, HasAlpha=%s", fileName, pngImage.getRawDataFormat(), pngImage.hasAlpha());
} finally {
image.dispose();
}
}
// The output may look like this:
// ImageFile=c:\temp\sample.png, FileFormat=Rgb24Bpp, used channels: 8,8,8, HasAlpha=False
// ImageFile=c:\temp\alpha.png, FileFormat=RGBA32Bpp, used channels: 8,8,8,8, HasAlpha=True
public double getVerticalResolution()
Provides access to the vertical resolution of the image. Developers can use this property to retrieve or modify the resolution setting, which indicates the number of pixels per inch (PPI) along the vertical axis of the image.
getVerticalResolution in class RasterImageNote by default this value is always 96 since different platforms cannot return the screen resolution. You may consider using the SetResolution method for updating both resolution values in single call.
The following example shows how to set horizontal/vertical resolution of a PNG image.
String dir = "c:\\temp\\";
com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
try {
com.aspose.imaging.fileformats.png.PngImage pngImage = (com.aspose.imaging.fileformats.png.PngImage) image;
// Get horizontal and vertical resolution of the PngImage.
double horizontalResolution = pngImage.getHorizontalResolution();
double verticalResolution = pngImage.getVerticalResolution();
System.out.println("The horizontal resolution, in pixels per inch: " + horizontalResolution);
System.out.println("The vertical resolution, in pixels per inch: " + verticalResolution);
if (horizontalResolution != 96.0 || verticalResolution != 96.0) {
// Use the SetResolution method for updating both resolution values in a single call.
System.out.println("Set resolution values to 96 dpi");
pngImage.setResolution(96.0, 96.0);
System.out.println("The horizontal resolution, in pixels per inch: " + pngImage.getHorizontalResolution());
System.out.println("The vertical resolution, in pixels per inch: " + pngImage.getVerticalResolution());
}
} finally {
image.dispose();
}
//The output may look like this:
//The horizontal resolution, in pixels per inch: 96.0
//The vertical resolution, in pixels per inch: 96.0
public void setVerticalResolution(double value)
Provides access to the vertical resolution of the image. Developers can use this property to retrieve or modify the resolution setting, which indicates the number of pixels per inch (PPI) along the vertical axis of the image.
setVerticalResolution in class RasterImagevalue - The vertical resolution.
Note by default this value is always 96 since different platforms cannot return the screen resolution. You may consider using the SetResolution method for updating both resolution values in single call.
public int getWidth()
Allows retrieval of the width of the image in pixels, providing essential information about its dimensions. This property is frequently used by developers to determine the image's width, enabling them to perform various operations based on its size
getWidth in interface IObjectWithBoundsgetWidth in class Imagepublic boolean hasTransparentColor()
Provides a boolean value indicating whether the image contains a transparent color. This property is crucial for applications that need to handle transparency, allowing developers to determine whether additional processing is required to handle transparent regions in the image.
hasTransparentColor in class RasterImagepublic boolean hasAlpha()
Returns a boolean value indicating whether the image has an alpha channel, which determines its transparency. This property is useful for applications that need to handle transparency, allowing developers to determine whether additional processing is required to handle transparent areas in the image.
hasAlpha in class RasterImagetrue if this instance has alpha; otherwise, false.The following example shows how to check if a PNG image supports alpha-channel.
// Helper class
class Utils {
public String getPngColorTypeString(int colorType) {
switch (colorType) {
case com.aspose.imaging.fileformats.png.PngColorType.Grayscale:
return "Grayscale";
case com.aspose.imaging.fileformats.png.PngColorType.Truecolor:
return "Truecolor";
case com.aspose.imaging.fileformats.png.PngColorType.IndexedColor:
return "IndexedColor";
case com.aspose.imaging.fileformats.png.PngColorType.GrayscaleWithAlpha:
return "GrayscaleWithAlpha";
case com.aspose.imaging.fileformats.png.PngColorType.TruecolorWithAlpha:
return "TruecolorWithAlpha";
default:
throw new IllegalArgumentException("colorType");
}
}
}
// Here is the main example
Utils utils = new Utils();
// Get all supported PNG color types.
java.lang.Long[] colorTypes = com.aspose.imaging.fileformats.png.PngColorType.getValues(com.aspose.imaging.fileformats.png.PngColorType.class);
for (java.lang.Long colorType : colorTypes) {
com.aspose.imaging.imageoptions.PngOptions createOptions = new com.aspose.imaging.imageoptions.PngOptions();
createOptions.setSource(new com.aspose.imaging.sources.StreamSource(new java.io.ByteArrayInputStream(new byte[0])));
createOptions.setColorType(colorType.intValue());
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions, 100, 100);
try {
com.aspose.imaging.fileformats.png.PngImage pngImage = (com.aspose.imaging.fileformats.png.PngImage) image;
if (pngImage.hasAlpha()) {
System.out.printf("A %s PNG image supports alpha channel\r\n", utils.getPngColorTypeString(createOptions.getColorType()));
} else {
System.out.printf("A %s PNG image doesn't support alpha channel\r\n", utils.getPngColorTypeString(createOptions.getColorType()));
}
} finally {
image.dispose();
}
}
// The output looks like this:
// A Grayscale PNG image doesn't support alpha channel
// A Truecolor PNG image doesn't support alpha channel
// A IndexedColor PNG image doesn't support alpha channel
// A GrayscaleWithAlpha PNG image supports alpha channel
// A TruecolorWithAlpha PNG image supports alpha channel
public Color getTransparentColor()
Retrieves the transparent color of the image, if it exists. This property is valuable for applications requiring precise handling of transparent areas within images, allowing developers to access and manipulate the specific transparent color used.
getTransparentColor in class RasterImagepublic void setTransparentColor(boolean value)
Provides a boolean value indicating whether the image contains a transparent color. This property is crucial for applications that need to handle transparency, allowing developers to determine whether additional processing is required to handle transparent regions in the image.
setTransparentColor in class RasterImageThe following example shows how to set fully transparent colors for a part of a TrueColor PNG image which doesn't support alpha channel.
com.aspose.imaging.imageoptions.PngOptions createOptions = new com.aspose.imaging.imageoptions.PngOptions();
createOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("c:\\temp\\transparent.png", false));
createOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.Truecolor);
// Create a TrueColor PNG image of 100x100 px.
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions, 100, 100);
try {
com.aspose.imaging.fileformats.png.PngImage pngImage = (com.aspose.imaging.fileformats.png.PngImage) image;
com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(pngImage);
// All red pixels will be considered as fully transparent.
pngImage.setTransparentColor(com.aspose.imaging.Color.getRed());
pngImage.setTransparentColor(true);
// All transparent pixels will have a background color.
pngImage.setBackgroundColor(com.aspose.imaging.Color.getGreen());
pngImage.setBackgroundColor(true);
// Fill the entire image with white color.
gr.fillRectangle(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getWhite()), pngImage.getBounds());
// Fill the top-left quarter of the image with the transparent color.
// This makes the top-left quarter colored in the background color.
com.aspose.imaging.Rectangle rect = new com.aspose.imaging.Rectangle(0, 0, pngImage.getWidth() / 2, pngImage.getHeight() / 2);
gr.fillRectangle(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed()), rect);
pngImage.save();
} finally {
image.dispose();
}
public void setTransparentColor(Color value)
Modifies the transparent color of the image, if it exists. This property is valuable for applications requiring precise handling of transparent areas within images, allowing developers to access and manipulate the specific transparent color used.
setTransparentColor in class RasterImageThe following example shows how to set fully transparent colors for a part of a TrueColor PNG image which doesn't support alpha channel.
com.aspose.imaging.imageoptions.PngOptions createOptions = new com.aspose.imaging.imageoptions.PngOptions();
createOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("c:\\temp\\transparent.png", false));
createOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.Truecolor);
// Create a TrueColor PNG image of 100x100 px.
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions, 100, 100);
try {
com.aspose.imaging.fileformats.png.PngImage pngImage = (com.aspose.imaging.fileformats.png.PngImage) image;
com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(pngImage);
// All red pixels will be considered as fully transparent.
pngImage.setTransparentColor(com.aspose.imaging.Color.getRed());
pngImage.setTransparentColor(true);
// All transparent pixels will have a background color.
pngImage.setBackgroundColor(com.aspose.imaging.Color.getGreen());
pngImage.setBackgroundColor(true);
// Fill the entire image with white color.
gr.fillRectangle(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getWhite()), pngImage.getBounds());
// Fill the top-left quarter of the image with the transparent color.
// This makes the top-left quarter colored in the background color.
com.aspose.imaging.Rectangle rect = new com.aspose.imaging.Rectangle(0, 0, pngImage.getWidth() / 2, pngImage.getHeight() / 2);
gr.fillRectangle(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed()), rect);
pngImage.save();
} finally {
image.dispose();
}
public boolean hasBackgroundColor()
Retrieves a boolean value indicating whether the image has a background color. This property is useful for applications needing to determine if an image includes a background color, which can be important for various processing tasks such as compositing, rendering, or exporting.
hasBackgroundColor in class Imagepublic Color getBackgroundColor()
Retrieves the background color of the image, if one is specified. This property is helpful for applications that need to identify and potentially manipulate the background color of an image.
getBackgroundColor in class Imagepublic void setBackgroundColor(boolean value)
Retrieves a boolean value indicating whether the image has a background color. This property is useful for applications needing to determine if an image includes a background color, which can be important for various processing tasks such as compositing, rendering, or exporting.
setBackgroundColor in class ImageThe following example shows how to set fully transparent colors for a part of a TrueColor PNG image which doesn't support alpha channel.
com.aspose.imaging.imageoptions.PngOptions createOptions = new com.aspose.imaging.imageoptions.PngOptions();
createOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("c:\\temp\\transparent.png", false));
createOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.Truecolor);
// Create a TrueColor PNG image of 100x100 px.
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions, 100, 100);
try {
com.aspose.imaging.fileformats.png.PngImage pngImage = (com.aspose.imaging.fileformats.png.PngImage) image;
com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(pngImage);
// All red pixels will be considered as fully transparent.
pngImage.setTransparentColor(com.aspose.imaging.Color.getRed());
pngImage.setTransparentColor(true);
// All transparent pixels will have a background color.
pngImage.setBackgroundColor(com.aspose.imaging.Color.getGreen());
pngImage.setBackgroundColor(true);
// Fill the entire image with white color.
gr.fillRectangle(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getWhite()), pngImage.getBounds());
// Fill the top-left quarter of the image with the transparent color.
// This makes the top-left quarter colored in the background color.
com.aspose.imaging.Rectangle rect = new com.aspose.imaging.Rectangle(0, 0, pngImage.getWidth() / 2, pngImage.getHeight() / 2);
gr.fillRectangle(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed()), rect);
pngImage.save();
} finally {
image.dispose();
}
public void setBackgroundColor(Color value)
Retrieves the background color of the image, if one is specified. This property is helpful for applications that need to identify and potentially manipulate the background color of an image.
setBackgroundColor in class ImageThe following example shows how to set fully transparent colors for a part of a TrueColor PNG image which doesn't support alpha channel.
com.aspose.imaging.imageoptions.PngOptions createOptions = new com.aspose.imaging.imageoptions.PngOptions();
createOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("c:\\temp\\transparent.png", false));
createOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.Truecolor);
// Create a TrueColor PNG image of 100x100 px.
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions, 100, 100);
try {
com.aspose.imaging.fileformats.png.PngImage pngImage = (com.aspose.imaging.fileformats.png.PngImage) image;
com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(pngImage);
// All red pixels will be considered as fully transparent.
pngImage.setTransparentColor(com.aspose.imaging.Color.getRed());
pngImage.setTransparentColor(true);
// All transparent pixels will have a background color.
pngImage.setBackgroundColor(com.aspose.imaging.Color.getGreen());
pngImage.setBackgroundColor(true);
// Fill the entire image with white color.
gr.fillRectangle(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getWhite()), pngImage.getBounds());
// Fill the top-left quarter of the image with the transparent color.
// This makes the top-left quarter colored in the background color.
com.aspose.imaging.Rectangle rect = new com.aspose.imaging.Rectangle(0, 0, pngImage.getWidth() / 2, pngImage.getHeight() / 2);
gr.fillRectangle(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed()), rect);
pngImage.save();
} finally {
image.dispose();
}
public boolean getInterlaced()
Retrieves a boolean value indicating whether the PngImage is
interlaced, which determines if the image data is stored in a progressive
manner for faster loading or transmission.
true if interlaced; otherwise, false.public final boolean isInterlaced()
Gets a value indicating whether this image instance is interlaced.
Value:true if this image instance is interlaced; otherwise, false.isInterlaced in interface com.aspose.fileformats.core.interfaces.IInterlacedpublic XmpPacketWrapper getXmpData()
Accesses or modifies the Extensible Metadata Platform (XMP) metadata associated with the image. This property facilitates the extraction, modification, or addition of metadata, enabling comprehensive management of image-related information.
getXmpData in interface IHasXmpDatagetXmpData in class RasterImagepublic void setXmpData(XmpPacketWrapper value)
Accesses or modifies the Extensible Metadata Platform (XMP) metadata associated with the image. This property facilitates the extraction, modification, or addition of metadata, enabling comprehensive management of image-related information.
setXmpData in interface IHasXmpDatasetXmpData in class RasterImagevalue - The XMP metadata.public ImageOptionsBase getDefaultOptions(Object[] args)
Gets the default options.
getDefaultOptions in class Imageargs - The arguments.public ImageOptionsBase getOriginalOptions()
Gets the options based on the original file settings.
This can be helpful to keep bit-depth and other parameters of the original image unchanged.
For example, if we load a black-white PNG image with 1 bit per pixel and then save it using the
DataStreamSupporter.Save(string) method, the output PNG image with 8-bit per pixel will be produced.
To avoid it and save PNG image with 1-bit per pixel, use this method to get corresponding saving options and pass them
to the Image.Save(string, ImageOptionsBase) method as the second parameter.
getOriginalOptions in class ImageCopyright (c) 2008-2025 Aspose Pty Ltd. All Rights Reserved.