- First, add necessary using statements. Open the code for the form by right-clicking on the form and clicking View Code. At the top of the code, add the following statements:
C# Copy Code using System.IO; using System.Diagnostics; using ImageGear; using ImageGear.Core; using ImageGear.Windows.Forms; using ImageGear.Display; using ImageGear.Processing; using ImageGear.Formats;
- Next add the following fields to Form1:
C# Copy Code // holds the image data private ImGearPage imGearPage = null; // controls how the page is displayed private ImGearPageDisplay imGearPageDisplay = null;
- If you are using a Deployment (Runtime) licensing, add the license initialization code to the Form1 constructor(Form1()) just before the call to InitializeComponent. This is not necessary if you are using an Evaluation or Development (Toolkit) license.
C# Copy Code //***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...");
- Below the code from the prior step, add the following calls to initialize the referenced formats:
C# Copy Code // Support for common formats: ImGearCommonFormats.Initialize();
- Now, add the following code to the Load Page menu handler:
C# Copy Code 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 imGearPage = ImGearFileFormats.LoadPage(stream, 0); } catch (ImGearException ex) { Debug.WriteLine(ex.Message); } } if (null != imGearPage && null != imGearPage.DIB && !imGearPage.DIB.IsEmpty()) { // create a new page display imGearPageDisplay = new ImGearPageDisplay(imGearPage); // associate the page display with the page view imGearPageView1.Display = imGearPageDisplay; // cause the page view to repaint imGearPageView1.Invalidate(); } } }
ImGearPageDisplay Class Constructs a new PageDisplay object for the given page. ImGearPageView Class.Invalidate Invalidates the control thus causing it to repaint. ImGearFileFormats Class.LoadPage Method Loads a file from a given stream and returns a new ImGearPage Class. ImGearFileFormats Class.GetSavingFilter Method Gets a filter string which can be passed to a windows common dialog filter member. - Now go to the handler created for the Zoom In menu item. Add the following code to the routine:
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(); }
ImGearPageDisplay Class.GetZoomInfo Method Retrieves the current zoom information from the display and page view. ImGearPageDisplay Class.UpdateZoomFrom Method Sets new zoom values from the given ImGearZoomInfo Structure object. ImGearZoomInfo Structure Holds horizontal and vertical zoom values and settings. - Add the following code to the Zoom Out click handler:
C# Copy Code 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(); }
- Add the code below to the Rotate 90 click handler:
C# Copy Code if (null != imGearPageDisplay && null != imGearPage && !imGearPageDisplay.Page.DIB.IsEmpty()) try { ImGearProcessing.Rotate(imGearPage, ImGearRotationValues.VALUE_90); imGearPageView1.Invalidate(); } catch (ImGearException ex) { Debug.WriteLine(ex.Message); }
ImGearProcessing Class.Rotate Method Rotates the given page by the given amount. - The code for the click handlers for the Rotate 180 and Rotate 270 menu items are the same as above except that the amount to rotate by is ImGearRotationValues.VALUE_180 and ImGearRotationValues.VALUE_270 respectively.
- Lastly, we can add the click code for the Exit menu item as shown below:
C# Copy Code this.Close();
- And now you are ready to compile and run the finished application.