Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.21
Writing and Reading Pages
Take the following steps to start a project and to add some code that writes and reads pages to/from an image file:
  1. Start Visual Studio .NET.
  2. Choose File->New->Project… from the menu.
  3. In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
  4. Type the project name as " Writing and Reading Pages" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK .
  5. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to Leadtools For .NET "\LEAD Technologies\LEADTOOLS 15\Bin\DotNet\Win32 " folder and select the following DLLs:
    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.WinForms.dll
    Press the Select button and then press the OK button to add the above DLLs to the application.
  6. Make sure Form1 is in design view. Go to the toolbox (View->Toolbox) and Drag and drop an instance of RasterImageViewer to the form. If you do not have RasterImageViewer in your toolbox, select Tools->Add Remove Toolbox Items from the menu. Click Browse and then select Leadtools.WinForms.DLL from "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Bin\DotNet\Win32" and then click Open and then click OK.
  7. From the toolbox (View->Toolbox ), add 4 Button controls as follows:
    NameText
    Button1First
    Button2Previous
    Button3Next
    Button4Last
  8. Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file:

    [Visual Basic]

     
    Imports Leadtools
    Imports Leadtools.Codecs
    Imports Leadtools.WinForms            
    [C#]
     
    using Leadtools;
    using Leadtools.Codecs;
    using Leadtools.WinForms;            
  9. Add an event handler to the Form1 Load event and add the following code:

    [Visual Basic]

    
                 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       ' Intitialize a new RasterCodecs object.
       RasterCodecs.Startup()
       Dim codecs As New RasterCodecs()
    
       ' Load OCR1 to OCR4 and add them all to one image
       Dim pageFileName() As String = _
       { _
          "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr1.tif", _
          "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr2.tif", _
          "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr3.tif", _
          "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr4.tif" _
       }
    
       ' load all the files in pageFileName as pages into one image
       Dim image As RasterImage = Nothing
    
       For i As Integer = 0 To pageFileName.Length - 1
          ' load the page image
          Dim page As RasterImage = codecs.Load(pageFileName(i))
          If (i = 0) Then
             ' first image, just save it into our variable
             image = page
          Else
             ' add this page into our image
             image.AddPage(page)
    
             ' we do not need this page anymore
             page.Dispose()
          End If
       Next
    
       ' we can now use this image in our viewer.  But let us try saving into a multi-page file then reloading it
    
       Dim outputFileName As String = "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr_All.tif"
    
       ' we can now just put "image" into the viewer, but lets save it as multi-page tiff and reload it
       codecs.Save(image, outputFileName, RasterImageFormat.Tif, 1, 1, 4, 1, CodecsSavePageMode.Overwrite)
       image.Dispose()
    
       ' load this file into a new image and put in the viewer.
    
       ' we know we have four pages, but let us use the GetInformation pages to get the number of pages
       Dim info As CodecsImageInfo = codecs.GetInformation(outputFileName, True)
       image = codecs.Load(outputFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, info.TotalPages)
    
       ' setup the viewer into Fit mode
       RasterImageViewer1.SizeMode = RasterPaintSizeMode.Fit
    
       ' view this image
       RasterImageViewer1.Image = image
       UpdateCaption()
    
       RasterCodecs.Shutdown()
    End Sub
       
    Private Sub UpdateCaption()
       Text = String.Format("Page {0} of {1}", RasterImageViewer1.Image.Page, RasterImageViewer1.Image.PageCount)
    End Sub
    
    [C#]
     
    private void Form1_Load(object sender, System.EventArgs e)
    {
       // Intitialize a new RasterCodecs object.
       RasterCodecs.Startup();
       RasterCodecs codecs = new RasterCodecs();
    
       // Load OCR1 to OCR4 and add them all to one image
       string[] pageFileName =
       {
          @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr1.tif",
          @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr2.tif",
          @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr3.tif",
          @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr4.tif"
       };
    
       // load all the files in pageFileName as pages into one image
       RasterImage image = null;
    
       for(int i = 0; i < pageFileName.Length; i++)
       {
          // load the page image
          RasterImage page = codecs.Load(pageFileName[i]);
          if(i == 0)
          {
             // first image, just save it into our variable
             image = page;
          }
          else
          {
             // add this page into our image
             image.AddPage(page);
    
             // we do not need this page anymore
             page.Dispose();
          }
       }
    
       // we can now use this image in our viewer.  But let us try saving into a multi-page file then reloading it
    
       string outputFileName = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr_All.tif";
    
       // we can now just put "image" into the viewer, but lets save it as multi-page tiff and reload it
       codecs.Save(image, outputFileName, RasterImageFormat.Tif, 1, 1, 4, 1, CodecsSavePageMode.Overwrite);
       image.Dispose();
    
       // load this file into a new image and put in the viewer.
    
       // we know we have four pages, but let us use the GetInformation pages to get the number of pages
       CodecsImageInfo info = codecs.GetInformation(outputFileName, true);
       image = codecs.Load(outputFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, info.TotalPages);
    
       // setup the viewer into Fit mode
       rasterImageViewer1.SizeMode = RasterPaintSizeMode.Fit;
    
       // view this image
       rasterImageViewer1.Image = image;
       UpdateCaption();
    
       RasterCodecs.Shutdown();
    }
       
    private void UpdateCaption()
    {
       Text = string.Format("Page {0} of {1}", rasterImageViewer1.Image.Page, rasterImageViewer1.Image.PageCount);
    }
    
  10. double-click Button1 (First) on the form to add a handler for the Click event and add the following code:

    [Visual Basic]

    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       ' goto first page
       If (RasterImageViewer1.Image.Page <> 1) Then
          RasterImageViewer1.Image.Page = 1
          UpdateCaption()
       End If
    End Sub
    
    [C#]
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       // goto first page
       if(rasterImageViewer1.Image.Page != 1)
       {
          rasterImageViewer1.Image.Page = 1;
          UpdateCaption();
       }
    }
    
  11. double-click Button2 (Previous) on the form to add a handler for the Click event and add the following code:

    [Visual Basic]

    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       ' goto previous page
       If (RasterImageViewer1.Image.Page > 1) Then
          RasterImageViewer1.Image.Page = RasterImageViewer1.Image.Page - 1
          UpdateCaption()
       End If
    End Sub
    
    [C#]
    
    private void button2_Click(object sender, System.EventArgs e)
    {
       // goto previous page
       if(rasterImageViewer1.Image.Page > 1)
       {
          rasterImageViewer1.Image.Page--;
          UpdateCaption();
       }
    }
    
  12. double-click Button3 (Next) on the form to add a handler for the Click event and add the following code:

    [Visual Basic]

    
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
       ' goto next page
       If (RasterImageViewer1.Image.Page < RasterImageViewer1.Image.PageCount) Then
          RasterImageViewer1.Image.Page = RasterImageViewer1.Image.Page + 1
          UpdateCaption()
       End If
    End Sub
    
    [C#]
    
    private void button3_Click(object sender, System.EventArgs e)
    {
       // goto next page
       if(rasterImageViewer1.Image.Page < rasterImageViewer1.Image.PageCount)
       {
          rasterImageViewer1.Image.Page++;
          UpdateCaption();
       }
    }
    
  13. double-click Button4 (Last) on the form to add a handler for the Click event and add the following code:

    [Visual Basic]

    
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
       ' goto last page
       If (RasterImageViewer1.Image.Page <> RasterImageViewer1.Image.PageCount) Then
          RasterImageViewer1.Image.Page = RasterImageViewer1.Image.PageCount
          UpdateCaption()
       End If
    End Sub
    
    [C#]
    
    private void button4_Click(object sender, System.EventArgs e)
    {
       // goto last page
       if(rasterImageViewer1.Image.Page != rasterImageViewer1.Image.PageCount)
       {
          rasterImageViewer1.Image.Page = rasterImageViewer1.Image.PageCount;
          UpdateCaption();
       }
    }
    
  14. Build, and Run the program to test it.