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.
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.Controls.WinForms.DLL from "<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32" and then click Open and then click OK.
From the toolbox (View->Toolbox ), add 4 Button controls as follows:
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 Leadtools
Imports Leadtools.Codecs
Imports 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
' Initialize a new RasterCodecs object.
Dim codecs As New RasterCodecs()
' Load OCR1 to OCR4 and add them all to one image
Dim 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 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 multipage file then reloading it
Dim 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 multipage 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)
' set up the viewer into Fit mode
RasterImageViewer1.Zoom(ControlSizeMode.Fit, RasterImageViewer1.ScaleFactor, RasterImageViewer1.DefaultZoomOrigin)
' view this image
RasterImageViewer1.Image = image
UpdateCaption()
End Sub
Private 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)
{
// Initialize a new RasterCodecs object.
RasterCodecs codecs = new RasterCodecs();
// Load OCR1 to OCR4 and add them all to one image
string[] 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 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 multipage file then reloading it
string outputFileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr_All.tif";
// we can now just put "image" into the viewer, but lets save it as multipage 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);
// set up the viewer into Fit mode
rasterImageViewer1.Zoom(ControlSizeMode.Fit, RasterImageViewer1.ScaleFactor, RasterImageViewer1.DefaultZoomOrigin);
// view this image
rasterImageViewer1.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 page
If (RasterImageViewer1.Image.Page <> 1) Then
RasterImageViewer1.Image.Page = 1
UpdateCaption()
End If
End Sub
private void button1_Click(object sender, System.EventArgs e)
{
// goto first page
if(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 page
If (RasterImageViewer1.Image.Page > 1) Then
RasterImageViewer1.Image.Page = RasterImageViewer1.Image.Page - 1
UpdateCaption()
End If
End Sub
private void button2_Click(object sender, System.EventArgs e)
{
// goto previous page
if(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 page
If (RasterImageViewer1.Image.Page < RasterImageViewer1.Image.PageCount) Then
RasterImageViewer1.Image.Page = RasterImageViewer1.Image.Page + 1
UpdateCaption()
End If
End Sub
private void button3_Click(object sender, System.EventArgs e)
{
// goto next page
if(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 page
If (RasterImageViewer1.Image.Page <> RasterImageViewer1.Image.PageCount) Then
RasterImageViewer1.Image.Page = RasterImageViewer1.Image.PageCount
UpdateCaption()
End If
End Sub
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();
}
}