Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, March 8, 2012 8:34:15 AM(UTC)

Amin  
Amin

Groups: Manager, Tech Support
Posts: 356


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.
LEAD Logo
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2020, Yet Another Forum.NET
This page was generated in 0.074 seconds.