Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.21
Advanced Magnifying Glass
Take the following steps to create a project that uses advanced features of the Magnifying Glass:
  1. Start Visual Studio .NET.
  2. Open the project that you created in the tutorial, Using the Magnifying Glass.
  3. Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file (after the existing Imports / using statements):

    [Visual Basic]

     
    Imports System.Drawing.Drawing2D 
    
    [C#]
    
    using System.Drawing.Drawing2D; 
    
  4. Add the following code to derive your own class from RasterMagnifyGlass, and override OnPaintBorder, OnPaintCrosshair and OnPaintImage. For Visual Basic, put this code at the end of the file. For #, put this code just inside the namespace.

    [Visual Basic]

    
    ' My own class, derived from Leadtools.WinForms.RasterMagnifyGlass
    Public Class MyMagGlass
       Inherits RasterMagnifyGlass
    
       Protected Overrides Sub OnPaintBorder(ByVal g As Graphics, ByVal centerPoint As Point)
          g.DrawEllipse(Pens.Blue, centerPoint.X - CInt(Me.Size.Width / 2), centerPoint.Y - CInt(Size.Height / 2), Size.Width - 1, Size.Height - 1)
       End Sub
    
       Protected Overrides Sub OnPaintCrosshair(ByVal g As Graphics, ByVal centerPoint As Point)
          g.DrawRectangle(Pens.Red, centerPoint.X - 10, centerPoint.Y - 10, 20, 20)
       End Sub
    
       Protected Overrides Sub OnPaintImage(ByVal g As Graphics, ByVal centerPoint As Point)
          Dim t As New Transformer(Viewer.Transform)
          Dim rcDst As New Rectangle(centerPoint.X - (Size.Width / 2), centerPoint.Y - (Size.Height / 2), Size.Width - 1, Size.Height - 1)
          Dim rcSrc As Rectangle = Rectangle.Round(t.RectangleToLogical(New RectangleF(rcDst.Left, rcDst.Top, rcDst.Width, rcDst.Height)))
    
          Dim gstate As GraphicsState = g.Save()
    
          Dim path As New GraphicsPath
          path.AddEllipse(rcDst)
          Dim rgn = New Region(path)
          path.Dispose()
    
          Viewer.Image.DisableEvents()
          Dim contrast As Integer = Viewer.Image.PaintContrast
          Viewer.Image.PaintContrast = 1000
    
          g.Clip = rgn
          Viewer.Image.Paint(g, rcSrc, rcSrc, rcDst, rcDst, Viewer.PaintProperties)
          Viewer.Image.PaintContrast = contrast
          Viewer.Image.EnableEvents()
    
          g.Restore(gstate)
          rgn.Dispose()
       End Sub
    
    [C#]
    
                
    // My own class, derived from Leadtools.WinForms.RasterMagnifyGlass
    public class MyMagGlass : RasterMagnifyGlass
    {
       protected override void OnPaintBorder(Graphics g, Point centerPoint)
       {
          g.DrawEllipse(Pens.Blue, centerPoint.X-(Size.Width/2), centerPoint.Y-(Size.Height/2), Size.Width-1, Size.Height-1);
       }
    
       protected override void OnPaintCrosshair(Graphics g, Point centerPoint)
       {
          g.DrawRectangle(Pens.Red, centerPoint.X-10, centerPoint.Y-10, 20, 20);
       }
    
       protected override void OnPaintImage(Graphics g, Point centerPoint)
       {
          Transformer t = new Transformer(Viewer.Transform);
          Rectangle rcDst = new Rectangle(centerPoint.X-(Size.Width/2), centerPoint.Y-(Size.Height/2), Size.Width-1, Size.Height-1);
          Rectangle rcSrc = Rectangle.Round(t.RectangleToLogical(rcDst));
    
          GraphicsState gstate = g.Save();
    
          GraphicsPath path = new GraphicsPath();
          path.AddEllipse(rcDst);
          Region rgn = new Region(path);
          path.Dispose();
    
          Viewer.Image.DisableEvents();
          int contrast = Viewer.Image.PaintContrast;
          Viewer.Image.PaintContrast = 1000;
    
          g.Clip = rgn;
          Viewer.Image.Paint(g, rcSrc, rcSrc, rcDst, rcDst, Viewer.PaintProperties);  
          Viewer.Image.PaintContrast = contrast;
          Viewer.Image.EnableEvents();
    
          g.Restore(gstate);
          rgn.Dispose();
       }
    }
    
  5. In the code for the Load event for Form1, add the following line, just before the line that sets the InteractiveMode property:

    [Visual Basic]

     
    RasterImageViewer1.MagnifyGlass = New MyMagGlass 'set the MagGlass
    
    [C#]
     
    rasterImageViewer1.MagnifyGlass = new MyMagGlass(); //set the MagGlass to my derived class
    
  6. Build, and Run the program to test it.