Writing and Reading Multipage Files (C++ 5.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 RasterView Control to the dialog box as follows:

 

a.

image\btnlead.gif In the Controls toolbar, click the LEAD RasterView control icon; then click the dialog box to add the control to the dialog box.

 

b.

Double-click the new LEAD RasterView control to edit its properties.

 

c.

Change the properties ID to IDC_LEADRASTERVIEW2.

5.

image\btncmd.gif 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_LEADRasterView2 to the CTutorDlg class and link the variable to the LEAD RasterView 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_LEADRASTERVIEW2.

 

e.

Click the Add Variable... button.

 

f.

Specify m_LEADRasterView2 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:

void CTutorDlg::OnMultipage() 
{
   ILEADRasterIO *pRasterIO1=NULL;
   ILEADRasterIO *pRasterIO2=NULL;
   
   CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO1);
   CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO2);
   
   // Disable automatic repainting of the image.
   m_LEADRasterView1.SetAutoRepaint(FALSE);
   m_LEADRasterView2.SetAutoRepaint(FALSE);
   // Turn off scroll bars to make sure we use the full client area.
   m_LEADRasterView1.SetAutoScroll(FALSE);
   m_LEADRasterView2.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.
   pRasterIO1->Load(m_LEADRasterView1.GetRaster(),
      "..\\..\\..\\..\\images\\image1.cmp", 0, 0, 1 );
   pRasterIO2->Load(m_LEADRasterView2.GetRaster(),
      "..\\..\\..\\..\\images\\image2.cmp", 0, 0, 1 );
   // Save the bitmaps to a single multipage TIFF file
   pRasterIO1->Save(m_LEADRasterView1.GetRaster(),
      "c:\\combined.tif", FILE_TIF, 24, (QFactorConstants)0, SAVE_OVERWRITE );
   pRasterIO2->Save(m_LEADRasterView2.GetRaster(),
      "c:\\combined.tif", FILE_TIF, 24, (QFactorConstants)0, SAVE_APPEND );
   // Clear the bitmaps from memory
   m_LEADRasterView1.GetRaster().SetBitmap( 0 );
   m_LEADRasterView2.GetRaster().SetBitmap( 0 );
   // Get information about the images so that we can size the controls.
   pRasterIO1->GetFileInfo(m_LEADRasterView1.GetRaster(),
      "c:\\combined.tif", 1, 0 );
   pRasterIO2->GetFileInfo(m_LEADRasterView2.GetRaster(),
      "c:\\combined.tif", 2, 0 );
   // Set the variables used for preserving the aspect ratio.
   float HeightFactor1 = pRasterIO1->GetInfoHeight();
   float WidthFactor1  = pRasterIO1->GetInfoWidth();
   float HeightFactor2 = pRasterIO2->GetInfoHeight();
   float WidthFactor2  = pRasterIO2->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_LEADRasterView1.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_LEADRasterView2.MoveWindow( CRect((int)Left, (int)Top,
      (int)Left + (int)Width,
      (int)Top + (int)Height) );
   // Load the bitmaps from the multipage TIFF file
   pRasterIO1->Load( m_LEADRasterView1.GetRaster(),
      "c:\\combined.tif", 24, 1, 1 );
   pRasterIO2->Load( m_LEADRasterView2.GetRaster(),
      "c:\\combined.tif", 24, 2, 1 );
   // Set the image display sizes to match the LEAD controls
   float ImageWidth = m_LEADRasterView1.GetScaleWidth();
   float ImageHeight = m_LEADRasterView1.GetScaleHeight();
   m_LEADRasterView1.SetDstRect( 0.0f, 0.0f, ImageWidth, ImageHeight );
   m_LEADRasterView1.SetDstClipRect( 0.0f, 0.0f, ImageWidth, ImageHeight );
   // Set the image display sizes to match the LEAD controls
   ImageWidth= m_LEADRasterView2.GetScaleWidth();
   ImageHeight= m_LEADRasterView2.GetScaleHeight();
   m_LEADRasterView2.SetDstRect( 0.0f, 0.0f, ImageWidth, ImageHeight );
   m_LEADRasterView2.SetDstClipRect( 0.0f, 0.0f, ImageWidth, ImageHeight );
   // Display the images
   m_LEADRasterView1.ForceRepaint();
   m_LEADRasterView2.ForceRepaint();
   // Set the mouse pointer back to the default();
   EndWaitCursor();
   pRasterIO1->Release();
   pRasterIO2->Release();
}

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.