Converting Multipage Images Using the LEADTOOLS GUI Dialog

Take the following steps to create and run a program that converts image files to most common formats. This tutorial covers image format conversion for single-page and multi-page formats such as PDF or TIFF.

NOTE: Blue highlights denote an instance of a LEADTOOLS object.

1.

Start Visual Studio 2008.

2.

Choose File->New->Project…from menu.

3.

In the New Project dialog box, choose Visual C++  in the Projects Types, and choose Win32 Project in the Templates.

4.

Type the project name as ImageConversion in the Project Name field.

5.

 In the Location field, use the Browse button to navigate to the Examples subdirectory (such as C:\LEAD Technologies\LEADTOOLS 17\Examples\CDLL). Uncheck both Create directory for solution and Add to Source Control options.  Choose OK.

6.

Choose Next> in the Win32 Application Wizard.

7.

Choose Windows application and Empty project and do not add common header files for ATL.  Then choose Finish.  Three empty folders will be created titled: Header Files, Resource Files, and Source Files. 

Folders Content
: Inside Visual Studio 2008 in the Solution Explorer review the folders.  They must be empty. If there are any objects or files in them, delete this project and restart from the first step.

Project Structure: Once the project is acceptable, in Windows Explorer go to the newly created project ImageConversion folder in the Examples directory. Make sure the project folder named ImageConversion exists in the correct directory. Inside the ImageConversion folder, there should not be any additional folders, only files of type .ncb, .sln, .suo, .vcproj, and .user. If there are any folders other than these files, remove the entire project folder and redo all previous steps.

8.

In Solution Explorer, right click on the Header Files folder, then Add->New Item.  In Categories, choose Code.  In the Templates, select Header File (.h). In Name field type ImageConversion. Choose Add.  Inspect the newly created header file and make sure it is empty.

9.

Add the following code to the ImageConversion.h in the Header File:

#define IDM_OPEN    100

#define IDM_CONVERT 200

#define IDM_EXIT    300

 

L_BOOL InitApplication (HINSTANCE hInstance);
L_BOOL InitInstance (HINSTANCE hInstance, L_INT nCmdShow);
LRESULT CALLBACK MainWndProc (HWND hWnd, L_UINT Message, WPARAM wParam, LPARAM lParam);

 

VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify);
VOID Window_OnDestroy (HWND hWnd);

 

extern L_INT ShowFileConvertDialog ( HWND hWnd, LPFILECONVERSIONDLGPARAMS pDlgParams ) ;
FILECONVERSIONDLGPARAMS          FileConversionParams ;

 

10.

In Solution Explorer, right click on the Header Files folder, then Add->New Item.  In Categories, choose Code.  In the Templates, select Header File (.h). In Name field type stdAfx. Choose Add.  Inspect the newly created header file and make sure it is empty.  Then add the following code:

 

#if !defined(INC_STDAFX_H)

#define INC_STDAFX_H

#include <windows.h>            /* Required for all Windows applications.*/

#include <windowsx.h>           /* Needed for message crackers.*/

#include <tchar.h>

#include <commdlg.h>            /* Windows' header for common dialog box. */

#include <string.h>             /* Standard C header files.*/

#include <ctype.h>

#include "..\..\..\include\ltdlg.h"
#include "..\common\ltDemos.h"

#endif 

11.

In Solution Explorer, right click on the Resource Files folder, then Add->New Item.  In Categories, choose Resource.  In the Templates, select Resource File(.rc). In Name field type ImageConversion. Choose Add.

12.

Right click on ImageConversion.rc in the Resource Files, choose View Code.  Select All text and delete its content.  To this same file add the following code:

#include "Windows.h"

#include "ImageConversion.h"

 

MENU_MAIN MENU 

BEGIN

    POPUP "&File"

    BEGIN

        MENUITEM "&Convert Image...",           IDM_CONVERT

        MENUITEM "&Exit..."                    IDM_EXIT

    END

END

LEAD ICON "..\\..\\Resources\\lvsample.ico"

13.

In the Header Files, click on the resource.h header file and delete from the project.

14.

In the Source Files, right click on the folder.  Choose Add->New Item. Choose Code in Categories, and C++ File(.cpp) in Templates. Type ImageConversion in the name field and choose Add.

15.

Right click on ImageConversion.cpp and choose Open.  Add to it the following code:

 

/*-------------------[Image Conversion Tutorial]-------------------------------------*/

#include "stdAfx.h"

#include "ImageConversion.h"           /* Application specific header file.*/

 

 

