AccuSoft Resource Center

ImageGear C# Tutorial

ImageGear is the industry standard for imaging application development. Building your application with our award-winning toolkits provides you with a powerful set of imaging libraries and image manipulation features and functions. In this tutorial, you will create a C# Windows Form application using the ImageGear ActiveX Custom Control. This tutorial uses Microsoft® Visual Studio® .NET, and assumes that you are using Microsoft Windows 2000 or XP. The application you create will load, display and zoom images. The instructions in this tutorial are specific to C# Windows Forms and ImageGear v14.

Preparing the Compiler

  1. Start Visual Studio .NET. The Visual Studio Development Environment appears.
     
  2. Create a new Visual C# Windows Application project (see below).



     
  3. Once the project is created you should add the ImageGear ActiveX controls to the Visual Studio Toolbar. Do this by selecting the Add/Remove Toolbox Items... option in the Tools menu.  In the Customize Toolbox dialog, click the COM Components tab and check off the following components:
  • IGCoreCtl Class
  • IGDisplayCtl Class
  • IGDlgsCtl Class
  • IGFormatsCtl Class
  • IGPageViewCtl Class
  • IGProcessingCtl Class

Note:  If you do not see a required control in the list, you may need to install the ImageGear ActiveX product.  If you selected to install this product on first use, simply run one of the ImageGear ActiveX samples.  To do this, click Start, Programs, AccuSoft, ImageGear v14, Version, ActiveX Samples (where Version is the edition of ImageGear [Professional, Enterprise or MD] being used) to locate the samples. Launching an ImageGear sample program will automatically cause the ActiveX product to be downloaded and installed on your machine.



Once you click OK, you will see the icon for the ImageGear control appear in the controls toolbar (below):

Setting up the Form

  1. Place a MainMenu control (found in the Windows Forms group of the toolbar) onto the form.  Add three menu items called mnuOpen, mnuZoomin, and mnuZoomout.  Set their captions to Open, Zoom In and Zoom Out respectively.


     
  2. In turn, add each ImageGear 14 control to the form by clicking the appropriate icon in the Dialog Editor section of the toolbar and drawing the control on the form.  Stretch the PageView Control (IGPageViewCtl) to fill most of the form (it should appear as a white rectangle).  The remaining controls (IGCoreCtl, IGDisplayCtl, IGFormatsCtl, IGProcessingCtl and IGDlgsCtl) should show up as small icons on the form.  For this tutorial we will use the default names (e.g. IGCoreCtl1) for each control.  Your form should now look something like this:

