PngImage

Inheritance: java.lang.Object, com.aspose.imaging.DisposableObject, com.aspose.imaging.DataStreamSupporter, com.aspose.imaging.Image, com.aspose.imaging.RasterImage, com.aspose.imaging.RasterCachedImage

All Implemented Interfaces: com.aspose.fileformats.core.interfaces.IInterlaced

public class PngImage extends RasterCachedImage implements IInterlaced

The new png image.

Constructors

ConstructorDescription
PngImage(int width, int height)Initializes a new instance of the PngImage class.
PngImage(String path)Initializes a new instance of the PngImage class.
PngImage(RasterImage rasterImage)Initializes a new instance of the PngImage class.
PngImage(String path, int colorType)Initializes a new instance of the PngImage class.
PngImage(RasterImage rasterImage, int colorType)Initializes a new instance of the PngImage class.
PngImage(InputStream stream)Initializes a new instance of the PngImage class.
PngImage(int width, int height, int colorType)Initializes a new instance of the PngImage class.
PngImage(PngOptions pngOptions, int width, int height)Initializes a new instance of the PngImage class.

Methods

MethodDescription
getBitsPerPixel()Gets the bits per pixel.
getHeight()Gets the height.
getHorizontalResolution()Gets or sets the horizontal resolution.
setHorizontalResolution(double value)Gets or sets the horizontal resolution.
getFileFormat()Gets a value of file format
getRawDataFormat()Gets the raw data format.
getVerticalResolution()Gets or sets the vertical resolution.
setVerticalResolution(double value)Gets or sets the vertical resolution.
getWidth()Gets the width.
hasTransparentColor()Gets a value indicating whether image has transparent color.
setTransparentColor(boolean value)Gets a value indicating whether image has transparent color.
hasAlpha()Get a value indicating whether this instance has alpha.
getTransparentColor()Gets the transparent color.
setTransparentColor(Color value)Gets the transparent color.
hasBackgroundColor()Gets a value indicating whether has background color.
setBackgroundColor(boolean value)Gets a value indicating whether has background color.
getBackgroundColor()Gets the background color.
setBackgroundColor(Color value)Gets the background color.
getInterlaced()Gets a value indicating whether this PngImage is interlaced.
isInterlaced()Gets a value indicating whether this image instance is interlaced.
getXmpData()Gets or sets the XMP metadata.
setXmpData(XmpPacketWrapper value)Gets or sets the XMP metadata.
getDefaultOptions(Object[] args)Gets the default options.
getOriginalOptions()Gets the options based on the original file settings.

Example: 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();
}

PngImage(int width, int height)

public PngImage(int width, int height)

Initializes a new instance of the PngImage class.

Parameters:

ParameterTypeDescription
widthintThe width.
heightintThe height.

PngImage(String path)

public PngImage(String path)

Initializes a new instance of the PngImage class.

Parameters:

ParameterTypeDescription
pathjava.lang.StringThe path to load an image.

PngImage(RasterImage rasterImage)

public PngImage(RasterImage rasterImage)

Initializes a new instance of the PngImage class.

Parameters:

ParameterTypeDescription
rasterImageRasterImageThe raster image.

PngImage(String path, int colorType)

public PngImage(String path, int colorType)

Initializes a new instance of the PngImage class.

Parameters:

ParameterTypeDescription
pathjava.lang.StringThe path to load an image.
colorTypeintThe color type.

PngImage(RasterImage rasterImage, int colorType)

public PngImage(RasterImage rasterImage, int colorType)

Initializes a new instance of the PngImage class.

Parameters:

ParameterTypeDescription
rasterImageRasterImageThe raster image.
colorTypeintThe color type.

PngImage(InputStream stream)

public PngImage(InputStream stream)

Initializes a new instance of the PngImage class.

Parameters:

ParameterTypeDescription
streamjava.io.InputStreamThe stream to load an image.

PngImage(int width, int height, int colorType)

public PngImage(int width, int height, int colorType)

Initializes a new instance of the PngImage class.

Parameters:

