Available in the LEADTOOLS Imaging toolkit. |
Writing and Reading Multipage Files (C++ 4.0 and later)
Take the following steps to create a multipage file, then load and display the images from the file. Only TIFF (MPT) and PCX (DCX) formats support multipage files.
1. Start with the project that you created in Loading and Displaying an Image.
2. Go to the Project WorkSpace and click the ResourceView tab.
3. Double-click Dialog and double-click IDD_TUTOR_DIALOG to bring up the application's dialog box.
4. Add a new LEAD Control to the dialog box as follows:
a. In the Controls toolbar, click the LEAD control icon; then click the dialog box to add the control to the dialog box.
b. Double-click the new LEAD control to edit its properties.
c. Change the properties ID to IDC_LEAD2.
5. Add a new button at the top of the dialog box. To do this, select the button control on the Controls toolbar. Then, click and drag to position the button on the dialog box.
6. Double-click the button and change IDC_BUTTON1 to IDC_MULTIPAGE. Also, set the Caption to &Multipage.
7. Press Ctrl-F4 to close all windows back to the Project Workspace.
8. Do the following to add m_Lead2 to the CTutorDlg class and link the variable to the LEAD control using dynamic data exchange:
a. From the main menu, select View > ClassWizard. (The MFC ClassWizard dialog box appears.)
b. Click the Member Variables tab.
c. In the Class Name box, select CTutorDlg.
d. In the Control IDs list, select IDC_LEAD2.
e. Click the Add Variable... button.
f. Specify m_Lead2 as the variable name, and Control as the category.
g. Click OK to close the ClassWizard.
9. Press Ctrl-W to go to the MFC Class Wizard; then do the following:
a. Click the Message Maps tab.
b. In the Class Name combo box, select CTutorDlg.
c. In the Object IDs list box, select IDC_MULTIPAGE.
d. In the Messages list box, select BN_CLICKED.
e. Click the Add function button. Choose OK for the default function name (OnMultipage).
f. Click the Edit Code button to start entering the code.
10. Enter new code as follows:
// Disable automatic repainting of the image.
m_Lead1.SetAutoRepaint(FALSE);
m_Lead2.SetAutoRepaint(FALSE);
// Turn off scroll bars to make sure we use the full client area.
m_Lead1.SetAutoScroll(FALSE);
m_Lead2.SetAutoScroll(FALSE);
// Position and size the main form so that it takes up most of the screen.
float Width = (float)GetSystemMetrics(SM_CXSCREEN) * 0.9f;
float Height = (float)GetSystemMetrics(SM_CYSCREEN)* 0.8f;
float Left = ((float)GetSystemMetrics(SM_CXSCREEN) - Width) / 2;
float Top = ((float)GetSystemMetrics(SM_CYSCREEN) - Height) / 2;
// Display the form and set the mouse pointer to an hourglass.
MoveWindow((int) Left, (int) Top, (int) Width, (int) Height);
BeginWaitCursor();
// Load the bitmaps. These path names may be different on your system.
m_Lead1.Load( "c:\\lead\\images\\image1.cmp", 0, 0, 1 );
m_Lead2.Load( "c:\\lead\\images\\image2.cmp", 0, 0, 1 );
// Save the bitmaps to a single multipage TIFF file
m_Lead1.Save( "c:\\lead\\images\\combined.tif", FILE_TIF, 24, 0, SAVE_OVERWRITE );
m_Lead2.Save( "c:\\lead\\images\\combined.tif", FILE_TIF, 24, 0, SAVE_APPEND );
// Clear the bitmaps from memory
m_Lead1.SetBitmap( 0 );
m_Lead2.SetBitmap( 0 );
// Get information about the images so that we can size the controls.
m_Lead1.GetFileInfo( "c:\\lead\\images\\combined.tif", 1, 0 );
m_Lead2.GetFileInfo( "c:\\lead\\images\\combined.tif", 2, 0 );
// Set the variables used for preserving the aspect ratio.
float HeightFactor1 = m_Lead1.GetInfoHeight();
float WidthFactor1 = m_Lead1.GetInfoWidth();
float HeightFactor2 = m_Lead2.GetInfoHeight();
float WidthFactor2 = m_Lead2.GetInfoWidth();
CRect rcWindow;
GetClientRect(rcWindow);
float HeightAllowed = rcWindow.Height() * 0.8f;
float WidthAllowed = rcWindow.Width() * 0.45f;
// Center each LEAD control on half of the form, preserving the aspect ratio.
// Check to see if using the maximum width will make the image too tall.
// Set the dimensions based on the result.
if( (WidthAllowed * HeightFactor1) / WidthFactor1 < HeightAllowed )
{
Left = ( (rcWindow.Width() / 4) - (WidthAllowed / 2) );
Width = WidthAllowed;
Height = ((Width * HeightFactor1) / WidthFactor1);
Top = ((rcWindow.Height() - Height) / 2);
}
else
{
Top = ((rcWindow.Height() - HeightAllowed) / 2);
Height = HeightAllowed;
Width = ((Height * WidthFactor1) / HeightFactor1);
Left = ((rcWindow.Width() / 4) - (Width / 2));
}
m_Lead1.MoveWindow( CRect((int)Left, (int)Top, (int)Left + (int)Width, (int)Top + (int)Height) );
if( (WidthAllowed * HeightFactor2) / WidthFactor2 < HeightAllowed )
{
Left = ((rcWindow.Width() * 3/4) - (WidthAllowed / 2));
Width = WidthAllowed;
Height = ((Width * HeightFactor2) / WidthFactor2);
Top = ((rcWindow.Height() - Height) / 2);
}
else
{
Top = (rcWindow.Height() - HeightAllowed) / 2;
Height = HeightAllowed;
Width = ((Height * WidthFactor2) / HeightFactor2);
Left = ((rcWindow.Width() * 3 / 4) - (Width / 2));
}
m_Lead2.MoveWindow( CRect((int)Left, (int)Top, (int)Left + (int)Width, (int)Top + (int)Height) );
// Load the bitmaps from the multipage TIFF file
m_Lead1.Load( "c:\\lead\\images\\combined.tif", 24, 1, 1 );
m_Lead2.Load( "c:\\lead\\images\\combined.tif", 24, 2, 1 );
// Set the image display sizes to match the LEAD controls
float ImageWidth = m_Lead1.GetScaleWidth();
float ImageHeight = m_Lead1.GetScaleHeight();
m_Lead1.SetDstRect( 0.0f, 0.0f, ImageWidth, ImageHeight );
m_Lead1.SetDstClipRect( 0.0f, 0.0f, ImageWidth, ImageHeight );
// Set the image display sizes to match the LEAD controls
ImageWidth= m_Lead2.GetScaleWidth();
ImageHeight= m_Lead2.GetScaleHeight();
m_Lead2.SetDstRect( 0.0f, 0.0f, ImageWidth, ImageHeight );
m_Lead2.SetDstClipRect( 0.0f, 0.0f, ImageWidth, ImageHeight );
// Display the images
m_Lead1.ForceRepaint();
m_Lead2.ForceRepaint();
// Set the mouse pointer back to the default();
EndWaitCursor();
11. Rebuild and run the application. Running the application may take a long time because it will load two images, write both of them into a TIF file and read them again.