/*---[WinMain]---------------------------------------------------------------

  Syntax:     int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,

                                                        LPSTR lpCmdLine, int nCmdShow )

  Parameters: hInstance       Current instance.

              hPrevInstance   previous instance.

              lpCmdLine       command line.

              nCmdShow        show-window type.

  Prototype:  Windows.h

  Notes:      Windows main function, calls initialization function

              and processes message loop.

  --------------------------------------------------------------------------*/

int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

   UNREFERENCED_PARAMETER(lpCmdLine);

   MSG msg; 

   if (!hPrevInstance)          /* Other instances of app running? */

      if (!InitApplication (hInstance))   /* Initialize shared things. */

         return (FALSE);        /* Exits if unable to initialize. */

   if (!InitInstance (hInstance, nCmdShow))  /* Do instance initializations. */

      return (FALSE);

   L_DlgInit(DLG_INIT_COLOR);

   /* Acquire and dispatch messages until a WM_QUIT message is received. */

   while (GetMessage (&msg, NULL, 0, 0))

   {

      TranslateMessage (&msg);  /* Translates virtual key codes. */

      DispatchMessage (&msg);   /* Dispatches message to window. */

   }

   return (int)msg.wParam;         /* Returns the value from PostQuitMessage. */

}

 

/*---[InitApplication]------------------------------------------------------

  Syntax:     L_BOOL InitApplication( HANDLE hInstance )

  Parameters: hInstance    Current instance.

  Notes:      Initializes window class structure and registers window class.

  --------------------------------------------------------------------------*/

L_BOOL InitApplication (HINSTANCE hInstance)

{

   WNDCLASS wcWindowClass;

   wcWindowClass.style = 0;     /* Class style(s). */

   wcWindowClass.lpfnWndProc = MainWndProc;  /* Function to retrieve messages */

   /* for windows of this class. */

   wcWindowClass.cbClsExtra = 0;/* No per-class extra data. */

   wcWindowClass.cbWndExtra = 0;/* No per-window extra data. */

   wcWindowClass.hInstance = hInstance;   /* Owner. */

   wcWindowClass.hIcon = LoadIcon (hInstance, TEXT("LEAD"));

   wcWindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);

   wcWindowClass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);

   wcWindowClass.lpszMenuName = TEXT("MENU_MAIN"); /* Menu Name. */

   wcWindowClass.lpszClassName = TEXT("LEADWClass");  /* Class Name. */

   /* Register the window class and return the result code. */

   return (RegisterClass (&wcWindowClass));

}

 

/*---[InitInstance]----------------------------------------------------------

  Syntax:     L_BOOL InitInstance( HINSTANCE hInstance, L_INT nCmdShow )

  Parameters: hInstance    Current instance.

              nCmdShow     Param for first ShowWindow() call.

  Notes:      Saves instance handle and creates main window.

  --------------------------------------------------------------------------*/

L_BOOL InitInstance (HINSTANCE hInstance, L_INT nCmdShow)

{

   HWND hWnd;                   /* Window handle. */

   /* Create a main window for this application instance. */

   hWnd = CreateWindow (

                        TEXT("LEADWClass"),
                        TEXT("LEADTOOLS Image Format conversion Tutorial"),  /* Window title */

                        WS_OVERLAPPEDWINDOW, /* Window style. */

                        CW_USEDEFAULT, /* Default horizontal position. */

                        CW_USEDEFAULT, /* Default vertical position. */

                        CW_USEDEFAULT, /* Default width. */

                        CW_USEDEFAULT, /* Default height. */

                        NULL,   /* Overlapped windows have no parent. */

                        NULL,   /* Use the window class menu. */

                        hInstance,  /* This instance owns this window. */

                        NULL    /* Pointer not needed. */

         );

   if (hWnd == NULL)

      return (FALSE);           /* If window could not be created, return "failure". */

   ShowWindow (hWnd, nCmdShow); /* Show the window. */

     return (TRUE);

}

 

/*---[MainWndProc]-----------------------------------------------------------

  Syntax:     LRESULT CALLBACK MainWndProc( HWND hWnd, L_UINT message,

                                                WPARAM wParam, LPARAM lParam )

  Parameters: hWnd            Window handle.

              message         Type of message.

              wParam          Additional information.

              lParam          Additional information.

  Notes:      This procedure is responsible for handling window messages.

  --------------------------------------------------------------------------*/

LRESULT CALLBACK MainWndProc (HWND hWnd, L_UINT Message, WPARAM wParam, LPARAM lParam)

{

   switch (Message)

   {

      HANDLE_MSG (hWnd, WM_COMMAND, Window_OnCommand);

      HANDLE_MSG (hWnd, WM_DESTROY, Window_OnDestroy);

   }

   return DefWindowProc (hWnd, Message, wParam, lParam);

}

 

