Add Transparency to an Image - Console C#

This tutorial shows how to create a C# Windows Console application that uses LEADTOOLS image processing classes to create a transparent area in an image using two different techniques then save the image and transparency information to file.

Overview  
Summary This tutorial covers how to add transparency to images in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on the Add Transparency to an Image - Console C# tutorial, get familiar with the basic steps of creating a project and loading and saving images, by reviewing the Add References and Set a License and Load and Save Images tutorials.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License Tutorial tutorial. If the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

If using NuGet references, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Code to Create Alpha Channel Transparency

With the project created, the references added, and the license set, coding can begin.

Open Program.cs in the Solution Explorer and add the following statements to the using block at the top.

C#
// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 

In the Program class add a new method called AddAlphaTransparency() that takes and image and sets all white and near-white areas in it as transparent by manipulating the image's alpha channel.

C#
static RasterImage AddAlphaTransparency(RasterImage inputImage) 
{ 
   // createa an 8-bit image to use later as Alpha channel 
   RasterImage alpha = inputImage.CreateAlphaImage(); 
   // Fill the alpha channel with white to cause all pixels to be opaque 
   FillCommand fill = new FillCommand(RasterColor.White); 
   fill.Run(alpha); 
   // find white and near-white pixels in the input image and create region from them 
   RasterColor colorLow = new RasterColor(245, 245, 245), colorHi = RasterColor.White; 
   inputImage.AddColorRgbRangeToRegion(colorLow, colorHi, RasterRegionCombineMode.Set); 
   // copy the region to the alpha image 
   alpha.SetRegion(null, inputImage.GetRegion(null), RasterRegionCombineMode.Set); 
   inputImage.MakeRegionEmpty(); 
   // fill the region with black to represent transparent pixels. 
   fill.Color = RasterColor.Black; 
   fill.Run(alpha); 
   var outputImage = inputImage.Clone(); 
   outputImage.SetAlphaImage(alpha); 
   return outputImage; 
} 

Add the Code to Set a Specific Color as Transparent

Add a new method called AddTransparentColor() that takes and image and converts all white and near-white areas to pure white, then marks the white color as transparent.

C#
static RasterImage AddTransparentColor(RasterImage inputImage) 
{ 
   var outputImage = inputImage.Clone(); 
   // find white and near-white pixels in the input image and create region from them 
   RasterColor colorLow = new RasterColor(245, 245, 245), colorHi = RasterColor.White; 
   outputImage.AddColorRgbRangeToRegion(colorLow, colorHi, RasterRegionCombineMode.Set); 
   // Fill the region with a single white color to enable setting it as a unique transparent color 
   FillCommand fill = new FillCommand(RasterColor.White); 
   fill.Run(outputImage); 
   outputImage.Transparent = true; 
   outputImage.TransparentColor = RasterColor.White; 
   outputImage.MakeRegionEmpty(); 
   return outputImage; 
} 

Add the Code to Load an Image and Save it With Transparency

In the Main method, load a base image to have transparency added to it and pass it to the 2 methods created above:

C#
// Add code below to the Main() method after the call to SetLicense() 
using (RasterCodecs codecs = new RasterCodecs()) 
{ 
   RasterImage image = codecs.Load(@"C:\LEADTOOLS22\Resources\Images\testframe1.jpg"); 
       
   // Create a new image with trasparency alpha channel from near-white parts of the image 
   RasterImage imageWithAlpha = AddAlphaTransparency(image); 
   //save as 32-bit PNG to store alpha channel with image 
   codecs.Save(imageWithAlpha, @"imagePlusAlpha.png", RasterImageFormat.Png, 32); 
 
   // Create a new image with trasparency alpha channel from near-white parts of the image 
   RasterImage imageWithTransparentColor = AddTransparentColor(image); 
   //save as 8-bit GIF to store image with transparent color. Also the PNG format supports transparent color. 
   codecs.Save(imageWithTransparentColor, @"imageWithTranspColor.gif", RasterImageFormat.Gif, 8); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps are followed correctly, the application runs and loads a JPEG image, adds transparency to it in two different ways and saves the resulting images with transparency to two different files.

Wrap-up

This tutorial showed how to add the necessary references to produce images with transparency as well as how to save them to image files.

See Also

Help Version 22.0.2021.10.18
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.