One type of transparency is based on alpha channel, which has values for each pixel in the image ranging from zero (fully transparent) to 255 (fully opaque).
The code below loads an image that has dark areas (black and near-black) and creates an alpha mask image that has transparent parts where the main image has dark pixels.
It then saves the main image as 32-bit PNG after setting the new mask image as the alpha channel of the main image.
Below is a code sample to illustrate how to do this:
==========================
RasterImage main = _codecs.Load("DarkBG1.jpg");
RasterImage alpha = main.Clone();
GrayscaleCommand gray = new GrayscaleCommand(8);
gray.Run(alpha);
//create a region of all dark areas
alpha.AddColorRgbRangeToRegion(new RasterColor(0, 0, 0), new RasterColor(40, 40, 40), RasterRegionCombineMode.Set);
FillCommand fill = new FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Black));
//fill the dark areas with black
fill.Run(alpha);
//invert the region so that it covers the non-dark areas
alpha.AddRectangleToRegion(null, new LeadRect(0, 0, alpha.Width, alpha.Height), RasterRegionCombineMode.Xor);
fill.Color = RasterColor.FromKnownColor(RasterKnownColor.White);
//fill the new regin with white
fill.Run(alpha);
//clear the region
alpha.MakeRegionEmpty();
//diffuse the edges to make the boundary smooth between black and white
AverageCommand avg = new AverageCommand(8);
avg.Run(alpha);
main.SetAlphaImage(alpha);
_codecs.Save(main, "DarkBG1withAlpha.png", RasterImageFormat.Png, 32);
==========================
Amin Dodin
Senior Support Engineer
LEAD Technologies, Inc.