Take the following steps to start a project and to add some code that zooms in and out:
- Start Visual Studio .NET.
- Choose File->New->Project… from the menu.
- 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.
- Type the project name as "Zoom In and Zoom Out" in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK .
- 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\DotNet\Win32 " folder and select the following DLLs:
- Leadtools.dll
- Leadtools.Codecs.dll
- Leadtools.WinForms.dll
- Leadtools.Codecs.Cmp.dll
Click 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.WinForms.DLL from "<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32" and then click Open and then click OK.
- From the toolbox (View->Toolbox ), add 3 Button controls as follows:
Name |
Text |
Button1 |
Zoom In |
Button2 |
Zoom Out |
Button3 |
Zoom None |
- 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;
- 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
' Load an image into the viewer
Dim codecs As New RasterCodecs()
rasterImageViewer1.Image = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Sample1.cmp")
' setup the viewer interactive mode to Zoom To Rectangle
RasterImageViewer1.InteractiveMode = RasterViewerInteractiveMode.ZoomTo
' hook into the ScaleFactorChanged event to update the caption whenever the scale factor changes
AddHandler RasterImageViewer1.ScaleFactorChanged, AddressOf rasterImageViewer1_ScaleFactorChanged
' change the scale factor momentaraly to update the caption
RasterImageViewer1.BeginUpdate()
RasterImageViewer1.ScaleFactor = RasterImageViewer1.ScaleFactor + 1
RasterImageViewer1.ScaleFactor = RasterImageViewer1.ScaleFactor - 1
RasterImageViewer1.EndUpdate()
End Sub
Private Sub rasterImageViewer1_ScaleFactorChanged(ByVal sender As Object, ByVal e As EventArgs)
Text = String.Format("Current ScaleFactor: {0} - Use the buttons of click and drag a rectangle on the image to zoom to", RasterImageViewer1.ScaleFactor * 100)
End Sub
[C#]
private void Form1_Load(object sender, System.EventArgs e)
{
// Load an image into the viewer
RasterCodecs codecs = new RasterCodecs();
rasterImageViewer1.Image = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\Sample1.cmp");
// setup the viewer interactive mode to Zoom To Rectangle
rasterImageViewer1.InteractiveMode = RasterViewerInteractiveMode.ZoomTo;
// hook into the ScaleFactorChanged event to update the caption whenever the scale factor changes
rasterImageViewer1.ScaleFactorChanged += new EventHandler(rasterImageViewer1_ScaleFactorChanged);
// change the scale factor momentaraly to update the caption
rasterImageViewer1.BeginUpdate();
rasterImageViewer1.ScaleFactor++;
rasterImageViewer1.ScaleFactor--;
rasterImageViewer1.EndUpdate();
}
private void rasterImageViewer1_ScaleFactorChanged(object sender, EventArgs e)
{
Text = String.Format("Current ScaleFactor: {0} - Use the buttons of click and drag a rectangle on the image to zoom to", rasterImageViewer1.ScaleFactor * 100);
}
- Double-click Button1 (Zoom In) 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 Button2.Click
' zoom in
RasterImageViewer1.ScaleFactor *= 1.2
End Sub
[C#]
private void button1_Click(object sender, System.EventArgs e)
{
// zoom in
rasterImageViewer1.ScaleFactor *= 1.2;
}
- Double-click Button2 (Zoom OUT) 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 Button3.Click
' zoom out
RasterImageViewer1.ScaleFactor /= 1.2
End Sub
[C#]
private void button2_Click(object sender, System.EventArgs e)
{
// zoom out
rasterImageViewer1.ScaleFactor /= 1.2;
}
- Double-click Button3 (Zoom None) 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
' no zoom (100)
RasterImageViewer1.ScaleFactor = 1
End Sub
[C#]
private void button3_Click(object sender, System.EventArgs e)
{
// no zoom (100)
rasterImageViewer1.ScaleFactor = 1;
}
- Build, and Run the program to test it.