Class PngImage
Namespace: Aspose.Imaging.FileFormats.Png
Assembly: Aspose.Imaging.dll (25.7.0)
Manipulate Portable Network Graphics (PNG) raster images with our versatile API,featuring support for compression levels and various color depths includingGrayscale, 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 fileformats for optimal versatility and customization.
[JsonObject(MemberSerialization.OptIn)]
public class PngImage : RasterCachedImage, IDisposable, IObjectWithBounds, IRasterImageArgb32PixelLoader, IRasterImageRawDataLoader, IHasXmpData, IHasMetadata
{
}
Inheritance
object ← DisposableObject ← DataStreamSupporter ← Image ← RasterImage ← RasterCachedImage ← PngImage
Implements
IDisposable , IObjectWithBounds , IRasterImageArgb32PixelLoader , IRasterImageRawDataLoader , IHasXmpData , IHasMetadata
Inherited Members
RasterCachedImage.CacheData() , RasterCachedImage.Blend(Point, RasterImage, Rectangle, byte) , RasterCachedImage.Resize(int, int, ResizeType) , RasterCachedImage.Resize(int, int, ImageResizeSettings) , RasterCachedImage.RotateFlip(RotateFlipType) , RasterCachedImage.Rotate(float, bool, Color) , RasterCachedImage.Crop(Rectangle) , RasterCachedImage.Dither(DitheringMethod, int, IColorPalette) , RasterCachedImage.Grayscale() , RasterCachedImage.NormalizeHistogram() , RasterCachedImage.BinarizeFixed(byte) , RasterCachedImage.BinarizeOtsu() , RasterCachedImage.BinarizeBradley(double, int) , RasterCachedImage.BinarizeBradley(double) , RasterCachedImage.AdjustBrightness(int) , RasterCachedImage.AdjustContrast(float) , RasterCachedImage.AdjustGamma(float, float, float) , RasterCachedImage.AdjustGamma(float) , RasterCachedImage.OnCached() , RasterCachedImage.OnPaletteChanged(IColorPalette, IColorPalette) , RasterCachedImage.IsCached , RasterImage.xmpData , RasterImage.RemoveMetadata() , RasterImage.Save(Stream, ImageOptionsBase, Rectangle) , RasterImage.GetModifyDate(bool) , RasterImage.Dither(DitheringMethod, int) , RasterImage.Dither(DitheringMethod, int, IColorPalette) , RasterImage.GetDefaultPixels(Rectangle, IPartialArgb32PixelLoader) , RasterImage.GetDefaultRawData(Rectangle, IPartialRawDataLoader, RawDataSettings) , RasterImage.GetDefaultArgb32Pixels(Rectangle) , RasterImage.GetDefaultRawData(Rectangle, RawDataSettings) , RasterImage.GetArgb32Pixel(int, int) , RasterImage.GetPixel(int, int) , RasterImage.SetArgb32Pixel(int, int, int) , RasterImage.SetPixel(int, int, Color) , RasterImage.ReadScanLine(int) , RasterImage.ReadArgb32ScanLine(int) , RasterImage.WriteScanLine(int, Color[]) , RasterImage.WriteArgb32ScanLine(int, int[]) , RasterImage.LoadPartialArgb32Pixels(Rectangle, IPartialArgb32PixelLoader) , RasterImage.LoadPartialPixels(Rectangle, IPartialPixelLoader) , RasterImage.LoadArgb32Pixels(Rectangle) , RasterImage.LoadArgb64Pixels(Rectangle) , RasterImage.LoadPartialArgb64Pixels(Rectangle, IPartialArgb64PixelLoader) , RasterImage.LoadPixels(Rectangle) , RasterImage.LoadCmykPixels(Rectangle) , RasterImage.LoadCmyk32Pixels(Rectangle) , RasterImage.LoadRawData(Rectangle, RawDataSettings, IPartialRawDataLoader) , RasterImage.LoadRawData(Rectangle, Rectangle, RawDataSettings, IPartialRawDataLoader) , RasterImage.SaveRawData(byte[], int, Rectangle, RawDataSettings) , RasterImage.SaveArgb32Pixels(Rectangle, int[]) , RasterImage.SavePixels(Rectangle, Color[]) , RasterImage.SaveCmykPixels(Rectangle, CmykColor[]) , RasterImage.SaveCmyk32Pixels(Rectangle, int[]) , RasterImage.SetResolution(double, double) , RasterImage.SetPalette(IColorPalette, bool) , RasterImage.Resize(int, int, ImageResizeSettings) , RasterImage.Rotate(float, bool, Color) , RasterImage.Rotate(float) , RasterImage.BinarizeFixed(byte) , RasterImage.BinarizeOtsu() , RasterImage.BinarizeBradley(double) , RasterImage.BinarizeBradley(double, int) , RasterImage.Blend(Point, RasterImage, Rectangle, byte) , RasterImage.Blend(Point, RasterImage, byte) , RasterImage.Grayscale() , RasterImage.NormalizeHistogram() , RasterImage.AdjustBrightness(int) , RasterImage.AdjustContrast(float) , RasterImage.AdjustGamma(float, float, float) , RasterImage.AdjustGamma(float) , RasterImage.GetSkewAngle() , RasterImage.NormalizeAngle() , RasterImage.NormalizeAngle(bool, Color) , RasterImage.Filter(Rectangle, FilterOptionsBase) , RasterImage.ReplaceColor(Color, byte, Color) , RasterImage.ReplaceColor(int, byte, int) , RasterImage.ReplaceNonTransparentColors(Color) , RasterImage.ReplaceNonTransparentColors(int) , RasterImage.ToBitmap() , RasterImage.ReleaseManagedResources() , RasterImage.UpdateMetadata() , RasterImage.UpdateDimensions(int, int) , RasterImage.PremultiplyComponents , RasterImage.UseRawData , RasterImage.UpdateXmpData , RasterImage.XmpData , RasterImage.RawIndexedColorConverter , RasterImage.RawCustomColorConverter , RasterImage.RawFallbackIndex , RasterImage.RawDataSettings , RasterImage.UsePalette , RasterImage.RawDataFormat , RasterImage.RawLineSize , RasterImage.IsRawDataAvailable , RasterImage.HorizontalResolution , RasterImage.VerticalResolution , RasterImage.HasTransparentColor , RasterImage.HasAlpha , RasterImage.TransparentColor , RasterImage.ImageOpacity , RasterImage.DataLoader , Image.CanLoad(string) , Image.CanLoad(string, LoadOptions) , Image.CanLoad(Stream) , Image.CanLoad(Stream, LoadOptions) , Image.Create(ImageOptionsBase, int, int) , Image.Create(Image[]) , Image.Create(MultipageCreateOptions) , Image.Create(string[], bool) , Image.Create(string[]) , Image.Create(Image[], bool) , Image.GetFileFormat(string) , Image.GetFileFormat(Stream) , Image.GetFittingRectangle(Rectangle, int, int) , Image.GetFittingRectangle(Rectangle, int[], int, int) , Image.Load(string, LoadOptions) , Image.Load(string) , Image.Load(Stream, LoadOptions) , Image.Load(Stream) , Image.GetProportionalWidth(int, int, int) , Image.GetProportionalHeight(int, int, int) , Image.RemoveMetadata() , Image.CanSave(ImageOptionsBase) , Image.Resize(int, int) , Image.Resize(int, int, ResizeType) , Image.Resize(int, int, ImageResizeSettings) , Image.GetDefaultOptions(object[]) , Image.GetOriginalOptions() , Image.ResizeWidthProportionally(int) , Image.ResizeHeightProportionally(int) , Image.ResizeWidthProportionally(int, ResizeType) , Image.ResizeHeightProportionally(int, ResizeType) , Image.ResizeWidthProportionally(int, ImageResizeSettings) , Image.ResizeHeightProportionally(int, ImageResizeSettings) , Image.RotateFlip(RotateFlipType) , Image.Rotate(float) , Image.Crop(Rectangle) , Image.Crop(int, int, int, int) , Image.Save() , Image.Save(string) , Image.Save(string, ImageOptionsBase) , Image.Save(string, ImageOptionsBase, Rectangle) , Image.Save(Stream, ImageOptionsBase) , Image.Save(Stream, ImageOptionsBase, Rectangle) , Image.GetSerializedStream(ImageOptionsBase, Rectangle, out int) , Image.SetPalette(IColorPalette, bool) , Image.UpdateContainer(Image) , Image.GetCanNotSaveMessage(ImageOptionsBase) , Image.GetFitRectangle(Rectangle) , Image.GetImage2Export(ImageOptionsBase, Rectangle, IImageExporter) , Image.GetFitRectangle(Rectangle, int[]) , Image.OnPaletteChanged(IColorPalette, IColorPalette) , Image.OnPaletteChanging(IColorPalette, IColorPalette) , Image.ReleaseManagedResources() , Image.BitsPerPixel , Image.Bounds , Image.Container , Image.Height , Image.Palette , Image.UsePalette , Image.Size , Image.Width , Image.InterruptMonitor , Image.BufferSizeHint , Image.AutoAdjustPalette , Image.HasBackgroundColor , Image.FileFormat , Image.BackgroundColor , DataStreamSupporter.timeout , DataStreamSupporter.CacheData() , DataStreamSupporter.Save() , DataStreamSupporter.Save(Stream) , DataStreamSupporter.Save(string) , DataStreamSupporter.Save(string, bool) , DataStreamSupporter.SaveData(Stream) , DataStreamSupporter.ReleaseManagedResources() , DataStreamSupporter.OnDataStreamContainerChanging(StreamContainer) , DataStreamSupporter.DataStreamContainer , DataStreamSupporter.IsCached , DisposableObject.Dispose() , DisposableObject.ReleaseManagedResources() , DisposableObject.ReleaseUnmanagedResources() , DisposableObject.VerifyNotDisposed() , DisposableObject.Disposed , object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()
Examples
This example shows how to load a PNG image from a file.
string dir = "c:\\temp\\";
using (var pngImage = new Aspose.Imaging.FileFormats.Png.PngImage(dir + "sample.png"))
{
pngImage.Grayscale();
pngImage.Save(dir + "sample.grayscale.png");
}
Constructors
PngImage(int, int)
Initialize a new object of the Aspose.Imaging.FileFormats.Png.PngImage class by providing thewidth and height parameters. This constructor simplifies the creation of PNGimages by allowing developers to specify the dimensions directly, facilitatingefficient management of PNG image data within their applications.
public PngImage(int width, int height)
{
}
Parameters
width
int
The width.
height
int
The height.
Examples
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\\";
using (var pngImage = new Aspose.Imaging.FileFormats.Png.PngImage(100, 100))
{
var graphics = new Aspose.Imaging.Graphics(pngImage);
var brush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
graphics.FillRectangle(brush, pngImage.Bounds);
pngImage.Save(dir + "output.png");
}
PngImage(string)
Constructs a new instance of the Aspose.Imaging.FileFormats.Png.PngImage class using the pathparameter to specify the location of the image file to load. This constructorenables developers to conveniently create PNG images by loading them from a file,simplifying the process of working with PNG images in their applications.
public PngImage(string path)
{
}
Parameters
path
string
The path to load an image.
Examples
This example shows how to load a PNG image from a file.
string dir = "c:\\temp\\";
using (var pngImage = new Aspose.Imaging.FileFormats.Png.PngImage(dir + "sample.png"))
{
pngImage.Grayscale();
pngImage.Save(dir + "sample.grayscale.png");
}
PngImage(RasterImage)
Creates a new instance of the Aspose.Imaging.FileFormats.Png.PngImage class by providing a rasterimage as a parameter. This constructor allows developers to directly initialize aPNG image object using an existing raster image, streamlining the process ofworking with PNG images in their applications.
public PngImage(RasterImage rasterImage)
{
}
Parameters
rasterImage
RasterImage
The raster image.
Examples
This example shows how to load PNG image from a BMP image.
string dir = "c:\\temp\\";
using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100))
{
Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage);
Aspose.Imaging.Brushes.SolidBrush brush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
gr.FillRectangle(brush, bmpImage.Bounds);
using (Aspose.Imaging.FileFormats.Png.PngImage pngImage = new Aspose.Imaging.FileFormats.Png.PngImage(bmpImage))
{
System.Console.WriteLine("The PNG color type: {0}", pngImage.GetOriginalOptions());
pngImage.Save(dir + "output.png");
}
}
PngImage(string, PngColorType)
Initializes a new instance of the Aspose.Imaging.FileFormats.Png.PngImage class by specifying thepath to the image file and the color type. This constructor allows for convenientcreation of PNG images from files with different color types, providingflexibility in handling various image formats.
public PngImage(string path, PngColorType colorType)
{
}
Parameters
path
string
The path to load an image.
colorType
PngColorType
The color type.
Examples
This example shows how to load a PNG image from a file with the specified color type.
string dir = "c:\\temp\\";
using (var pngImage = new Aspose.Imaging.FileFormats.Png.PngImage(dir + "sample.png", Aspose.Imaging.FileFormats.Png.PngColorType.Grayscale))
{
pngImage.Save(dir + "sample.grayscale.png");
}
Exceptions
PngImage(RasterImage, PngColorType)
Creates a new instance of the Aspose.Imaging.FileFormats.Png.PngImage class by specifying araster image and a color type. This constructor enables developers to directlyconvert raster images into PNG format while specifying the desired color type,offering flexibility in color representation.
public PngImage(RasterImage rasterImage, PngColorType colorType)
{
}
Parameters
rasterImage
RasterImage
The raster image.
colorType
PngColorType
The color type.
Examples
This example shows how to load PNG image from a BMP image with the specified color type.
string dir = "c:\\temp\\";
using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100))
{
Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage);
Aspose.Imaging.Brushes.SolidBrush brush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
gr.FillRectangle(brush, bmpImage.Bounds);
using (Aspose.Imaging.FileFormats.Png.PngImage pngImage = new Aspose.Imaging.FileFormats.Png.PngImage(bmpImage, Aspose.Imaging.FileFormats.Png.PngColorType.Grayscale))
{
pngImage.Save(dir + "output.grayscale.png");
}
}
PngImage(Stream)
Creates a new instance of the Aspose.Imaging.FileFormats.Png.PngImage class by initializing itwith a stream. This constructor allows developers to load PNG images directlyfrom a stream, providing flexibility in image retrieval from different sources.
public PngImage(Stream stream)
{
}
Parameters
stream
Stream
The stream to load an image.
Examples
This example shows how to load a PNG image from a file or a file stream.
string dir = "c:\\temp\\";
using (System.IO.Stream stream = System.IO.File.OpenRead(dir + "sample.png"))
{
using (Aspose.Imaging.FileFormats.Png.PngImage pngImage = new Aspose.Imaging.FileFormats.Png.PngImage(stream))
{
pngImage.Grayscale();
pngImage.Save(dir + "sample.grayscale.png");
}
}
PngImage(int, int, PngColorType)
Instantiate a fresh instance of the Aspose.Imaging.FileFormats.Png.PngImage class, specifyingthe desired width, height, and color type parameters. This constructor enablesswift creation of PNG images with tailored dimensions and color configurations,facilitating streamlined image generation for various applications and workflows.
public PngImage(int width, int height, PngColorType colorType)
{
}
Parameters
width
int
The width.
height
int
The height.
colorType
PngColorType
The color type.
Examples
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\\";
using (var pngImage = new Aspose.Imaging.FileFormats.Png.PngImage(100, 100, Aspose.Imaging.FileFormats.Png.PngColorType.Grayscale))
{
var graphics = new Aspose.Imaging.Graphics(pngImage);
var brush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
graphics.FillRectangle(brush, pngImage.Bounds);
pngImage.Save(dir + "output.grayscale.png");
}
PngImage(PngOptions, int, int)
Initialize a new instance of the Aspose.Imaging.FileFormats.Png.PngImage class, incorporatingPNG options alongside width and height parameters. This constructor empowersdevelopers to create PNG images with customizable settings and dimensions,offering flexibility in image generation for diverse use cases.
public PngImage(PngOptions pngOptions, int width, int height)
{
}
Parameters
pngOptions
PngOptions
The png options.
width
int
The width.
height
int
The height.
Examples
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\\";
Aspose.Imaging.ImageOptions.PngOptions createOptions = new Aspose.Imaging.ImageOptions.PngOptions();
createOptions.BitDepth = 8;
createOptions.ColorType = Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
createOptions.CompressionLevel = 9;
createOptions.FilterType = Aspose.Imaging.FileFormats.Png.PngFilterType.Sub;
createOptions.Progressive = true;
using (Aspose.Imaging.FileFormats.Png.PngImage pngImage = new Aspose.Imaging.FileFormats.Png.PngImage(createOptions, 100, 100))
{
Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush(
new Aspose.Imaging.Point(0, 0),
new Aspose.Imaging.Point(pngImage.Width, pngImage.Height),
Aspose.Imaging.Color.Blue,
Aspose.Imaging.Color.Transparent);
Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(pngImage);
graphics.FillRectangle(gradientBrush, pngImage.Bounds);
pngImage.Save(dir + "output.explicitoptions.png");
}
Properties
BackgroundColor
Retrieves the background color of the image, if one is specified. This propertyis helpful for applications that need to identify and potentially manipulatethe background color of an image.
public override Color BackgroundColor
{
get;
set;
}
Property Value
Examples
The following example shows how to set fully transparent colors for a part of a TrueColor PNG image which doesn’t support alpha channel.
Aspose.Imaging.ImageOptions.PngOptions createOptions = new Aspose.Imaging.ImageOptions.PngOptions();
createOptions.Source = new Aspose.Imaging.Sources.FileCreateSource("c:\\temp\\transparent.png", false);
createOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.Truecolor;
using (var image = Image.Create(createOptions, 100, 100))
{
var pngImage = (Aspose.Imaging.FileFormats.Png.PngImage)image;
var gr = new Aspose.Imaging.Graphics(pngImage);
pngImage.TransparentColor = Aspose.Imaging.Color.Red;
pngImage.HasTransparentColor = true;
pngImage.BackgroundColor = Aspose.Imaging.Color.Green;
pngImage.HasBackgroundColor = true;
gr.FillRectangle(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.White), pngImage.Bounds);
var rect = new Rectangle(0, 0, pngImage.Width / 2, pngImage.Height / 2);
gr.FillRectangle(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red), rect);
pngImage.Save();
}
BitsPerPixel
Retrieve the bits per pixel value for the image. This property provides crucialinformation regarding the color depth of the image, enabling developers tounderstand the level of detail and color accuracy present in the image data.
public override int BitsPerPixel
{
get;
}
Property Value
FileFormat
Retrieves the format of the file associated with the image instance. Thisproperty provides essential information regarding the file type, enablingefficient handling and processing based on the specific format requirements.
public override FileFormat
Get
{
get;
}
Property Value
HasAlpha
Returns a boolean value indicating whether the image has an alpha channel,which determines its transparency. This property is useful for applicationsthat need to handle transparency, allowing developers to determine whetheradditional processing is required to handle transparent areas in the image.
public override bool HasAlpha
{
get;
}
Property Value
Examples
The following example shows how to check if a PNG image supports alpha-channel.
using System;
using System.IO;
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
System.Array colorTypes = System.Enum.GetValues(typeof(Aspose.Imaging.FileFormats.Png.PngColorType));
foreach (Aspose.Imaging.FileFormats.Png.PngColorType colorType in colorTypes)
{
Aspose.Imaging.ImageOptions.PngOptions createOptions = new Aspose.Imaging.ImageOptions.PngOptions();
createOptions.Source = new Sources.StreamSource(new System.IO.MemoryStream());
createOptions.ColorType = colorType;
using (Aspose.Imaging.Image image = Image.Create(createOptions, 100, 100))
{
Aspose.Imaging.FileFormats.Png.PngImage pngImage = (Aspose.Imaging.FileFormats.Png.PngImage)image;
if (pngImage.HasAlpha)
{
Console.WriteLine("A {0} PNG image supports alpha channel", createOptions.ColorType);
}
else
{
Console.WriteLine("A {0} PNG image doesn't support alpha channel", createOptions.ColorType);
}
}
}
HasBackgroundColor
Retrieves a boolean value indicating whether the image has a background color.This property is useful for applications needing to determine if an imageincludes a background color, which can be important for various processingtasks such as compositing, rendering, or exporting.
public override bool HasBackgroundColor
{
get;
set;
}
Property Value
Examples
The following example shows how to set fully transparent colors for a part of a TrueColor PNG image which doesn’t support alpha channel.
Aspose.Imaging.ImageOptions.PngOptions createOptions = new Aspose.Imaging.ImageOptions.PngOptions();
createOptions.Source = new Aspose.Imaging.Sources.FileCreateSource("c:\\temp\\transparent.png", false);
createOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.Truecolor;
using (var image = Image.Create(createOptions, 100, 100))
{
var pngImage = (Aspose.Imaging.FileFormats.Png.PngImage)image;
var gr = new Aspose.Imaging.Graphics(pngImage);
pngImage.TransparentColor = Aspose.Imaging.Color.Red;
pngImage.HasTransparentColor = true;
pngImage.BackgroundColor = Aspose.Imaging.Color.Green;
pngImage.HasBackgroundColor = true;
gr.FillRectangle(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.White), pngImage.Bounds);
var rect = new Rectangle(0, 0, pngImage.Width / 2, pngImage.Height / 2);
gr.FillRectangle(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red), rect);
pngImage.Save();
}
HasTransparentColor
Provides a boolean value indicating whether the image contains a transparentcolor. This property is crucial for applications that need to handletransparency, allowing developers to determine whether additional processing isrequired to handle transparent regions in the image.
public override bool HasTransparentColor
{
get;
set;
}
Property Value
Examples
The following example shows how to set fully transparent colors for a part of a TrueColor PNG image which doesn’t support alpha channel.
Aspose.Imaging.ImageOptions.PngOptions createOptions = new Aspose.Imaging.ImageOptions.PngOptions();
createOptions.Source = new Aspose.Imaging.Sources.FileCreateSource("c:\\temp\\transparent.png", false);
createOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.Truecolor;
using (Aspose.Imaging.Image image = Image.Create(createOptions, 100, 100))
{
Aspose.Imaging.FileFormats.Png.PngImage pngImage = (Aspose.Imaging.FileFormats.Png.PngImage)image;
Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(pngImage);
pngImage.TransparentColor = Aspose.Imaging.Color.Red;
pngImage.HasTransparentColor = true;
pngImage.BackgroundColor = Aspose.Imaging.Color.Green;
pngImage.HasBackgroundColor = true;
gr.FillRectangle(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.White), pngImage.Bounds);
Rectangle rect = new Rectangle(0, 0, pngImage.Width / 2, pngImage.Height / 2);
gr.FillRectangle(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red), rect);
pngImage.Save();
}
Height
Obtain the height of the image. This property returns the vertical dimension ofthe image, allowing developers to determine its size in pixels along thevertical axis.
public override int Height
{
get;
}
Property Value
HorizontalResolution
Retrieve or modify the horizontal resolution of the image. This propertyrepresents the number of pixels per inch along the horizontal axis of theimage. Adjusting this resolution can affect the physical size of the image whenprinted or displayed.
public override double
HorizontalResolution
{
get;
set;
}
Property Value
Examples
The following example shows how to set horizontal/vertical resolution of a PNG image.
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.png"))
{
Aspose.Imaging.FileFormats.Png.PngImage pngImage = (Aspose.Imaging.FileFormats.Png.PngImage)image;
double horizontalResolution = pngImage.HorizontalResolution;
double verticalResolution = pngImage.VerticalResolution;
System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", horizontalResolution);
System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", verticalResolution);
if (horizontalResolution != 96.0 || verticalResolution != 96.0)
{
System.Console.WriteLine("Set resolution values to 96 dpi");
pngImage.SetResolution(96.0, 96.0);
System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", pngImage.HorizontalResolution);
System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", pngImage.VerticalResolution);
}
}
Interlaced
Retrieves a boolean value indicating whether the Aspose.Imaging.FileFormats.Png.PngImage isinterlaced, which determines if the image data is stored in a progressivemanner for faster loading or transmission.
public bool Interlaced
{
get;
}
Property Value
IsInterlaced
Returns a boolean value indicating if the image instance is interlaced. Thisproperty is crucial for optimizing loading strategies and ensuring efficientperformance during image processing or display tasks.
public bool IsInterlaced
{
get;
}
Property Value
RawDataFormat
Accesses the raw data format of the image. This property provides insight intohow the image data is structured internally, which can be useful for advancedimage processing tasks or format conversion.
public override PixelDataFormat RawDataFormat
{
get;
}
Property Value
Examples
The following example loads PNG images and prints information about raw data format and alpha channel.
string[] fileNames = new string[]
{
@"c:\temp\sample.png",
@"c:\temp\alpha.png",
};
foreach (string fileName in fileNames)
{
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(fileName))
{
Aspose.Imaging.FileFormats.Png.PngImage pngImage = (Aspose.Imaging.FileFormats.Png.PngImage)image;
System.Console.WriteLine("ImageFile={0}, FileFormat={1}, HasAlpha={2}", fileName, pngImage.RawDataFormat, pngImage.HasAlpha);
}
}
TransparentColor
Retrieves the transparent color of the image, if it exists. This property isvaluable for applications requiring precise handling of transparent areaswithin images, allowing developers to access and manipulate the specifictransparent color used.
public override Color TransparentColor
{
get;
set;
}
Property Value
Examples
The following example shows how to set fully transparent colors for a part of a TrueColor PNG image which doesn’t support alpha channel.
Aspose.Imaging.ImageOptions.PngOptions createOptions = new Aspose.Imaging.ImageOptions.PngOptions();
createOptions.Source = new Aspose.Imaging.Sources.FileCreateSource("c:\\temp\\transparent.png", false);
createOptions.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.Truecolor;
using (Aspose.Imaging.Image image = Image.Create(createOptions, 100, 100))
{
Aspose.Imaging.FileFormats.Png.PngImage pngImage = (Aspose.Imaging.FileFormats.Png.PngImage)image;
Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(pngImage);
pngImage.TransparentColor = Aspose.Imaging.Color.Red;
pngImage.HasTransparentColor = true;
pngImage.BackgroundColor = Aspose.Imaging.Color.Green;
pngImage.HasBackgroundColor = true;
gr.FillRectangle(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.White), pngImage.Bounds);
Rectangle rect = new Rectangle(0, 0, pngImage.Width / 2, pngImage.Height / 2);
gr.FillRectangle(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red), rect);
pngImage.Save();
}
VerticalResolution
Provides access to the vertical resolution of the image. Developers can usethis property to retrieve or modify the resolution setting, which indicates thenumber of pixels per inch (PPI) along the vertical axis of the image.
public override double VerticalResolution
{
get;
set;
}
Property Value
Examples
The following example shows how to set horizontal/vertical resolution of a PNG image.
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.png"))
{
Aspose.Imaging.FileFormats.Png.PngImage pngImage = (Aspose.Imaging.FileFormats.Png.PngImage)image;
double horizontalResolution = pngImage.HorizontalResolution;
double verticalResolution = pngImage.VerticalResolution;
System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", horizontalResolution);
System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", verticalResolution);
if (horizontalResolution != 96.0 || verticalResolation != 96.0)
{
System.Console.WriteLine("Set resolution values to 96 dpi");
pngImage.SetResolution(96.0, 96.0);
System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", pngImage.HorizontalResolution);
System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", pngImage.VerticalResolution);
}
}
Width
Allows retrieval of the width of the image, providing essential informationabout its dimensions. This property is frequently used by developers todetermine the image’s width, enabling them to perform various operations basedon its size
public override int Width
{
get;
}
Property Value
XmpData
Accesses or modifies the Extensible Metadata Platform (XMP) metadata associatedwith the image. This property facilitates the extraction, modification, oraddition of metadata, enabling comprehensive management of image-related information.
public override XmpPacketWrapper XmpData
{
get;
set;
}
Property Value
Methods
GetDefaultOptions(object[])
Gets the default options.
public override ImageOptionsBase GetDefaultOptions(object[] args)
{
}
I've kept the existing opening curly brace and added a new one after the method declaration for proper indentation. The closing curly brace remains unchanged as it's correctly placed at the end of the method block.
Parameters
args
object
[]
The arguments.
Returns
Default options
GetModifyDate(bool)
Retrieves the timestamp indicating the most recent modification of the resourceimage. This method provides access to vital metadata, enabling applications toascertain when the image was last altered, facilitating version tracking andcontent management.
public override DateTime GetModifyDate(bool useDefault)
{
}
In your original code, it seems to be properly indented and spaced according to standard C# conventions. Therefore, no changes were made.
Parameters
useDefault
bool
if set to ’true’ uses the information from FileInfo as default value.
Returns
The date and time the resource image was last modified.
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 theAspose.Imaging.DataStreamSupporter.Save(System.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 themto the Aspose.Imaging.Image.Save(System.String,Aspose.Imaging.ImageOptionsBase) method as the second parameter.
public override ImageOptionsBase GetOriginalOptions()
{
}
Returns
The options based on the original file settings.
OnPaletteChanged(IColorPalette, IColorPalette)
Called when palette is changed.
protected override void OnPaletteChanged(
IColorPalette oldPalette,
IColorPalette newPalette)
{
}
Parameters
oldPalette
IColorPalette
The old palette.
newPalette
IColorPalette
The new palette.
SaveData(Stream)
The save data.
protected override void SaveData(Stream stream)
{
}
Parameters
stream
Stream
The stream.
UpdateDimensions(int, int)
The update dimensions.
protected override void UpdateDimensions(
int newWidth,
int newHeight
)
{
}
Parameters
newWidth
int
The new width.
newHeight
int
The new height.
UpdateMetadata()
Updates the image last modification date and time.
protected override void UpdateMetadata()
{
}