/*---[Window_OnDestroy]----------------------------------------------------

  Syntax:     VOID Window_OnDestroy( HWND hWnd );

  Parameters: hWnd            Window handle.

  Notes:      This procedure is responsible for handling WM_DESTROY.

  --------------------------------------------------------------------------*/

VOID Window_OnDestroy (HWND hWnd)

{

   UNREFERENCED_PARAMETER (hWnd);

   L_DlgFree();

   PostQuitMessage (0);         /* Post WM_QUIT, to end the application. */

   return;

}

 

/*---[Window_OnCommand]-----------------------------------------------------

  Syntax:     VOID Window_OnCommand( HWND hWnd, L_INT id, HWND hwndCtl,

                                     UINT codeNotify )

  Parameters: hWnd            Window handle.

              id              Menu item or Control ID.

              hwndCtl         0 if menu item selected, else window handle of

                              the control.

              codeNotify      1 if accelerator keystroke, else notification

                              code, such as BN_CLICKED.

  Notes:      This procedure is responsible for handling the call to the LEADTOOLS

               image format conversion dialog procedure ShowFileConvertDialog or to exit

               application.

  --------------------------------------------------------------------------*/

VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify)

{   

   L_INT nRet;

    switch (id)

   {   

      case IDM_CONVERT:

            nRet = ShowFileConvertDialog (hWnd,&FileConversionParams ) ; /* Call up of LEADTOOLS dialog box */

         break;

      case IDM_EXIT:

            DestroyWindow (hWnd);

         break;

   }

   return;

}

 

 

/*---[ShowFileConvertDialog]-----------------------------------------------------

Syntax: L_INT ShowFileConvertDialog ( HWND hWnd, LPFILECONVERSIONDLGPARAMS pDlgParams )

parameters: hWnd        Window handle

            pDlgParams  LEADTOOLS struct

Notes:      This procedure is responsible to provide the parameters to the LEADTOOLS

            dialog to be used for image format conversion.

--------------------------------------------------------------------------------*/

L_INT ShowFileConvertDialog ( HWND hWnd, LPFILECONVERSIONDLGPARAMS pDlgParams )

{   

   pDlgParams->uStructSize           = sizeof ( FILECONVERSIONDLGPARAMS ) ;
   pDlgParams->uOverwrite            = DLG_FILECONVERSION_OVERWRITE_SKIP ;
   pDlgParams->pFileFormats          = NULL ;
   pDlgParams->nFileFormatsCount     = 0 ;
   pDlgParams->bUseLogReport         = TRUE ;
   pDlgParams->bRemoveSrcFile        = FALSE ;
   pDlgParams->bShowFullPath         = TRUE ;
   pDlgParams->bUseOriginalFolder    = TRUE ;
   pDlgParams->pszSrcFileList        = NULL ;
   pDlgParams->uDlgFlags             = DLG_FILECONVERSION_SHOW_PREVIEW        |
                                       DLG_FILECONVERSION_SHOW_LOADOPTIONS    |
                                       DLG_FILECONVERSION_SHOW_FILEINFO       |
                                       DLG_FILECONVERSION_SHOW_PREVIEW_PAGES  |
                                       DLG_FILECONVERSION_SHOW_RESIZE         |
                                       DLG_FILECONVERSION_SHOW_ROTATE         |
                                       DLG_FILECONVERSION_SHOW_NAMINGTEMPLATE |
                                       DLG_FILECONVERSION_SHOW_OVERWRITE      |
                                       DLG_FILECONVERSION_SHOW_OVERWRITE_ALL  |
                                       DLG_FILECONVERSION_SHOW_ADD            |
                                       DLG_FILECONVERSION_SHOW_ADDFOLDER      |
                                       DLG_FILECONVERSION_SHOW_REMOVE         |
                                       DLG_FILECONVERSION_SHOW_SELECTALL      |
                                       DLG_FILECONVERSION_SHOW_DELETEORIGINAL |
                                       DLG_FILECONVERSION_SHOW_NEWFORMATSUPDATES;
   return L_DlgFileConversion ( hWnd, pDlgParams ) ;    

}

16.

Compile and run the code to test it.

NOTE: For multiple-page conversion set dialog options in the following order:

1.       Load the file or image to convert.

2.       Source Files->Options->All Pages, Choose OK.

3.       Resulting Files->Overwrite-> Choose Replace.

4.       File Type->Options-> Choose Append.

5.       Optionally: Subtype-> Choose Uncompressed.

For more information, refer to:

Implementing PDF Plug-in Features

LEADTOOLS Basic Data Types

L_DLGINIT

L_DLGFREE

L_DlgFileConversion

See also:

Conversion Considerations

Introduction: Image Display

Printing an Image

Tutorials

L_FileInfo

L_SaveBitmap