This tutorial shows how to read an image file and detect the proper format and file extension in a Windows C/C++ API application using the LEADTOOLS SDK.
Note
This feature is useful if the file does not have a file extension or if you are reading the file from a stream.
This tutorial shows how to read a given image file and detect the proper format, along with the appropriate file extension, using the LEADTOOLS SDK . This is useful if the filename's extension is incorrect or missing, or if the image is being read from a memory buffer.
Overview | |
---|---|
Summary | This tutorial covers how to use LEADTOOLS to detect a file's format and file extension in a Windows CDLL application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (18 KB) |
Platform | Windows C DLL Application |
IDE | Visual Studio 2017, 2019, 2022 |
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 Detect Image Format and Extension - 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 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\\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.
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 &Image Format menu item to the File drop-down menu, between the Open and Save items. Leave the new menu item's ID as ID_FILE_IMAGEFORMAT
.
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_IMAGEFORMAT:
{
TCHAR szFileName[260] = TEXT(""); // File name
if (SUCCESS != GetBitmapLoadingName(hWnd, szFileName, ARRAYSIZE(szFileName)))
break;
FILEINFO info = { 0 }; // Make sure to fill the struct with zeros before using it
if (SUCCESS != L_FileInfo(szFileName, &info, sizeof FILEINFO, FILEINFO_TOTALPAGES, NULL))
{
MessageBox(hWnd, TEXT("Error getting file information"), TEXT("LEADTOOLS Demo"), MB_ICONERROR);
break;
}
L_TCHAR szExtension[100] = TEXT("");
L_GetFormatFileExtension(info.Format, szExtension, sizeof szExtension);
L_TCHAR szMimeType[100] = TEXT("");
L_GetFormatMimeType(info.Format, szMimeType, sizeof szMimeType);
L_TCHAR CONST szFormatMessage[] = L_TEXT(
"Image Format: %d\n"
"Information for: %s\n"
"Extension: %s\n"
"Mime Type: %s");
L_TCHAR szMessage[1024] = TEXT("");
wsprintf(szMessage, szFormatMessage,
info.Format,
info.Name,
szExtension,
szMimeType);
MessageBox(hWnd, szMessage, TEXT("File Information"), MB_ICONINFORMATION);
}
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.
Note: The
FILEINFO
structure must be filled with zeros before using it withL_FileInfo()
.
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps are followed correctly, the application starts. To test, follow the instructions below.
Select File -> Image Format.
Select the image you would like to detect extension information for and press OK.
The image below shows the result for a PNG image file that was incorrectly named with ".txt" extension.
This tutorial showed how to use the L_FileInfo
, L_GetFormatFileExtension
and L_GetFormatMimeType
functions to retrieve extension and type information from a selected image.