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.
The following tutorial refers specifically to 64-bit installations; for 32-bit installations:
|
Using the desired version of Visual Studio (2010 or later):
Your output target directory should be set to $YOURLOCALPROJ\bin\x64\Debug\ |
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 |
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 |
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 |
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.
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.