public class PngImage extends RasterCachedImage implements com.aspose.fileformats.core.interfaces.IInterlaced
The new png 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();
}
| Constructor and Description |
|---|
PngImage(InputStream stream)
Initializes a new instance of the
PngImage class. |
PngImage(int width,
int height)
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. |
PngImage(RasterImage rasterImage)
Initializes a new instance of the
PngImage class. |
PngImage(RasterImage rasterImage,
int colorType)
Initializes a new instance of the
PngImage class. |
PngImage(String path)
Initializes a new instance of the
PngImage class. |
PngImage(String path,
int colorType)
Initializes a new instance of the
PngImage class. |
| Modifier and Type | Method and Description |
|---|---|
Color |
getBackgroundColor()
Gets the background color.
|
int |
getBitsPerPixel()
Gets the bits per pixel.
|
ImageOptionsBase |
getDefaultOptions(Object[] args)
Gets the default options.
|
long |
getFileFormat()
Gets a value of file format
|
int |
getHeight()
Gets the height.
|
double |
getHorizontalResolution()
Gets or sets the horizontal resolution.
|
boolean |
getInterlaced()
Gets a value indicating whether this
PngImage is interlaced. |
ImageOptionsBase |
getOriginalOptions()
Gets the options based on the original file settings.
|
PixelDataFormat |
getRawDataFormat()
Gets the raw data format.
|
Color |
getTransparentColor()
Gets the transparent color.
|
double |
getVerticalResolution()
Gets or sets the vertical resolution.
|
int |
getWidth()
Gets the width.
|
XmpPacketWrapper |
getXmpData()
Gets or sets the XMP metadata.
|
boolean |
hasAlpha()
Get a value indicating whether this instance has alpha.
|
boolean |
hasBackgroundColor()
Gets a value indicating whether has background color.
|
boolean |
hasTransparentColor()
Gets a value indicating whether image has transparent color.
|
boolean |
isInterlaced()
Gets a value indicating whether this image instance is interlaced.
|
void |
setBackgroundColor(boolean value)
Gets a value indicating whether has background color.
|
void |
setBackgroundColor(Color value)
Gets the background color.
|
void |
setHorizontalResolution(double value)
Gets or sets the horizontal resolution.
|
void |
setTransparentColor(boolean value)
Gets a value indicating whether image has transparent color.
|
void |
setTransparentColor(Color value)
Gets the transparent color.
|
void |
setVerticalResolution(double value)
Gets or sets the vertical resolution.
|
void |
setXmpData(XmpPacketWrapper value)
Gets or sets the XMP metadata.
|
adjustBrightness, adjustContrast, adjustGamma, adjustGamma, binarizeBradley, binarizeBradley, binarizeFixed, binarizeOtsu, cacheData, crop, dither, grayscale, isCached, resize, resize, rotate, rotateFlipcrop, dither, filter, getArgb32Pixel, getDefaultArgb32Pixels, getDefaultPixels, getDefaultRawData, getDefaultRawData, getImageOpacity, getModifyDate, getPixel, getPremultiplyComponents, getRawCustomColorConverter, getRawDataSettings, getRawFallbackIndex, getRawIndexedColorConverter, getRawLineSize, getSkewAngle, getUpdateXmpData, getUseRawData, isRawDataAvailable, isUsePalette, loadArgb32Pixels, loadArgb64Pixels, loadCmyk32Pixels, loadCmykPixels, loadPartialArgb32Pixels, loadPartialPixels, loadPixels, loadRawData, loadRawData, normalizeAngle, normalizeAngle, readArgb32ScanLine, readScanLine, 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, getBounds, getBufferSizeHint, getContainer, getFileFormat, getFileFormat, getFittingRectangle, getFittingRectangle, getInterruptMonitor, getPalette, getProgressEventHandler, getProgressEventHandlerInfo, getProportionalHeight, getProportionalWidth, 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)
Initializes a new instance of the PngImage class.
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)
Initializes a new instance of the PngImage class.
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)
Initializes a new instance of the PngImage class.
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.
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)
Initializes a new instance of the PngImage class.
rasterImage - The raster image.colorType - The color type.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();
}
public PngImage(InputStream stream)
Initializes a new instance of the PngImage class.
stream - The stream to load an image.public PngImage(int width,
int height,
int colorType)
Initializes a new instance of the PngImage class.
width - The width.height - The height.colorType - The color type.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();
}
public PngImage(PngOptions pngOptions, int width, int height)
Initializes a new instance of the PngImage class.
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()
Gets the bits per pixel.
getBitsPerPixel in class Imagepublic int getHeight()
Gets the height.
getHeight in interface IObjectWithBoundsgetHeight in class Imagepublic double getHorizontalResolution()
Gets or sets the horizontal resolution.
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)
Gets or sets the horizontal resolution.
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()
Gets a value of file format
getFileFormat in class ImageFileFormatpublic PixelDataFormat getRawDataFormat()
Gets the raw data format.
getRawDataFormat in class RasterImageThe 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()
Gets or sets the vertical resolution.
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)
Gets or sets the vertical resolution.
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()
Gets the width.
getWidth in interface IObjectWithBoundsgetWidth in class Imagepublic boolean hasTransparentColor()
Gets a value indicating whether image has transparent color.
hasTransparentColor in class RasterImagepublic void setTransparentColor(boolean value)
Gets a value indicating whether image has transparent color.
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 hasAlpha()
Get a value indicating whether this instance has alpha.
Value:true if this instance has alpha; otherwise, false.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()
Gets the transparent color.
getTransparentColor in class RasterImagepublic void setTransparentColor(Color value)
Gets the transparent color.
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()
Gets a value indicating whether has background color.
hasBackgroundColor in class Imagepublic void setBackgroundColor(boolean value)
Gets a value indicating whether has background color.
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 Color getBackgroundColor()
Gets the background color.
getBackgroundColor in class Imagepublic void setBackgroundColor(Color value)
Gets the background color.
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()
Gets a value indicating whether this PngImage is interlaced.
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()
Gets or sets the XMP metadata.
Value: The XMP metadata.getXmpData in class RasterImagepublic void setXmpData(XmpPacketWrapper value)
Gets or sets the XMP metadata.
Value: The XMP metadata.setXmpData 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-2022 Aspose Pty Ltd. All Rights Reserved.