User Guide > Getting Started > Tutorial: Create Your First Project > WPF Application |
In this tutorial, you will configure a C# or VB.NET project for a WPF application and use ImageGear .NET capabilities. You will also learn how to zoom in/out and rotate a file that is loaded.
The following tutorial refers specifically to 64-bit installations; for 32-bit installations:
|
Using the desired version of Visual Studio (2010 or later):
If you are using ImageGear .NET 64-bit, we suggest that you set your project to be compiled for Any CPU because there might be some conflicts within the design view when set to x64 due to the custom variables that are created for this sample. |
Your output target directory should be set to $YOURLOCALPROJ\bin\x64\Debug\ |
XAML |
Copy Code |
---|---|
<Window x:Class="IG_Tutorial_WPF.MainWindow" |
XAML |
Copy Code |
---|---|
<Window x:Class="IG_Tutorial_WPF.MainWindow" |
The body of the MainWindow class should look like this:
C# |
Copy Code |
---|---|
using System.Windows; using System.Windows.Input; using System.IO; using System.Windows.Forms; using System.Diagnostics; using ImageGear; using ImageGear.ART; using ImageGear.Core; using ImageGear.Display; using ImageGear.Windows.Forms; using ImageGear.Windows.Controls; using ImageGear.Processing; using ImageGear.Formats; using ImageGear.WPF; using ImageGear.WPF.HDP; using ImageGear.Evaluation; namespace IG_Tutorial_WPF { public partial class MainWindow : Window { public static RoutedCommand ZoomInCmd = new RoutedCommand(); public static RoutedCommand ZoomOutCmd = new RoutedCommand(); public static RoutedCommand Rotate90Cmd = new RoutedCommand(); // Hold the image data. ImGearPage igPage = null; // Control how the page is displayed. ImGearPresentationPageDisplay igPageDisplay = null; public MainWindow() { // 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(); // Insert the HD Photo format held by the IGWPF component. ImGearFileFormats.Filters.Insert(0, ImGearHDPhoto.CreateHDPhotoFormat()); // Initialize common formats. ImGearCommonFormats.Initialize(); InitializeComponent(); } private void OpenExecuted(object sender, ExecutedRoutedEventArgs e) { ImGearLoadingSelection selection = ImGearWinForms.SelectLocalFileToLoad(null, "Select File"); if (null != selection) { using (FileStream fileContent = ((ImGearLocalFile) selection.File).OpenToRead()) { try { igPage = ImGearFileFormats.LoadPage(fileContent, 0); } catch (ImGearException ex) { Debug.WriteLine(ex.Message); } } if (null != igPage && null != igPage.DIB && !igPage.DIB.IsEmpty()) { // Create a new page display. igPageDisplay = new ImGearPresentationPageDisplay(igPage); // Associate the page display with the page view. imGearPageView1.Display = igPageDisplay; // Cause the page view to update. imGearPageView1.Update(); } } } private void ZoomInExecuted(object sender, ExecutedRoutedEventArgs e) { // Get the current zoom info. 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. igPageDisplay.UpdateZoomFrom(igZoomInfo); // Trigger an update, letting it know only the layout has changed. imGearPageView1.Update(true); } private void ZoomOutExecuted(object sender, ExecutedRoutedEventArgs e) { // Get the current zoom info. ImGearZoomInfo igZoomInfo = igPageDisplay.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. igPageDisplay.UpdateZoomFrom(igZoomInfo); // Trigger an update, letting it know only the layout has changed. imGearPageView1.Update(true); } private void ExectutedRotate90(object sender, ExecutedRoutedEventArgs e) { 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.Update(); } catch (ImGearException ex) { Debug.WriteLine(ex.Message); } } private void CanExectutePageAvailable(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = (null != igPage && null != igPage.DIB && !igPage.DIB.IsEmpty() && null != igPageDisplay); } } } |
VB.NET |
Copy Code |
---|---|
Imports System.Windows Imports System.Windows.Input Imports System.IO Imports System.Windows.Forms Imports System.Diagnostics Imports ImageGear Imports ImageGear.ART Imports ImageGear.Core Imports ImageGear.Display Imports ImageGear.Windows.Forms Imports ImageGear.Windows.Controls Imports ImageGear.Processing Imports ImageGear.Formats Imports ImageGear.WPF Imports ImageGear.WPF.HDP Imports ImageGear.Evaluation Class MainWindow Public Shared ZoomInCmd As New RoutedCommand() Public Shared ZoomOutCmd As New RoutedCommand() Public Shared Rotate90Cmd As New RoutedCommand() ' Hold the image data. Private igPage As ImGearPage ' Control how the page is displayed. Private igPageDisplay As ImGearPresentationPageDisplay 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 OpenExecuted(sender As Object, e As ExecutedRoutedEventArgs) Dim selection As ImGearLoadingSelection = ImGearWinForms.SelectLocalFileToLoad(Nothing, "Select File") If Not selection Is Nothing Then Dim localFile As ImGearLocalFile = selection.File Using fileContent As FileStream = localFile.OpenToRead() Try ' Load the image into the page. igPage = ImGearFileFormats.LoadPage(fileContent, 0) Catch ex As ImGearException Debug.WriteLine(ex.Message) End Try If Not igPage Is Nothing AndAlso Not igPage.DIB Is Nothing AndAlso Not igPage.DIB.IsEmpty() Then ' Create a new page display. igPageDisplay = New ImGearPresentationPageDisplay(igPage) ' Associate the page display with the page view. imGearPageView1.Display = igPageDisplay ' Cause the page view to update. imGearPageView1.Update() End If End Using End If End Sub Private Sub ZoomInExecuted(sender As Object, e As ExecutedRoutedEventArgs) ' Get the current zoom info. Dim igZoomInfo As ImGearZoomInfo = 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. igPageDisplay.UpdateZoomFrom(igZoomInfo) ' Trigger an update, letting it know only the layout has changed. imGearPageView1.Update(True) End Sub Private Sub ZoomOutExecuted(sender As Object, e As ExecutedRoutedEventArgs) ' Get the current zoom info. Dim igZoomInfo As ImGearZoomInfo = igPageDisplay.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. igPageDisplay.UpdateZoomFrom(igZoomInfo) ' Trigger an update, letting it know only the layout has changed. imGearPageView1.Update(True) End Sub Private Sub ExectutedRotate90(sender As Object, e As ExecutedRoutedEventArgs) 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.Update() Catch ex As ImGearException Debug.WriteLine(ex.Message) End Try End Sub Private Sub CanExectutePageAvailable(sender As Object, e As CanExecuteRoutedEventArgs) e.CanExecute = (Not igPage Is Nothing AndAlso Not igPage.DIB Is Nothing AndAlso Not igPage.DIB.IsEmpty() AndAlso Not igPageDisplay Is Nothing) End Sub End Class |
In what follows, we will go over some areas of this sample code in more detail.
C# |
Copy Code |
---|---|
public static RoutedCommand ZoomInCmd = new RoutedCommand(); public static RoutedCommand ZoomOutCmd = new RoutedCommand(); public static RoutedCommand Rotate90Cmd = new RoutedCommand(); |
VB.NET |
Copy Code |
---|---|
Public Shared ZoomInCmd As New RoutedCommand() Public Shared ZoomOutCmd As New RoutedCommand() Public Shared Rotate90Cmd As New RoutedCommand() |
XAML |
Copy Code |
---|---|
<Window.CommandBindings> |
C# |
Copy Code |
---|---|
public MainWindow() { // 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(); // Insert the HD Photo format held by the IGWPF component. ImGearFileFormats.Filters.Insert(0, ImGearHDPhoto.CreateHDPhotoFormat()); // 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 |
---|---|
ImGearLoadingSelection selection = ImGearWinForms.SelectLocalFileToLoad(null, "Select File"); if (null != selection) { using (FileStream fileContent = ((ImGearLocalFile) selection.File).OpenToRead()) { try { igPage = ImGearFileFormats.LoadPage(fileContent, 0); } catch (ImGearException ex) { Debug.WriteLine(ex.Message); } } if (null != igPage && null != igPage.DIB && !igPage.DIB.IsEmpty()) { // Create a new page display. igPageDisplay = new ImGearPresentationPageDisplay(igPage); // Associate the page display with the page view. imGearPageView1.Display = igPageDisplay; // Cause the page view to update. imGearPageView1.Update(); } } |
VB.NET |
Copy Code |
---|---|
Dim selection As ImGearLoadingSelection = ImGearWinForms.SelectLocalFileToLoad(Nothing, "Select File") If Not selection Is Nothing Then Dim localFile As ImGearLocalFile = selection.File Using fileContent As FileStream = localFile.OpenToRead() Try ' Load the image into the page. igPage = ImGearFileFormats.LoadPage(fileContent, 0) Catch ex As ImGearException Debug.WriteLine(ex.Message) End Try If Not igPage Is Nothing AndAlso Not igPage.DIB Is Nothing AndAlso Not igPage.DIB.IsEmpty() Then ' Create a new page display. igPageDisplay = New ImGearPresentationPageDisplay(igPage) ' Associate the page display with the page view. imGearPageView1.Display = igPageDisplay ' Cause the page view to update. imGearPageView1.Update() End If End Using End If |
C# |
Copy Code |
---|---|
// Get the current zoom info. 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. igPageDisplay.UpdateZoomFrom(igZoomInfo); // Trigger an update, letting it know only the layout has changed. imGearPageView1.Update(true); |
VB.NET |
Copy Code |
---|---|
' Get the current zoom info. Dim igZoomInfo As ImGearZoomInfo = 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. igPageDisplay.UpdateZoomFrom(igZoomInfo) ' Trigger an update, letting it know only the layout has changed. imGearPageView1.Update(True) |
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 |
---|---|
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.Update(); } catch (ImGearException ex) { Debug.WriteLine(ex.Message); } |
VB.NET |
Copy Code |
---|---|
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.Update() Catch ex As ImGearException Debug.WriteLine(ex.Message) End Try |
Notice that for performing 180 and 270 rotations, we simply change the second parameter of the Rotate method to ImGearRotationValues.VALUE_180 and ImGearRotationValues.VALUE_270, respectively.