Merge and Split Multipage Images - Windows C DLL

This tutorial shows how to merge and split multipage images using the LEADTOOLS SDK in a Windows C/C++ API Application.

Overview  
Summary This tutorial shows how to work with multipage images using the LEADTOOLS SDK in a Windows CDLL application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (19 KB)
Platform Windows C DLL Application
IDE Visual Studio 2017, 2019, 2022
Development License Download LEADTOOLS
Try it in another language
  • C API: C DLL (Windows)

  • Java: Android

Required Knowledge

Before working on the Merge and Split Multipage Images - Windows C DLL tutorial, 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.

Create the Project and Add LEADTOOLS References

Start with a copy of the 64-bit Windows API project created in the Load, Display, and Save Images tutorial. If the project is not available, create it by following the steps in that tutorial.

Ensure that the following required header and DLL files are included in the pre-compiled header file (either pch.h or stdafx.h, depending on the version of Visual Studio used).

#include "C:\LEADTOOLS23\Include\L_Bitmap.h" // use actual path where you installed LEADTOOLS 
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\Ltkrn_x.lib") 
 
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\Ltfil_x.lib") // file loading and saving 
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\Ltdis_x.lib") // image display 

Note

For a complete list of Dlls that are required for your application, 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 Merge Image Code

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

The steps below are for Visual Studio 2019; they could be different for other versions of Visual Studio.

Go to the Solution Explorer and double-click the resources file (.rc).

Add a new &Merge Images menu item to the File drop-down menu, between the Open and Save items. Next add a new Split &File menu item to the File drop-down menu, between the Merge Images and Save items.

Go to the WndProc function and under the switch (wmId) statement that is below the WM_COMMAND case, add a new case:

switch (wmId) 
{ 
   case ID_FILE_MERGEIMAGES: 
   { 
      TCHAR szOldTitle[1024]; 
      GetWindowText(hWnd, szOldTitle, 1024); 
      WIN32_FIND_DATA findData = { 0 }; 
      HANDLE hFindPng = FindFirstFile(TEXT("C:\\LEADTOOLS23\\Resources\\Images\\*.png"), &findData); 
      BOOL foundPage = (hFindPng != INVALID_HANDLE_VALUE); 
      while(foundPage) 
      { 
         TCHAR szWindowTitle[1024], szPageFilename[1024]; 
         wsprintf(szWindowTitle, TEXT("Appending page %s ..."), findData.cFileName); 
         SetWindowText(hWnd, szWindowTitle); 
         Sleep(200); //delay to see title 
         wsprintf(szPageFilename, TEXT("C:\\LEADTOOLS23\\Resources\\Images\\%s"), findData.cFileName); 
         AppendTiffPage(szPageFilename, TEXT("C:\\LEADTOOLS23\\Resources\\Images\\merged.tif")); 
         foundPage = FindNextFile(hFindPng, &findData); 
      } 
      if (hFindPng != INVALID_HANDLE_VALUE) 
         FindClose(hFindPng); 
      SetWindowText(hWnd, szOldTitle); 
   } 
   break; 
   // keep rest of the code as is 

Add the below code in the AppendTiffPage function. This function can be placed above the WndProc function:

void AppendTiffPage(TCHAR* pszInputPageFile, const TCHAR* pszOutputMultipageTiff) 
{ 
   BITMAPHANDLE BmpPage = { 0 }; 
   L_INT nRet = L_LoadBitmap(pszInputPageFile, &BmpPage, sizeof BITMAPHANDLE, 0, ORDER_BGR, NULL, NULL); 
   if(SUCCESS == nRet) 
   { 
      SAVEFILEOPTION saveOption = { 0 }; 
      L_GetDefaultSaveFileOption(&saveOption, sizeof SAVEFILEOPTION); 
      saveOption.PageNumber = 2; 
      L_SaveBitmap((TCHAR*)pszOutputMultipageTiff, &BmpPage, FILE_TIFLZW, 0, 2, &saveOption); 
      L_FreeBitmap(&BmpPage); 
   } 
} 

Add the Split Image Code

In the WndProc function, add a new case to the switch (wmId) statement:

switch (wmId) 
{ 
   case ID_FILE_SPLITFILE: 
      SplitFile(TEXT("C:\\LEADTOOLS23\\Resources\\Images\\merged.tif")); 
      break; 
   // keep rest of the code as is 

Add the below code in the SplitFile function, which can be placed above the WndProc function:

#include <shlwapi.h> 
#pragma comment (lib, "Shlwapi.lib") 
void SplitFile(const TCHAR* pszMultipageFile) 
{ 
   TCHAR szPageBaseName[1024]; 
   StrCpy(szPageBaseName, pszMultipageFile); 
   PathRemoveExtension(szPageBaseName); 
   FILEINFO fileInfo = { 0 }; //IMPORTANT: must zero the struct before using it 
   L_FileInfo((TCHAR *)pszMultipageFile, &fileInfo, sizeof FILEINFO, FILEINFO_TOTALPAGES, NULL); 
   for (int i = 1; i <= fileInfo.TotalPages; ++i) 
   { 
      BITMAPHANDLE BmpPage = { 0 }; 
      LOADFILEOPTION LoadOption = { 0 }; 
      L_GetDefaultLoadFileOption(&LoadOption, sizeof LOADFILEOPTION); 
      LoadOption.PageNumber = i; 
      L_INT nRet = L_LoadBitmap((TCHAR*)pszMultipageFile, &BmpPage, sizeof BITMAPHANDLE, 0, ORDER_BGRORGRAY, &LoadOption, NULL); 
      if (SUCCESS != nRet) //skip this page 
         break; 
      TCHAR szPageName[1024]; 
      wsprintf(szPageName, TEXT("%s_page%d.png"), szPageBaseName, i); 
      L_SaveBitmap(szPageName, &BmpPage, FILE_PNG, 0, 9, NULL); 
      L_FreeBitmap(&BmpPage); 
   } 
} 

Run the Project

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

If the steps were followed correctly, the application should run. Select File -> Merge Images to merge all the [PNG] files located inside the Images directory into a single [TIFF] file.

Select File -> Split File to split each page from merged.tif into a [PNG] file with the page number appended to the name.

Wrap-up

This tutorial covered how to merge multiple [PNG] images from a directory into a [TIFF] file. Also learned how to split a multipage [TIFF] image into individual files.

See Also

Help Version 23.0.2024.5.22
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.