Add Transparency to an Image - C# .NET 6

This tutorial shows how to create and save a transparent area in an image applying two different techniques in a C# .NET Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to add transparency to images in a C# .NET 6 application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on this tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License 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 NuGet references are used, 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>\LEADTOOLS23\Bin\net:

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:

Add the Alpha Channel Transparency Code

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 Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 

In the Program class add a new method called AddAlphaTransparency() that takes an 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) 
{ 
   // Create 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 Transparent Color Code

Add a new method called AddTransparentColor() that takes an 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 Load and Save Code

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(); 
RasterImage image = codecs.Load(@"C:\LEADTOOLS23\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, @"C:\LEADTOOLS23\Resources\Images\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, @"C:\LEADTOOLS23\Resources\Images\imageWithTranspColor.gif", RasterImageFormat.Gif, 8); 

Handling Streams

To load the file using MemoryStream, replace the code in the Main() method with the following:

C#
static void Main(string[] args) 
{ 
   InitLEAD(); 
 
   // Memory Stream to load and save the image 
   using RasterCodecs codecs = new RasterCodecs(); 
 
   string filePath = @"C:\LEADTOOLS23\Resources\Images\testframe1.jpg"; 
   byte[] bytes = File.ReadAllBytes(filePath); 
   using MemoryStream ms = new MemoryStream(bytes); 
   RasterImage image = codecs.Load(ms); 
 
   // Create a new image with transparency 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, @"C:\LEADTOOLS23\Resources\Images\imagePlusAlpha.png", RasterImageFormat.Png, 32); 
 
   // Create a new image with transparency 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 PNG formats supports transparent color. 
   codecs.Save(imageWithTransparentColor, @"C:\LEADTOOLS23\Resources\Images\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 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 23.0.2024.3.11
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.


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