Option TransparentColor

hi, we saw the option Options.TransparentColor is removed from new imaging library
we wonder what is the best way for correctly changing our code
this is one of our code lines now:
OptionsIT.TransparentColor = new Aspose.Imaging.TransparentColorSetting(Aspose.Imaging.Color.Transparent);

how can we change it?
thank you

Hi Valerio,


Thank you for contacting Aspose support.

Aspose.Imaging for .NET 2.7.0 has exposed the TransparentColor property of type Aspose.Imaging.Color to RasterImage and PngImage classes. You may use any of the aforesaid according to your application requirements.

C#

//Load the source image (any format) in an instance of RasterImage
using (RasterImage image = (RasterImage)Image.Load(sourceImage))
{
//Set the background color for the image
image.BackgroundColor = Color.White;

//Set the transparent color for the image
image.TransparentColor = Color.Black;

//Set the HasTransparentColor & HasBackgroundColor properties to true
image.HasTransparentColor = true;
image.HasBackgroundColor = true;

//Save the image on disc in PNG format
image.Save(outputImage, new PngOptions());
}

so, if the original image has transparent background color we don’t need to do anything?
or we need to load image and set:
image.TransparentColor = Color.Transparent;

image.HasTransparentColor = true;

image.HasBackgroundColor = true;

?

our workflow is - as example - to convert a tiff either in png (with original transparent background) and in pdf (with white background).

thanks a lot

Hi Valerio,


If your base/input image already has transparency then you do not need to load it for specifying the transparency again. Let me explain the concept of TransparentColor & BackgroundColor properties so you may employ them accordingly in your application.

First of all, there is no processing of these two color settings. It means, that no pixel data is affected during the image save process, and these colors are just stored in special data chunks before the image’s pixel data. However, these two colors serve as information for the PNG decoders.

The background color will typically be used to fill unused screen space around the image, as well as any transparent pixels within the image. (Thus, background color is valid and useful even when the image does not use transparency.) The background color is either used as background or ignored; it is not an intermediate layer between the PNG image and some other background. Moreover, the image viewers often ignore background data, for example - IrfanView has its own background color setting and uses it despite of any in-file data.
So, if you wish to produce image with transparent parts and does not want to use RGBA colors, you may set the transparent color - this color will be substituted with the underlying layer.

Please consider the following code snippet.


using (RasterImage image = (RasterImage)Image.Load(sourceImage))
{
//Set the background color for the image
image.BackgroundColor = Color.White;

//Set the transparent color for the image
image.TransparentColor = Color.Black;

//Set the HasTransparentColor & HasBackgroundColor properties to true
image.HasTransparentColor = true;
image.HasBackgroundColor = true;

//Save the image on disc in PNG format
image.Save(outputImage, new PngOptions());
}

Here we are setting the transparent color as black therefore any black pixel will be replaced by the underlying canvas. Additionally we are saving the background color so ideally the PNG decoder will use background color as base layer and black will be replaced by white.

Please feel free to write back in case you have more questions or concerns for us.