Take the following steps to start a project and to add some code that writes and reads pages to/from an image file:
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 "<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32 " folder and select the following DLLs:
Press the Select button and then press the OK button to add the above DLLs to the application.
| Name | Text |
| Button1 | First |
| Button2 | Previous |
| Button3 | Next |
| Button4 | Last |
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:
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.Controls.WinForms
using Leadtools;using Leadtools.Codecs;using Leadtools.Controls.WinForms;
Add an event handler to the Form1 Load event and add the following code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' Intitialize a new RasterCodecs object.Dim codecs As New RasterCodecs()' Load OCR1 to OCR4 and add them all to one imageDim pageFileName() As String = _{ _"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif", _"C:\Users\Public\Documents\LEADTOOLS Images\Ocr2.tif", _"C:\Users\Public\Documents\LEADTOOLS Images\Ocr3.tif", _"C:\Users\Public\Documents\LEADTOOLS Images\Ocr4.tif" _}' load all the files in pageFileName as pages into one imageDim image As RasterImage = NothingFor i As Integer = 0 To pageFileName.Length - 1' load the page imageDim page As RasterImage = codecs.Load(pageFileName(i))If (i = 0) Then' first image, just save it into our variableimage = pageElse' add this page into our imageimage.AddPage(page)' we do not need this page anymorepage.Dispose()End IfNext' we can now use this image in our viewer. But let us try saving into a multi-page file then reloading itDim outputFileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Ocr_All.tif"' we can now just put "image" into the viewer, but lets save it as multi-page tiff and reload itcodecs.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 pagesDim info As CodecsImageInfo = codecs.GetInformation(outputFileName, True)image = codecs.Load(outputFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, info.TotalPages)' set up the viewer into Fit modeRasterImageViewer1.Zoom(ControlSizeMode.Fit, RasterImageViewer1.ScaleFactor, RasterImageViewer1.DefaultZoomOrigin)' view this imageRasterImageViewer1.Image = imageUpdateCaption()End SubPrivate Sub UpdateCaption()Text = String.Format("Page {0} of {1}", RasterImageViewer1.Image.Page, RasterImageViewer1.Image.PageCount)End Sub
private void Form1_Load(object sender, System.EventArgs e){// Intitialize a new RasterCodecs object.RasterCodecs codecs = new RasterCodecs();// Load OCR1 to OCR4 and add them all to one imagestring[] pageFileName ={@"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif",@"C:\Users\Public\Documents\LEADTOOLS Images\Ocr2.tif",@"C:\Users\Public\Documents\LEADTOOLS Images\Ocr3.tif",@"C:\Users\Public\Documents\LEADTOOLS Images\Ocr4.tif"};// load all the files in pageFileName as pages into one imageRasterImage image = null;for(int i = 0; i < pageFileName.Length; i++){// load the page imageRasterImage page = codecs.Load(pageFileName[i]);if(i == 0){// first image, just save it into our variableimage = page;}else{// add this page into our imageimage.AddPage(page);// we do not need this page anymorepage.Dispose();}}// we can now use this image in our viewer. But let us try saving into a multi-page file then reloading itstring outputFileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr_All.tif";// we can now just put "image" into the viewer, but lets save it as multi-page tiff and reload itcodecs.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 pagesCodecsImageInfo info = codecs.GetInformation(outputFileName, true);image = codecs.Load(outputFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, info.TotalPages);// set up the viewer into Fit moderasterImageViewer1.Zoom(ControlSizeMode.Fit, RasterImageViewer1.ScaleFactor, RasterImageViewer1.DefaultZoomOrigin);// view this imagerasterImageViewer1.Image = image;UpdateCaption();}private void UpdateCaption(){Text = string.Format("Page {0} of {1}", rasterImageViewer1.Image.Page, rasterImageViewer1.Image.PageCount);}
Double-click Button1 (First) on the form to add a handler for the Click event and add the following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click' goto first pageIf (RasterImageViewer1.Image.Page <> 1) ThenRasterImageViewer1.Image.Page = 1UpdateCaption()End IfEnd Sub
private void button1_Click(object sender, System.EventArgs e){// goto first pageif(rasterImageViewer1.Image.Page != 1){rasterImageViewer1.Image.Page = 1;UpdateCaption();}}
Double-click Button2 (Previous) on the form to add a handler for the Click event and add the following code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click' goto previous pageIf (RasterImageViewer1.Image.Page > 1) ThenRasterImageViewer1.Image.Page = RasterImageViewer1.Image.Page - 1UpdateCaption()End IfEnd Sub
private void button2_Click(object sender, System.EventArgs e){// goto previous pageif(rasterImageViewer1.Image.Page > 1){rasterImageViewer1.Image.Page--;UpdateCaption();}}
Double-click Button3 (Next) on the form to add a handler for the Click event and add the following code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click' goto next pageIf (RasterImageViewer1.Image.Page < RasterImageViewer1.Image.PageCount) ThenRasterImageViewer1.Image.Page = RasterImageViewer1.Image.Page + 1UpdateCaption()End IfEnd Sub
private void button3_Click(object sender, System.EventArgs e){// goto next pageif(rasterImageViewer1.Image.Page < rasterImageViewer1.Image.PageCount){rasterImageViewer1.Image.Page++;UpdateCaption();}}
Double-click Button4 (Last) on the form to add a handler for the Click event and add the following code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click' goto last pageIf (RasterImageViewer1.Image.Page <> RasterImageViewer1.Image.PageCount) ThenRasterImageViewer1.Image.Page = RasterImageViewer1.Image.PageCountUpdateCaption()End IfEnd Sub
private void button4_Click(object sender, System.EventArgs e){// goto last pageif(rasterImageViewer1.Image.Page != rasterImageViewer1.Image.PageCount){rasterImageViewer1.Image.Page = rasterImageViewer1.Image.PageCount;UpdateCaption();}}