ImageGear .NET v24.12 - Updated
WinForms Application
User Guide > Getting Started > Tutorial: Create Your First Project > WinForms Application

In this tutorial, you will configure a C# or VB.NET project for a Windows Forms application and use ImageGear .NET capabilities. You will also learn how to load a document, then view, zoom, and rotate it.

This tutorial also makes use of the ImageGear .NET GUI components. Steps to add these components to your Visual Studio installation can be found under Installing GUI Components.

The following tutorial refers specifically to 64-bit installations; for 32-bit installations:

Using the desired version of Visual Studio (2010 or later):

  1. Create a new "Windows Forms Application" project, using C# or VB.NET and name the project: IG_Tutorial_WinForms.
  2. If you installed IG NET 64-bit, using the Configuration Manager, create a new project platform (if you don’t have one already) for x64. Make sure your project is set to compile targeting Debug and x64. Make sure you now have $YOURLOCALPROJ\bin\x64\Debug\, and if is not there, create it.
  3. Add references and required resources into your projects in one of the following ways:
    • Recommended: use our NuGet Packages. For this project, you need the following packages:
      Accusoft.ImageGear.WinForms.nupkg (https://www.nuget.org/packages/Accusoft.ImageGear.WinForms/)
    • Manually:
      1. Copy all files (and folders) inside $INSTALLDIR\ImageGear .NET v24 64-bit\Bin\ to your local output bin directory in your project (i.e., $YOURLOCALPROJ\bin\x64\Debug\ ).
      2. Add the following references to your project from $YOURLOCALPROJ\bin\x64\Debug\:
        • ImageGear24.Core.dll
        • ImageGear24.Evaluation.dll
        • ImageGear24.Formats.Common.dll
        • ImageGear24.Presentation.dll
        • ImageGear24.Windows.Forms.dll

    Your output target directory should be set to $YOURLOCALPROJ\bin\x64\Debug\  

  4. Next, you will add some pertinent controls to your form. For this tutorial, keep the default names for the controls:
    1. First, create a menu on the form. From the Windows Forms toolbox, drag a MenuStrip control onto the form:
      1. Create three MenuItems called File, View, and Processing.
      2. Under the File menu, add Load Page
      3. Under the View menu, add Zoom In and Zoom Out.
      4. Under the Processing menu, add Rotate 90.
      5. Double-click each added menu item to create a Click Event Handler.
    2. Second, also from the toolbox, drag the ImGearPageView control onto the form. This control will be used to display the document loaded. Set the Dock (in the Layout group) property of the imGearPageView1 control to Fill. This will cause the control to resize as the form resizes.
  5. At this point your project is ready for some code. The following code can be used to load a document, view it using an ImGearPageView control, zoom in/zoom out on it and rotate it. In the next step, we will go over some areas of this sample code in more detail.
    C#
    Copy Code
    using System;
    using System.Windows.Forms;
    using System.IO;
    using System.Diagnostics;
    
    using ImageGear.Core;
    using ImageGear.Display;
    using ImageGear.Processing;
    using ImageGear.Formats;
    using ImageGear.Evaluation;
    
    namespace IG_Tutorial_WinForms
    {
       public partial class Form1 : Form
       {
    
           // Hold the image data.
           private ImGearPage igPage = null;
           // Control how the page is displayed.
           private ImGearPageDisplay imGearPageDisplay = null;
    
           public Form1()
           {
               // Initialize license for a Deployment(Runtime) license.
               //***The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey
               //methods must be called to distribute the runtime.***
               //ImGearLicense.SetSolutionName("YourSolutionName");
               //ImGearLicense.SetSolutionKey(12345, 12345, 12345, 12345);
               //Manually Reported Runtime licenses also require the following method
               //call to SetOEMLicenseKey.
               //ImGearLicense.SetOEMLicenseKey("2.0.AStringForOEMLicensing...");
    
               // Initialize evaluation license.
               ImGearEvaluationManager.Initialize();
    
               // Initialize common formats.
               ImGearCommonFormats.Initialize();
    
               InitializeComponent();
           }
    
           private void loadPageToolStripMenuItem_Click(object sender, EventArgs e)
           {
               OpenFileDialog openFileDialog1 = new OpenFileDialog();
               openFileDialog1.Filter =
               ImGearFileFormats.GetSavingFilter(ImGearSavingFormats.UNKNOWN);
    
               if (DialogResult.OK == openFileDialog1.ShowDialog())
               {
                   using (FileStream stream =
                       new FileStream(openFileDialog1.FileName, FileMode.Open,
                           FileAccess.Read, FileShare.Read))
                   {
                       try
                       {
                           // Load the image into the page.
                           igPage = ImGearFileFormats.LoadPage(stream, 0);
                       }
                       catch (ImGearException ex)
                       {
                           Debug.WriteLine(ex.Message);
                       }
                   }
                   if (null != igPage && null != igPage.DIB &&
                   !igPage.DIB.IsEmpty())
                   {
                       // Create a new page display.
                       imGearPageDisplay = new ImGearPageDisplay(igPage);
                       // Associate the page display with the page view.
                       imGearPageView1.Display = imGearPageDisplay;
                       // Cause the page view to repaint.
                       imGearPageView1.Invalidate();
                   }
               }
           }
    
           private void zoomInToolStripMenuItem_Click(object sender, EventArgs e)
           {
               if (null != imGearPageDisplay && !imGearPageDisplay.Page.DIB.IsEmpty())
               {
                   // Get the current zoom info.
                   ImGearZoomInfo igZoomInfo = imGearPageDisplay.GetZoomInfo(imGearPageView1);
    
                   // Increase the zoom.
                   igZoomInfo.Horizontal.Value = igZoomInfo.Horizontal.Value * 1.25;
                   igZoomInfo.Vertical.Value = igZoomInfo.Vertical.Value * 1.25;
                   igZoomInfo.Horizontal.Fixed = true;
                   igZoomInfo.Vertical.Fixed = true;
    
                   // Set the new zoom values and repaint the view.
                   imGearPageDisplay.UpdateZoomFrom(igZoomInfo);
                   imGearPageView1.Invalidate();
               }
           }
    
           private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e)
           {
               if (null != imGearPageDisplay && !imGearPageDisplay.Page.DIB.IsEmpty())
               {
                   // Get the current zoom info.
                   ImGearZoomInfo igZoomInfo = imGearPageDisplay.GetZoomInfo(imGearPageView1);
    
                   igZoomInfo.Horizontal.Value = igZoomInfo.Horizontal.Value * (1 / 1.25);
                   igZoomInfo.Vertical.Value = igZoomInfo.Vertical.Value * (1 / 1.25);
                   igZoomInfo.Horizontal.Fixed = true;
                   igZoomInfo.Vertical.Fixed = true;
                   // Set the new zoom values and repaint the view.
                   imGearPageDisplay.UpdateZoomFrom(igZoomInfo);
                   imGearPageView1.Invalidate();
               }
           }
    
           private void rotate90ToolStripMenuItem_Click(object sender, EventArgs e)
           {
               if (null != imGearPageDisplay && null != igPage && !imGearPageDisplay.Page.DIB.IsEmpty())
               {
                   try
                   {
                       // Rotate the given page 90 degrees using ImGearRotationValues.VALUE_90.
                       // ImGearRotationValues.VALUE_90 is the constant value for indicating the amount of the rotation.
                       // It may be changed for other values, e.g. ImGearRotationValues.VALUE_180, ImGearRotationValues.VALUE_270.
                       ImGearProcessing.Rotate(igPage, ImGearRotationValues.VALUE_90);
                       imGearPageView1.Invalidate();
                   }
                   catch (ImGearException ex)
                   {
                       Debug.WriteLine(ex.Message);
                   }
               }
           }
    
       }
    }
    VB.NET
    Copy Code
    Imports System.IO
    Imports System.Diagnostics
    
    Imports ImageGear
    Imports ImageGear.Core
    Imports ImageGear.Windows.Forms
    Imports ImageGear.Display
    Imports ImageGear.Processing
    Imports ImageGear.Formats
    Imports ImageGear.Evaluation
    
    Public Class Form1
    
       ' Hold the image data.
       Private igPage As ImGearPage
       ' Control how the page is displayed.
       Private igPageDisplay As ImGearPageDisplay
    
       Public Sub New()
    
           ' Initialize license for a Deployment(Runtime) license.
           '***The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey
           'methods must be called to distribute the runtime.***
           'ImGearLicense.SetSolutionName("YourSolutionName");
           'ImGearLicense.SetSolutionKey(12345, 12345, 12345, 12345);
           'Manually Reported Runtime licenses also require the following method
           'call to SetOEMLicenseKey.
           'ImGearLicense.SetOEMLicenseKey("2.0.AStringForOEMLicensing...");
    
           'Initialize evaluation license.
           ImGearEvaluationManager.Initialize()
    
           'Initialize common formats.
           ImGearCommonFormats.Initialize()
    
           ' This call is required by the designer.
           InitializeComponent()
    
       End Sub
    
       Private Sub LoadPageToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles LoadPageToolStripMenuItem.Click
           Dim openFileDialog1 As New OpenFileDialog
           openFileDialog1.Filter = "All files (*.*)|*.*"
           openFileDialog1.FilterIndex = 1
           openFileDialog1.RestoreDirectory = True
           If openFileDialog1.ShowDialog = DialogResult.OK Then
               Using stream As New FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read)
                   Try
                       ' Load the image into the page.
                       igPage = ImGearFileFormats.LoadPage(stream, 0)
                   Catch ex As ImGearException
                       Debug.WriteLine(ex.Message)
                   End Try
                   If Not igPage Is Nothing AndAlso Not igPage.DIB Is Nothing Then
                       ' Create a new page display.
                       igPageDisplay = New ImGearPageDisplay(igPage)
                       ' Associate the page display with the page view.
                       ImGearPageView1.Display = igPageDisplay
                       ' Cause the page view to repaint.
                       ImGearPageView1.Invalidate()
                   End If
               End Using
           End If
       End Sub
    
       Private Sub ZoomInToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ZoomInToolStripMenuItem.Click
           If Not igPageDisplay Is Nothing AndAlso Not igPageDisplay.Page.DIB.IsEmpty() Then
               ' Get the current zoom info.
               Dim igZoomInfo As ImGearZoomInfo
               igZoomInfo = igPageDisplay.GetZoomInfo(ImGearPageView1)
               ' Increase the zoom.
               igZoomInfo.Horizontal.Value = igZoomInfo.Horizontal.Value * 1.25
               igZoomInfo.Vertical.Value = igZoomInfo.Vertical.Value * 1.25
               igZoomInfo.Horizontal.Fixed = True
               igZoomInfo.Vertical.Fixed = True
               ' Set the new zoom values and repaint the view.
               igPageDisplay.UpdateZoomFrom(igZoomInfo)
               ImGearPageView1.Invalidate()
           End If
       End Sub
    
       Private Sub ZoomOutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ZoomOutToolStripMenuItem.Click
           If Not igPageDisplay Is Nothing AndAlso Not igPageDisplay.Page.DIB.IsEmpty() Then
               ' Get the current zoom info.
               Dim igZoomInfo As ImGearZoomInfo
               igZoomInfo = igPageDisplay.GetZoomInfo(ImGearPageView1)
               ' Decrease the zoom.
               igZoomInfo.Horizontal.Value = igZoomInfo.Horizontal.Value / 1.25
               igZoomInfo.Vertical.Value = igZoomInfo.Vertical.Value / 1.25
               igZoomInfo.Horizontal.Fixed = True
               igZoomInfo.Vertical.Fixed = True
               ' Set the new zoom values and repaint the view.
               igPageDisplay.UpdateZoomFrom(igZoomInfo)
               ImGearPageView1.Invalidate()
           End If
       End Sub
    
       Private Sub Rotate90ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles Rotate90ToolStripMenuItem.Click
           If Not igPage Is Nothing Then
               Try
                   ' Rotate the given page 90 degrees using ImGearRotationValues.VALUE_90.
                   ' ImGearRotationValues.VALUE_90 is the constant value for indicating the amount of the rotation.
                   ' It may be changed for other values, e.g. ImGearRotationValues.VALUE_180, ImGearRotationValues.VALUE_270.
                   ImGearProcessing.Rotate(igPage, ImGearRotationValues.VALUE_90)
               Catch ex As Exception
                   Console.WriteLine(ex.Message)
               End Try
               ImGearPageView1.Invalidate()
           End If
       End Sub
    End Class
  6. If you are using a Deployment (Runtime) licensing, add the license initialization code to the Form1 constructor. This is not necessary if you are using an Evaluation or Development (Toolkit) license. Please add the ImGearEvaluationManager.Initialize() call if you are evaluating the product.
    C#
    Copy Code
           public Form1()
           {
               // Initialize license for a Deployment(Runtime) license.
               //***The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey
               //methods must be called to distribute the runtime.***
               //ImGearLicense.SetSolutionName("YourSolutionName");
               //ImGearLicense.SetSolutionKey(12345, 12345, 12345, 12345);
               //Manually Reported Runtime licenses also require the following method
               //call to SetOEMLicenseKey.
               //ImGearLicense.SetOEMLicenseKey("2.0.AStringForOEMLicensing...");
    
               // Initialize evaluation license.
               ImGearEvaluationManager.Initialize();
    
               // Initialize common formats.
               ImGearCommonFormats.Initialize();
    
               InitializeComponent();
           }
    VB.NET
    Copy Code
       Public Sub New()
    
           ' Initialize license for a Deployment(Runtime) license.
           '***The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey
           'methods must be called to distribute the runtime.***
           'ImGearLicense.SetSolutionName("YourSolutionName");
           'ImGearLicense.SetSolutionKey(12345, 12345, 12345, 12345);
           'Manually Reported Runtime licenses also require the following method
           'call to SetOEMLicenseKey.
           'ImGearLicense.SetOEMLicenseKey("2.0.AStringForOEMLicensing...");
    
           'Initialize evaluation license.
           ImGearEvaluationManager.Initialize()
    
           'Initialize common formats.
           ImGearCommonFormats.Initialize()
    
           ' This call is required by the designer.
           InitializeComponent()
    
       End Sub
  7. Load and display a document using:
    C#
    Copy Code
               OpenFileDialog openFileDialog1 = new OpenFileDialog();
               openFileDialog1.Filter =
               ImGearFileFormats.GetSavingFilter(ImGearSavingFormats.UNKNOWN);
    
               if (DialogResult.OK == openFileDialog1.ShowDialog())
               {
                   using (FileStream stream =
                       new FileStream(openFileDialog1.FileName, FileMode.Open,
                           FileAccess.Read, FileShare.Read))
                   {
                       try
                       {
                           // Load the image into the page.
                           igPage = ImGearFileFormats.LoadPage(stream, 0);
                       }
                       catch (ImGearException ex)
                       {
                           Debug.WriteLine(ex.Message);
                       }
                   }
                   if (null != igPage && null != igPage.DIB &&
                   !igPage.DIB.IsEmpty())
                   {
                       // Create a new page display.
                       imGearPageDisplay = new ImGearPageDisplay(igPage);
                       // Associate the page display with the page view.
                       imGearPageView1.Display = imGearPageDisplay;
                       // Cause the page view to repaint.
                       imGearPageView1.Invalidate();
                   }
               }
    VB.NET
    Copy Code
           Dim openFileDialog1 As New OpenFileDialog
           openFileDialog1.Filter = "All files (*.*)|*.*"
           openFileDialog1.FilterIndex = 1
           openFileDialog1.RestoreDirectory = True
           If openFileDialog1.ShowDialog = DialogResult.OK Then
               Using stream As New FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read)
                   Try
                       ' Load the image into the page.
                       igPage = ImGearFileFormats.LoadPage(stream, 0)
                   Catch ex As ImGearException
                       Debug.WriteLine(ex.Message)
                   End Try
                   If Not igPage Is Nothing AndAlso Not igPage.DIB Is Nothing Then
                       ' Create a new page display.
                       igPageDisplay = New ImGearPageDisplay(igPage)
                       ' Associate the page display with the page view.
                       ImGearPageView1.Display = igPageDisplay
                       ' Cause the page view to repaint.
                       ImGearPageView1.Invalidate()
                   End If
               End Using
           End If
  8. Execute the Zoom In operation as follows:
    C#
    Copy Code
               if (null != imGearPageDisplay && !imGearPageDisplay.Page.DIB.IsEmpty())
               {
                   // Get the current zoom info.
                   ImGearZoomInfo igZoomInfo = imGearPageDisplay.GetZoomInfo(imGearPageView1);
    
                   // Increase the zoom.
                   igZoomInfo.Horizontal.Value = igZoomInfo.Horizontal.Value * 1.25;
                   igZoomInfo.Vertical.Value = igZoomInfo.Vertical.Value * 1.25;
                   igZoomInfo.Horizontal.Fixed = true;
                   igZoomInfo.Vertical.Fixed = true;
    
                   // Set the new zoom values and repaint the view.
                   imGearPageDisplay.UpdateZoomFrom(igZoomInfo);
                   imGearPageView1.Invalidate();
               }
    VB.NET
    Copy Code
           If Not igPageDisplay Is Nothing AndAlso Not igPageDisplay.Page.DIB.IsEmpty() Then
               ' Get the current zoom info.
               Dim igZoomInfo As ImGearZoomInfo
               igZoomInfo = igPageDisplay.GetZoomInfo(ImGearPageView1)
               ' Increase the zoom.
               igZoomInfo.Horizontal.Value = igZoomInfo.Horizontal.Value * 1.25
               igZoomInfo.Vertical.Value = igZoomInfo.Vertical.Value * 1.25
               igZoomInfo.Horizontal.Fixed = True
               igZoomInfo.Vertical.Fixed = True
               ' Set the new zoom values and repaint the view.
               igPageDisplay.UpdateZoomFrom(igZoomInfo)
               ImGearPageView1.Invalidate()
           End If

    Similarly, we can perform Zoom Out by just modifying the multiplier of the Horizontal and Vertical values as (1/1.25). See Zoom Out Click Event Handler.

  9. Finally, rotate the page displayed with:
    C#
    Copy Code
               if (null != imGearPageDisplay && null != igPage && !imGearPageDisplay.Page.DIB.IsEmpty())
               {
                   try
                   {
                       // Rotate the given page 90 degrees using ImGearRotationValues.VALUE_90.
                       // ImGearRotationValues.VALUE_90 is the constant value for indicating the amount of the rotation.
                       // It may be changed for other values, e.g. ImGearRotationValues.VALUE_180, ImGearRotationValues.VALUE_270.
                       ImGearProcessing.Rotate(igPage, ImGearRotationValues.VALUE_90);
                       imGearPageView1.Invalidate();
                   }
                   catch (ImGearException ex)
                   {
                       Debug.WriteLine(ex.Message);
                   }
               }
    VB.NET
    Copy Code
           If Not igPage Is Nothing Then
               Try
                   ' Rotate the given page 90 degrees using ImGearRotationValues.VALUE_90.
                   ' ImGearRotationValues.VALUE_90 is the constant value for indicating the amount of the rotation.
                   ' It may be changed for other values, e.g. ImGearRotationValues.VALUE_180, ImGearRotationValues.VALUE_270.
                   ImGearProcessing.Rotate(igPage, ImGearRotationValues.VALUE_90)
               Catch ex As Exception
                   Console.WriteLine(ex.Message)
               End Try
               ImGearPageView1.Invalidate()
           End If

    Notice that for performing 180 and 270 degree rotations, we simply change the second parameter of the Rotate method to ImGearRotationValues.VALUE_180 and ImGearRotationValues.VALUE_270, respectively.