ParameterTypeDescription
widthintThe width.
heightintThe height.
colorTypeintThe color type.

PngImage(PngOptions pngOptions, int width, int height)

public PngImage(PngOptions pngOptions, int width, int height)

Initializes a new instance of the PngImage class.

Parameters:

ParameterTypeDescription
pngOptionsPngOptionsThe png options.
widthintThe width.
heightintThe height.

getBitsPerPixel()

public int getBitsPerPixel()

Gets the bits per pixel.

Returns: int

getHeight()

public int getHeight()

Gets the height.

Returns: int

getHorizontalResolution()

public double getHorizontalResolution()

Gets or sets the horizontal resolution.

Returns: double

Example: 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

setHorizontalResolution(double value)

public void setHorizontalResolution(double value)

Gets or sets the horizontal resolution.

Parameters:

ParameterTypeDescription
valuedouble

getFileFormat()

public long getFileFormat()

Gets a value of file format

Returns: long

getRawDataFormat()

public PixelDataFormat getRawDataFormat()

Gets the raw data format.

Returns: PixelDataFormat

Example: The 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

getVerticalResolution()

public double getVerticalResolution()

Gets or sets the vertical resolution.

Returns: double

Example: 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

setVerticalResolution(double value)

public void setVerticalResolution(double value)

Gets or sets the vertical resolution.

Parameters:

ParameterTypeDescription
valuedouble

getWidth()

public int getWidth()

Gets the width.

Returns: int

hasTransparentColor()

public boolean hasTransparentColor()

Gets a value indicating whether image has transparent color.

Returns: boolean

setTransparentColor(boolean value)

public void setTransparentColor(boolean value)

Gets a value indicating whether image has transparent color.

Parameters:

ParameterTypeDescription
valueboolean

Example: The 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();
}

hasAlpha()

public boolean hasAlpha()

Get a value indicating whether this instance has alpha.

Value: true if this instance has alpha; otherwise, false.

Returns: boolean

Example: 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

getTransparentColor()

public Color getTransparentColor()

Gets the transparent color.

Returns: Color

setTransparentColor(Color value)

public void setTransparentColor(Color value)

Gets the transparent color.

Parameters:

ParameterTypeDescription
valueColor

Example: The 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();
}

hasBackgroundColor()

public boolean hasBackgroundColor()

Gets a value indicating whether has background color.

Returns: boolean

setBackgroundColor(boolean value)

public void setBackgroundColor(boolean value)

Gets a value indicating whether has background color.

Parameters:

ParameterTypeDescription
valueboolean

Example: The 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();
}

getBackgroundColor()

public Color getBackgroundColor()

Gets the background color.

Returns: Color

setBackgroundColor(Color value)

public void setBackgroundColor(Color value)

Gets the background color.

Parameters:

ParameterTypeDescription
valueColor

Example: The 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();
}

getInterlaced()

public boolean getInterlaced()

Gets a value indicating whether this PngImage is interlaced.

Value: true if interlaced; otherwise, false.

Returns: boolean

isInterlaced()

public final boolean isInterlaced()

Gets a value indicating whether this image instance is interlaced.

Value: true if this image instance is interlaced; otherwise, false.

Returns: boolean - a value indicating whether this image instance is interlaced.

getXmpData()

public XmpPacketWrapper getXmpData()

Gets or sets the XMP metadata.

Value: The XMP metadata.

Returns: XmpPacketWrapper

setXmpData(XmpPacketWrapper value)

public void setXmpData(XmpPacketWrapper value)

Gets or sets the XMP metadata.

Value: The XMP metadata.

Parameters:

ParameterTypeDescription
valueXmpPacketWrapper

getDefaultOptions(Object[] args)

public ImageOptionsBase getDefaultOptions(Object[] args)

Gets the default options.

Parameters:

ParameterTypeDescription
argsjava.lang.Object[]The arguments.

Returns: ImageOptionsBase - Default options

getOriginalOptions()

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.

Returns: ImageOptionsBase - The options based on the original file settings.