ImageGear .NET v24.14 - Updated September 15, 2020
WinForms Application
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 uses the ImageGear .NET GUI components. Make sure you've followed the steps to Install GUI Components.

For local development, ImageGear .NET must be installed from the product installer. This will ensure that licensing is set up properly for local development. Once ImageGear .NET is installed, projects can use NuGet for building and deployment.

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

Using Visual Studio 2015 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 it 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\Bin\x64 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 section, 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 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);
                   }
               }
           }
    
       }
    }

Now, we'll take a closer look at each section of the code:

  1. First, add the ImGearEvaluationManager.Initialize() call if you are evaluating the product. You also need to initialize common formats.
    C#
    Copy Code
           public Form1()
           {
      
               // Initialize evaluation license.
               ImGearEvaluationManager.Initialize();
    
               // Initialize common formats.
               ImGearCommonFormats.Initialize();
    
               InitializeComponent();
           }
  2. Next, load and display a document:
    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();
                   }
               }
  3. Execute the Zoom In operation:
    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();
               }

    Similarly, you can perform Zoom Out by modifying the multiplier of the Horizontal and Vertical values as (1/1.25). See the zoomOutToolStripMenuItem_Click function in the code example in step 5 (above).

  4. Finally, rotate the page displayed:
    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);
                   }
               }

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