Packages

 

com.aspose.imaging.fileformats.png

Class PngImage

  • All Implemented Interfaces:
    IObjectWithBounds, IRasterImageArgb32PixelLoader, IRasterImageRawDataLoader, com.aspose.imaging_internal.IPixelsSaver, com.aspose.imaging_internal.progressmanagement.IProgressEventHandler, com.aspose.imaging_internal.progressmanagement.IProgressInformer, com.aspose.ms.System.IDisposable, Closeable, AutoCloseable


    public class PngImage
    extends RasterCachedImage

    The new png image.

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

    • Constructor Detail

      • PngImage

        public PngImage(int width,
                        int height)

        Initializes a new instance of the PngImage class.

        Parameters:
        width - The width.
        height - The height.
        Code example:

        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");
        }
        

      • PngImage

        public PngImage(String path)

        Initializes a new instance of the PngImage class.

        Parameters:
        path - The path to load an image.
        Code 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

        public PngImage(RasterImage rasterImage)

        Initializes a new instance of the PngImage class.

        Parameters:
        rasterImage - The raster image.
        Code example:

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

      • PngImage

        public PngImage(String path,
                        int colorType)

        Initializes a new instance of the PngImage class.

        Parameters:
        path - The path to load an image.
        colorType - The color type.
        Throws:
        com.aspose.ms.System.ArgumentNullException - if path is null
        Code example:

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

      • PngImage

        public PngImage(RasterImage rasterImage,
                        int colorType)

        Initializes a new instance of the PngImage class.

        Parameters:
        rasterImage - The raster image.
        colorType - The color type.
        Code example:

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

      • PngImage

        public PngImage(InputStream stream)

        Initializes a new instance of the PngImage class.

        Parameters:
        stream - The stream to load an image.
      • PngImage

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

        Initializes a new instance of the PngImage class.

        Parameters:
        width - The width.
        height - The height.
        colorType - The color type.
        Code example:

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

      • PngImage

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

        Initializes a new instance of the PngImage class.

        Parameters:
        pngOptions - The png options.
        width - The width.
        height - The height.
        Code example:

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

    • Method Detail

      • getBitsPerPixel

        public int getBitsPerPixel()

        Gets the bits per pixel.

        Specified by:
        getBitsPerPixel in class Image
        Returns:
        The image bits per pixel count.
      • getHorizontalResolution

        public double getHorizontalResolution()

        Gets or sets the horizontal resolution.

        Overrides:
        getHorizontalResolution in class RasterImage
        Returns:
        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.

        Code 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

        public void setHorizontalResolution(double value)

        Gets or sets the horizontal resolution.

        Overrides:
        setHorizontalResolution in class RasterImage
        Parameters:
        value - 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.

      • getFileFormat

        public long getFileFormat()

        Gets a value of file format

        Overrides:
        getFileFormat in class Image
      • getRawDataFormat

        public PixelDataFormat getRawDataFormat()

        Gets the raw data format.

        Overrides:
        getRawDataFormat in class RasterImage
        Returns:
        The raw data format.
        Code 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.

        Overrides:
        getVerticalResolution in class RasterImage
        Returns:
        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.

        Code 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

        public void setVerticalResolution(double value)

        Gets or sets the vertical resolution.

        Overrides:
        setVerticalResolution in class RasterImage
        Parameters:
        value - 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.

      • hasTransparentColor

        public boolean hasTransparentColor()

        Gets a value indicating whether image has transparent color.

        Overrides:
        hasTransparentColor in class RasterImage
      • setTransparentColor

        public void setTransparentColor(boolean value)

        Gets a value indicating whether image has transparent color.

        Overrides:
        setTransparentColor in class RasterImage
        Code 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.
        Overrides:
        hasAlpha in class RasterImage
        Returns:
        true if this instance has alpha; otherwise, false.
        Code 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
        

      • setTransparentColor

        public void setTransparentColor(Color value)

        Gets the transparent color.

        Overrides:
        setTransparentColor in class RasterImage
        Code 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.

        Overrides:
        hasBackgroundColor in class Image
      • setBackgroundColor

        public void setBackgroundColor(boolean value)

        Gets a value indicating whether has background color.

        Overrides:
        setBackgroundColor in class Image
        Code 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();
        }
        

      • setBackgroundColor

        public void setBackgroundColor(Color value)

        Gets the background color.

        Overrides:
        setBackgroundColor in class Image
        Code 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.
      • setXmpData

        public void setXmpData(XmpPacketWrapper value)

        Gets or sets the XMP metadata.

        Value: The XMP metadata.
        Overrides:
        setXmpData in class RasterImage
        Parameters:
        value - The XMP metadata.
      • 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.

        Overrides:
        getOriginalOptions in class Image
        Returns:
        The options based on the original file settings.