Add the Code

  1. Every C# Windows Application form gets the following set of using statements:

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;


    Add the following other using statements to Form1.cs:

    using GearDIALOGSLib;
    using GearCORELib;
    using GearFORMATSLib;
    using GearDISPLAYLib;
    using GearPROCESSINGLib;
  1. When you added the menu items and the ImageGear ActiveX controls to your form, Visual Studio automatically added private member variables for each of them to your Form1.cs file:

    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.MainMenu mainMenu1;
    private System.Windows.Forms.MenuItem mnuOpen;
    private System.Windows.Forms.MenuItem mnuZoomin;
    private System.Windows.Forms.MenuItem mnuZoomout;
    private AxGearCORELib.AxIGCoreCtl axIGCoreCtl1;
    private AxGearDISPLAYLib.AxIGDisplayCtl axIGDisplayCtl1;
    private AxGearFORMATSLib.AxIGFormatsCtl axIGFormatsCtl1;
    private AxGearDIALOGSLib.AxIGDlgsCtl axIGDlgsCtl1;
    private AxGearVIEWLib.AxIGPageViewCtl axIGPageViewCtl1;
    private AxGearPROCESSINGLib.AxIGProcessingCtl axIGProcessingCtl1;


    You’ll need to add the following additional private members to the Form1 class:

    private IGFileDlg m_dlgFile = null;
    private IGPage m_igPage = null;
    private IGPageDisplay m_igPageDisp = null;

     
  2. Next, use the Visual Studio forms designer to add Event handlers for the Load and Resize events for Form1:



     
  3. Add the following code to the Form1_Load() and Form1_Resize() Event Handler methods:

    private void Form1_Load(object sender, System.EventArgs e)
    {
    axIGCoreCtl1.License.SetSolutionName("AccuSoft 1-100-14");
    axIGCoreCtl1.Result.NotificationFlags =
    enumIGErrorHandlingFlags.IG_ERR_OLE_ERROR;

    try
    {
    axIGCoreCtl1.AssociateComponent(axIGFormatsCtl1.ComponentInterface);
    axIGCoreCtl1.AssociateComponent(axIGDisplayCtl1.ComponentInterface);
    axIGCoreCtl1.AssociateComponent(axIGProcessingCtl1.ComponentInterface);

    axIGDlgsCtl1.GearCore = axIGCoreCtl1.ComponentInterface;
    axIGDlgsCtl1.GearFormats = axIGFormatsCtl1.ComponentInterface;
    axIGDlgsCtl1.GearDisplay = axIGDisplayCtl1.ComponentInterface;

    m_dlgFile = axIGDlgsCtl1.CreateFileDlg();
    m_igPage = axIGCoreCtl1.CreatePage();
    m_igPageDisp = axIGDisplayCtl1.CreatePageDisplay( m_igPage );
    axIGPageViewCtl1.PageDisplay = m_igPageDisp;
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
    axIGPageViewCtl1.Left = 0;
    axIGPageViewCtl1.Top = 0;
    axIGPageViewCtl1.Width = this.ClientSize.Width;
    axIGPageViewCtl1.Height = this.ClientSize.Height;
    }

The code above includes the following ImageGear methods and properties:

  IGCoreCtl License.SetSolutionName This method of the IGLicense object sets the solution name for the ImageGear license.
  Result.NotificationFlag This method of the IGResult object tells ImageGear to report errors using the VB On Error mechanism.
  AssociateComponent This method associates ImageGear components with the Core control.
  CreatePage This method creates and returns a new IGPage object.
  Result.isOk This property of the IGResult object tells if there are any errors on the error stack.
IGDlgsCtl GearFormats This property sets the current Formats control used by the Dialog control.
  GearCore This property sets the current Core control used by the Dialog control.
IGDisplayCtl CreatePageDisplay This method creates a new IGPageDisplay object for the specified IGPage.
IGPageViewCtl PageDisplay This property sets the current Page Display object displayed by the Page View control.
  1. Next, create a click procedure for mnuOpen by double-clicking the menu on the form and then copying the following code into Form1.cs:

    private void mnuOpen_Click(object sender, System.EventArgs e)
    {
    try
    {
    IGFileDlgPageLoadOptions dlgLoadOptions = null;

    dlgLoadOptions =
    (IGFileDlgPageLoadOptions)
    axIGDlgsCtl1.CreateFileDlgOptions(
    enumIGFileDlgOptionsType.IG_FILEDLGOPTIONS_PAGELOADOBJ);

    if(m_dlgFile.Show((IIGFileDlgOptions)dlgLoadOptions))
    {
    axIGFormatsCtl1.LoadPageFromFile(m_igPage,
    dlgLoadOptions.FileName, dlgLoadOptions.PageNum);
    }
    axIGPageViewCtl1.UpdateView();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

The code above includes the following methods:

  IGDlgsCtl CreateFileDlgOptions This method creates a file dialog options object that will contain for example the filename and page number.
  IGFileDlg Show This method shows the file dialog and returns True if the user clicked OK.
  IGFormatsCtl LoadPageFromFile This method loads an image into the specified IGPage from the specified filename.
  1. Finally, add click procedures for the mnuZoomin and mnuZoomout commands by double clicking on each control respectively in the Dialog designer, and copying the following code into Form1.cs:

    private void mnuZoomin_Click(object sender, System.EventArgs e)
    {
    try
    {
    IGDisplayZoomInfo zoominfo = null;
    zoominfo = (IGDisplayZoomInfo)
    m_igPageDisp.GetZoomInfo( axIGPageViewCtl1.hWnd );
    zoominfo.HZoom = zoominfo.HZoom * 1.25;
    zoominfo.VZoom = zoominfo.VZoom * 1.25;
    zoominfo.Mode =
    GearDISPLAYLib.enumZoomModes.IG_DSPL_ZOOM_H_FIXED |
    GearDISPLAYLib.enumZoomModes.IG_DSPL_ZOOM_V_FIXED;
    m_igPageDisp.UpdateZoomFrom(zoominfo);

    axIGPageViewCtl1.UpdateView();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

The above code zooms the image to 125 percent, or 5/4ths, of its size (zoom in by 25 percent).

private void mnuZoomout_Click(object sender, System.EventArgs e)
{
try
{
IGDisplayZoomInfo zoominfo = null;
zoominfo = (IGDisplayZoomInfo)
m_igPageDisp.GetZoomInfo( axIGPageViewCtl1.hWnd );
zoominfo.HZoom = zoominfo.HZoom * 0.80;
zoominfo.VZoom = zoominfo.VZoom * 0.80;
zoominfo.Mode =
GearDISPLAYLib.enumZoomModes.IG_DSPL_ZOOM_H_FIXED |
GearDISPLAYLib.enumZoomModes.IG_DSPL_ZOOM_V_FIXED;
m_igPageDisp.UpdateZoomFrom(zoominfo);

axIGPageViewCtl1.UpdateView();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

The above code zooms out, decreasing the image to 4/5ths of its size, thereby canceling the effect of the zoom-in above.

The code above includes:

  IGPageDisplay GetZoomInfo This method creates and returns a new IGDisplayZoomInfo object that contains zoom mode and values.
    UpdateZoomFrom This method sets zoom mode and values from the specified IGDisplayZoomInfo object.

Running the Application

That's it! Simply compile and run the C# project and you will have a simple, yet functional image viewer. Add the plug-ins to enable support for over 100 image formats.