检测图像格式和扩展Windows C DLL

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 in a Windows C/C++ API application. 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
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 theAdd References and Set a LicenseandLoad, Display, and Save Imagestutorials, before working on the检测图像格式和扩展Windows C DLLtutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in theLoad, Display, and Save Imagestutorial. If the project is not available, create it by following the steps in that tutorial.

Open the pre-compiled header file (eitherpch.horstdafx.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

Note

For a complete list of DLLs that are required for specific application features, refer toFiles 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 toSetting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS references and setting a license are covered in more detail in theAdd References and Set a Licensetutorial.

Add the Detect Image Format Code

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

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

Add a new&Image Formatmenu item to theFiledrop-down menu, between theOpenandSaveitems. Leave the new menu item's ID asID_FILE_IMAGEFORMAT.

Open the project's CPP file and navigate to theWndProcfunction. Under theswitch (wmId)statement that is below theWM_COMMANDcase, add a new case and the code below.

switch(wmId){caseID_FILE_IMAGEFORMAT:{TCHAR szFileName[260] = TEXT("");// File nameif(SUCCESS != GetBitmapLoadingName(hWnd, szFileName, ARRAYSIZE(szFileName)))break;FILEINFO info = { 0 };// Make sure to fill the struct with zeros before using itif(SUCCESS != L_FileInfo(szFileName, &info,sizeofFILEINFO, 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,sizeofszExtension);L_TCHAR szMimeType[100] = TEXT("");L_GetFormatMimeType(info.Format, szMimeType,sizeofszMimeType);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 theGetBitmapLoadingName()function, called inside the code above, is listed in theLoad, Display, and Save Imagestutorial.

Note

TheFILEINFOstructure must be filled with zeros before using it withL_FileInfo().

Run the Project

Run the project by pressingF5, or by selectingDebug -> Start Debugging.

If the steps are followed correctly, the application starts. To test, follow the instructions below.

  1. SelectFile -> Image Format.

  2. Select the image you would like to detect extension information for and pressOK.

The image below shows the result for a PNG image file that was incorrectly named with ".txt" extension.

The application runs and a message displays the extension information from the selected image file.

Wrap-up

This tutorial showed how to use theL_FileInfo,L_GetFormatFileExtensionandL_GetFormatMimeTypefunctions to retrieve extension and type information from a selected image.

See Also

iOS
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2022LEAD Technologies, Inc.All Rights Reserved.