Combine Images with Transparency - Console C#

This tutorial shows how to create a C# Windows Console application that uses LEADTOOLS image processing classes to merge an image with transparent area on top of a background image.

Summary This tutorial covers how to combine two images using transparency in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (231 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 Combine Images with Transparency - 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:


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 and Load the Two Images to be Combined

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.

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

Add the following code in the Main() method after the call to SetLicense() that will load an image containing transparency as well as create a solid color background image that will be combined with it.

// Add code below to the Main() method after the call to SetLicense() 
string fileWithAlpha = @"imagePlusAlpha.png"; 
using (RasterCodecs codecs = new RasterCodecs()) 
   CodecsImageInfo info = codecs.GetInformation(fileWithAlpha, false); 
   if (info.HasAlpha) 
      RasterImage backgroundImage = RasterImage.Create(info.Width, info.Height, 24, info.XResolution, RasterColor.FromKnownColor(RasterKnownColor.CornflowerBlue)); 
      RasterImage imageWithAlpha = codecs.Load(fileWithAlpha, 32, CodecsLoadByteOrder.Bgr, 1, 1); 
      RasterImage combinedImage = CombineTransparentImage(imageWithAlpha, backgroundImage); 
      codecs.Save(combinedImage, @"C:\LEADTOOLS22\Resources\Images\combinedImage.jpg", RasterImageFormat.Jpeg411, 24); 

Add the Code to Combine the Background and Transparent Images

Use the code below to add a new method called CombineTransparentImage that will combine the loaded transparent image with the solid color background image using the FeatherAlphaBlendCommand image processing class.

static RasterImage CombineTransparentImage(RasterImage imageWithAlpha, RasterImage backgroundImage) 
   RasterImage maskImage = imageWithAlpha.CreateAlphaImage(); 
   RasterImage outputImage = backgroundImage.Clone(); 
   LeadRect rect = new LeadRect(0, 0, outputImage.Width, outputImage.Height); 
   FeatherAlphaBlendCommand blend = new FeatherAlphaBlendCommand(imageWithAlpha, new LeadPoint(0, 0), rect, maskImage); 
   return outputImage; 

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 an image containing transparent alpha channel then combines it with a solid color background image. The result is then saved to a file on disk.

Note: The image with transparency used for this tutorial, imagePlusAlpha.png, can be obtained from the output of the code in the tutorial, Add Transparency to an Image - Console C#, which adds transparency to a sample image.


This tutorial showed how to add the necessary references to combine a transparent image with a solid color background using the FeatherAlphaBlendCommand class.

See Also

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

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