This tutorial shows how to read and write TIFF tags and comments in a Windows C/C++ API application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to use LEADTOOLS functions for reading and writing TIFF tags and comments in a Windows C DLL application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (19 KB) |
Platform | Windows C DLL Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
Try it in another language |
|
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 Read and Write TIFF Tags and Comments - Windows C DLL tutorial.
Start with a copy of the 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.
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 LTV22_CONFIG
#include "C:\LEADTOOLS22\Include\L_Bitmap.h" // use the actual path where LEADTOOLS is installed
#pragma comment (lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\x64\\Ltkrn_x.lib")
#pragma comment (lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\x64\\Ltfil_x.lib") // file loading and saving
#pragma comment (lib, "C:\\LEADTOOLS22\\Lib\\CDLL\\x64\\Ltdis_x.lib") // image display
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
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.
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 Tiff &Comment menu item to the File drop-down menu, between the Open and Save items. Leave the new menu item's ID as ID_FILE_TIFFCOMMENT
.
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_TIFFCOMMENT:
{
TCHAR szFileName[260] = TEXT(""); // File name
if (SUCCESS != GetBitmapLoadingName(hWnd, szFileName, ARRAYSIZE(szFileName)))
break;
L_UCHAR szCommentWritten[] = "LEADTOOLS Tutorial";
if (L_SetComment(CMNT_SZSOFTWARE, szCommentWritten, sizeof szCommentWritten) < 0)
{
MessageBox(hWnd, TEXT("Error setting comment"), TEXT("LEADTOOLS Tutorial"), MB_ICONERROR);
break;
}
if (SUCCESS != L_WriteFileComment(szFileName, NULL))
{
MessageBox(hWnd, TEXT("Error writing comment to file"), TEXT("LEADTOOLS Tutorial"), MB_ICONERROR);
break;
}
L_UCHAR szCommentRead[100] = "";
if (L_ReadFileComment(szFileName, CMNT_SZSOFTWARE, szCommentRead, sizeof szCommentRead, NULL) < 0)
{
MessageBox(hWnd, TEXT("Error reading comment from file"), TEXT("LEADTOOLS Tutorial"), MB_ICONERROR);
break;
}
MessageBoxA(hWnd, (LPCSTR)szCommentRead, "Comment Text", MB_ICONINFORMATION); // Use the ANSI message box function.
}
break;
// Keep rest of the code as is
The code for the GetBitmapLoadingName()
function, called inside the code above, is listed in the Load, Display, and Save Images tutorial.
Using the Solution Explorer, navigate back to the resources file (.rc).
Add a new Tiff &Tag menu item to the File drop-down menu, between the Open and Save items. Leave the new menu item's ID as ID_FILE_TIFFTAG
.
Go to the WndProc
function and 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_TIFFTAG:
{
TCHAR szFileName[260] = TEXT(""); // File name
if (SUCCESS != GetBitmapLoadingName(hWnd, szFileName, ARRAYSIZE(szFileName)))
break;
// This code reads the Xresolution from a TIFF image, modifies the value, and writes it back.
const unsigned uXResTagID = 282;
L_UINT16 uType;
L_UINT uCount;
L_INT nBytes = L_ReadFileTag(szFileName, uXResTagID , &uType, &uCount, NULL, NULL);
if((uType != TAG_RATIONAL) || (nBytes != 8))
{
MessageBox(hWnd, TEXT("Incorrect tag type or size"), TEXT("LEADTOOLS Tutorial"), MB_ICONERROR);
break;
}
L_UINT32 xResFraction[2];
L_ReadFileTag(szFileName, uXResTagID, &uType, &uCount, xResFraction, NULL);
xResFraction[0] = xResFraction[0] * 5; // Modify XResolution Numerator
// xResFraction[1] is the XResolution Denominator. Keep it unchanged
if (SUCCESS != L_SetTag(uXResTagID, TAG_RATIONAL, 1, xResFraction))
{
MessageBox(hWnd, TEXT("Error setting tag"), TEXT("LEADTOOLS Tutorial"), MB_ICONERROR);
break;
}
if (SUCCESS != L_WriteFileTag(szFileName, NULL))
{
MessageBox(hWnd, TEXT("Error writing tag"), TEXT("LEADTOOLS Tutorial"), MB_ICONERROR);
break;
}
MessageBox(hWnd, TEXT("Modified file XResolution through tag"), TEXT("LEADTOOLS Tutorial"), MB_ICONINFORMATION);
}
break;
// Keep rest of the code as is
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and gives the user the ability to execute the following commands:
This tutorial showed how to use the L_SetComment
, L_WriteFileComment
, L_ReadFileComment
, L_ReadFileTag
, L_SetTag
and L_WriteFileTag
functions.