Combine Images with Transparency - Console C#

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

Overview  
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 2019, 2022
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 the Combine Images with Transparency - Console C# 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 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 DLL files are required for your application, refer to Files to be Included With Your Application.

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

C#
// 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.

C#
// Add code below to the Main() method after the call to SetLicense() 
// See Note below for where to obtain imagePlusAlpha.png 
string fileWithAlpha = @"FILE PATH TO 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.

C#
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); 
   blend.Run(outputImage); 
   return outputImage; 
} 

Handling Streams

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

C#
static void Main(string[] args) 
{ 
   try 
   { 
      SetLicense(); 
      // See Note below for where to obtain imagePlusAlpha.png 
      string fileWithAlpha = @"C:\LEADTOOLS22\Resources\Images\imagePlusAlpha.png"; 
      using (RasterCodecs codecs = new RasterCodecs()) 
      { 
         // Load using Memory Stream 
         byte[] fileWithAlphaData = File.ReadAllBytes(fileWithAlpha); 
         using (MemoryStream fileWithAlphaStream = new MemoryStream(fileWithAlphaData)) 
         { 
            CodecsImageInfo info = codecs.GetInformation(fileWithAlphaStream, false); 
            if (info.HasAlpha) 
            { 
               RasterImage backgroundImage = RasterImage.Create(info.Width, info.Height, 24, info.XResolution, RasterColor.FromKnownColor(RasterKnownColor.CornflowerBlue)); 
               RasterImage imageWithAlpha = codecs.Load(fileWithAlphaStream); 
               RasterImage combinedImage = CombineTransparentImage(imageWithAlpha, backgroundImage); 
 
               // Save to Memory Stream 
               MemoryStream outputStream = new MemoryStream(); 
               codecs.Save(combinedImage, outputStream, RasterImageFormat.Jpeg411, 24); 
            } 
         } 
      } 
   } 
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.ToString()); 
   } 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

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.

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.

Wrap-up

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.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

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