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:
- Your project should already be set to compile to target Debug and x86, and you should have the directory: $YOURLOCALPROJ\bin\x86\Debug\.
- Throughout these instructions, replace x64 with x86.
- The 32-bit ImageGear binaries are found in $INSTALLDIR\ImageGear .NET v23\Bin\
Using the desired version of Visual Studio (2010 or later):
- Create a new "WPF Application" project, using C# or VB.NET and name the project: IG_Tutorial_WPF.
- If you installed ImageGear .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.
- 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.WPF.nupkg (https://www.nuget.org/packages/Accusoft.ImageGear.WPF/)
Accusoft.ImageGear.ART.WPF.nupkg (https://www.nuget.org/packages/Accusoft.ImageGear.ART.WPF/)
- Manually:
- Copy all files (and folders) inside $INSTALLDIR\ImageGear .NET v23 64-bit\Bin\ to your local output bin directory in your project (i.e., $YOURLOCALPROJ\bin\x64\Debug\ ).
- Manually add the reference System.Windows.Forms to your project from the .NET Assemblies tab.
- Add the following references to your project from $YOURLOCALPROJ\bin\x64\Debug\:
- ImageGear23.Core.dll
- ImageGear23.Art.dll
- ImageGear23.Evaluation.dll
- ImageGear23.Formats.Common.dll
- ImageGear23.Presentation.dll
- ImageGear23.Windows.Controls.dll
- ImageGear23.Wpf.dll
Your output target directory should be set to $YOURLOCALPROJ\bin\x64\Debug\
- Open the MainWindow.xaml screen with both the Design and XAML panels for the window visible. You will use the XAML panel to create the graphical user interface. First, in the XAML panel, create a custom alias namespace. So, add some code to the header so it looks like this:
XAML |
Copy Code |
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:custom="clr-namespace:IG_Tutorial_WPF" xmlns:IGWC="clr-namespace:ImageGear.Windows.Controls;assembly=ImageGear23.Windows.Controls" Title="MainWindow" Height="300" Width="300"> </Window> |
- For organizational purposes, we will use a Grid. Inside of that Grid we will have a Menu. That Menu will have a File > Open item that will be responsible for opening a file, View > Zoom In | Zoom Out for zoom in/out on the file loaded and Processing > Rotate 90 for rotating the file loaded. To finish the GUI, we also need a PageView control, which will be used to display the file. After adding these controls, the XAML code should result in the following:
XAML |
Copy Code |
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:custom="clr-namespace:IG_Tutorial_WPF" xmlns:IGWC="clr-namespace:ImageGear.Windows.Controls;assembly=ImageGear23.Windows.Controls" Title="MainWindow" Height="300" Width="300"> <Window.CommandBindings> <CommandBinding Command="Open" Executed="OpenExecuted"/> <CommandBinding Command="{x:Static custom:MainWindow.ZoomInCmd}" CanExecute="CanExectutePageAvailable" Executed="ZoomInExecuted"/> <CommandBinding Command="{x:Static custom:MainWindow.ZoomOutCmd}" CanExecute="CanExectutePageAvailable" Executed="ZoomOutExecuted"/> <CommandBinding Command="{x:Static custom:MainWindow.Rotate90Cmd}" CanExecute="CanExectutePageAvailable" Executed="ExectutedRotate90"/> </Window.CommandBindings> <Grid> <DockPanel Name="DockPanel1"> <Menu DockPanel.Dock="Top" Height="22" Name="menu1"> <MenuItem Name="mnuFile" Header="_File"> <MenuItem Name="mnuFileOpen" Header="_Open" Command="Open"/> </MenuItem> <MenuItem Name="mnuView" Header="_View"> <MenuItem Name="mnuViewZoomIn" Header="Zoom _In" Command="{x:Static custom:MainWindow.ZoomInCmd}"/> <MenuItem Name="mnuViewZoomOut" Header="Zoom _Out" Command="{x:Static custom:MainWindow.ZoomOutCmd}"/> </MenuItem> <MenuItem Name="mnuProcessing" Header="_Processing"> <MenuItem Name="mnuProcessingRot90" Header="Rotate 90" Command="{x:Static custom:MainWindow.Rotate90Cmd}"/> </MenuItem> </Menu> <IGWC:PageView x:Name="imGearPageView1"/> </DockPanel> </Grid> </Window> |
- Each Command Binding entry will have its own handler for Executed, but all of the them will share a single handler for CanExecute. Right-click over each handler created and navigate to its definition in the .xaml.cs file. In the case of CanExecute, you can navigate to its definition through any of the controls.
- Next, add the functionality to the controls in the MainWindow.xaml.cs file:
- First, add some using statements and declare the some members required for this project.
- Second, declare the static RoutedCommands required for the controls created.
- Third, two member variables will need to be added to the class: ImGearPage to hold the image data and ImGearPageDisplay that will control the ways it is shown.
- Finally, add the code for the commands that will be executed by each control.
The body of the MainWindow class should look like this:
In what follows, we will go over some areas of this sample code in more detail.
- We declare the route commands required for this project in the .xaml.cs file as:
- Then, the Command Bindings in the .xaml file for the RoutedCommands created above:
XAML |
Copy Code |
<Window.CommandBindings> <CommandBinding Command="Open" Executed="OpenExecuted"/> <CommandBinding Command="{x:Static custom:MainWindow.ZoomInCmd}" CanExecute="CanExectutePageAvailable" Executed="ZoomInExecuted"/> <CommandBinding Command="{x:Static custom:MainWindow.ZoomOutCmd}" CanExecute="CanExectutePageAvailable" Executed="ZoomOutExecuted"/> <CommandBinding Command="{x:Static custom:MainWindow.Rotate90Cmd}" CanExecute="CanExectutePageAvailable" Executed="ExectutedRotate90"/> </Window.CommandBindings> |
- If you are using a Deployment (Runtime) licensing, add the license initialization code to the Main Window 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.
- Load and display a document using:
- Execute the Zoom In operation as follows:
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.
- Finally, we can rotate the page displayed with:
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.