Combine Images with Transparency - Windows C DLL

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

Overview  
Summary This tutorial covers how to combine two images using transparency in a Windows C DLL application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (246 KB)
Platform Windows C DLL Application
IDE Visual Studio 2019+
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project and loading/displaying an image by reviewing the Add References and Set a License and Load, Display, and Save Images tutorials, before working on the Combine Images with Transparency - Windows C DLL 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, create it by following the steps in that tutorial.

Open the pre-compiled header file (either pch.h or stdafx.h, depending on the version of Visual Studio used) and ensure the below lines are added.

#define LTV23_CONFIG 
#include "C:\LEADTOOLS23\Include\L_Bitmap.h" // use the actual path where LEADTOOLS is installed 
 
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\Ltkrn_x.lib") 
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\Ltdis_x.lib") // needed for region processing 
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\Ltfil_x.lib") // file loading and saving 

Note: For a complete list of DLLs that are required for specific application features, refer to Files to be Included with your Application - C API

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit functionality 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 references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Code to Combine Transparent Image with Solid Background Image

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

In the Solution Explorer, double-click the resources file (.rc).

Add a new &Combine Images menu item to the File drop-down menu and move it above the Exit item. Leave the new menu item's ID as ID_FILE_COMBINEIMAGES.

Load a base image that has transparency and use the L_FeatherAlphaBlendBitmap() function to combine it with a solid color background image.

Open the project's CPP file and navigate to the WndProc function. Under the switch (wmId) statement that is below the WM_COMMAND case, add a new case and the code below.

switch (wmId) 
{ 
case ID_FILE_COMBINEIMAGES: 
{ 
   L_TCHAR szFileWithAlpha[] = L_TEXT("C:\\LEADTOOLS23\\Resources\\Images\\imagePlusAlpha.png"); 
   FILEINFO info = { 0 }; // Must initialize struct with zeros 
   L_FileInfo(szFileWithAlpha, &info, sizeof info, 0, NULL); 
   if (info.Flags & FILEINFO_HAS_ALPHA) 
   { 
      BITMAPHANDLE bmpCombined = { 0 }; 
      L_CreateBitmap(&bmpCombined, sizeof bmpCombined, TYPE_CONV, info.Width, info.Height, 24, ORDER_BGR, NULL, info.ViewPerspective, NULL, 0); 
      L_FillBitmap(&bmpCombined, RGB(102, 148, 237)); 
      BITMAPHANDLE bmpWithAlpha = { 0 }; 
      L_LoadBitmap(szFileWithAlpha, &bmpWithAlpha, sizeof BITMAPHANDLE, 32, ORDER_BGR, NULL, NULL); 
      BITMAPHANDLE bmpMask = { 0 }; 
      L_GetBitmapAlpha(&bmpWithAlpha, &bmpMask, sizeof BITMAPHANDLE); 
      L_FeatherAlphaBlendBitmap(&bmpCombined, 0, 0, BITMAPWIDTH(&bmpWithAlpha), BITMAPHEIGHT(&bmpWithAlpha), &bmpWithAlpha, 0, 0, &bmpMask, 0, 0, 0); 
      L_SaveBitmap((L_TCHAR*)L_TEXT("C:\\LEADTOOLS23\\Resources\\Images\\combinedImages.jpg"), &bmpCombined, FILE_JPEG_411, 24, 2, NULL); 
      L_FreeBitmap(&bmpCombined); 
      L_FreeBitmap(&bmpWithAlpha); 
      L_FreeBitmap(&bmpMask); 
   } 
} 
break; 
// Keep rest of the code as is 

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 - Windows C DLL, which adds transparency to a sample image.

Wrap-up

This tutorial showed how to use the various LEADTOOLS functions to combine a transparent image with a solid color background.

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.