LEADTOOLS Windows Forms (Leadtools.WinForms assembly)
LEAD Technologies, Inc

GetTransformWithDpi Method

Example 





Return the transform of the viewer taking into account the DPI of the image.
Syntax
public Matrix GetTransformWithDpi()
'Declaration
 
Public Function GetTransformWithDpi() As Matrix
'Usage
 
Dim instance As RasterImageViewer
Dim value As Matrix
 
value = instance.GetTransformWithDpi()
public Matrix GetTransformWithDpi()
 function Leadtools.WinForms.RasterImageViewer.GetTransformWithDpi()
public:
Matrix^ GetTransformWithDpi(); 

Return Value

A System.Drawing.Drawing2D.Matrix object containing the values of the transform of the viewer taking into account the DPI of the image
Remarks

Use this instead of the Transform property when the value of the UseDpi property is set to true.

If the value of the UseDpi property is false, then the return value of this method is an exact copy of the Transform matrix.

Note, since this method returns a copy of the matrix used inside this RasterImageViewer object, you need to call Matrix.Dispose on the return object.

Example
 
Friend Class MyForm4 : Inherits Form
      Private _viewer As RasterImageViewer
      ' Are we currently rubber banding
      Private isRubberBanding As Boolean
      ' The current rubberband rectangle
      Private rubberBandingRectangle As Rectangle
      ' Flag to indicates if the rubber band rectangle is drawn
      Private isRubberBandingRectangleDrawn As Boolean
      ' Did we clicp the cursor?
      Private isCursorClipped As Boolean
      Private saveClipRectangle As Rectangle

      Public Sub New(ByVal title As String)
         Text = title
         Size = New Size(750, 450)

         ' Create the raster viewer
         _viewer = New RasterImageViewer()
         _viewer.DoubleBuffer = True
         _viewer.Dock = DockStyle.Fill

         ' Load an image into the viewer
         Dim codecs As RasterCodecs = New RasterCodecs()
         _viewer.Image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp"))
         codecs.Dispose()

         Controls.Add(_viewer)
         _viewer.BringToFront()
         StartRubberBanding()
      End Sub

      ' Call this to start rubber banding on the viewer
      Private Sub StartRubberBanding()
         ' Subclass to the event we need
         AddHandler _viewer.MouseDown, AddressOf _viewer_MouseDown
         AddHandler _viewer.MouseMove, AddressOf _viewer_MouseMove
         AddHandler _viewer.MouseUp, AddressOf _viewer_MouseUp
         AddHandler _viewer.LostFocus, AddressOf _viewer_LostFocus
      End Sub

      ' Call this to stop rubber banding on the viewer
      Private Sub StopRubberBanding()
         ' Un-subclass to the events
         RemoveHandler _viewer.MouseDown, AddressOf _viewer_MouseDown
         RemoveHandler _viewer.MouseMove, AddressOf _viewer_MouseMove
         RemoveHandler _viewer.MouseUp, AddressOf _viewer_MouseUp
         RemoveHandler _viewer.LostFocus, AddressOf _viewer_LostFocus

         isRubberBanding = False
         rubberBandingRectangle = Rectangle.Empty
         isRubberBandingRectangleDrawn = False
         isCursorClipped = False
         saveClipRectangle = Rectangle.Empty
      End Sub

      ' Begins the rubber banding operation
      Private Sub BeginRubberBanding(ByVal x As Integer, ByVal y As Integer)
         rubberBandingRectangle = Rectangle.FromLTRB(x, y, x, y)

         isRubberBanding = True
         _viewer.Capture = True

         ' Clip the cursor
         ClipCursor(True)

         DrawRubberBandRectangle()
      End Sub

      ' Ends the rubber banding
      Private Sub EndRubberBanding()
         _viewer.Capture = False
         isRubberBanding = False

         If isRubberBandingRectangleDrawn Then
            DrawRubberBandRectangle()
         End If

         If isCursorClipped Then
            ClipCursor(False)
         End If
      End Sub

      Private Sub ClipCursor(ByVal clip As Boolean)
         If clip Then
            Dim rect As Rectangle = Rectangle.Intersect(FixRectangle(_viewer.PhysicalViewRectangle), _viewer.ClientRectangle)
            rect = _viewer.RectangleToScreen(rect)
            Dim parent As Control = _viewer.Parent
            Do While Not parent Is Nothing
               rect = Rectangle.Intersect(rect, _viewer.Parent.RectangleToScreen(_viewer.Parent.ClientRectangle))
               If TypeOf parent Is Form Then
                  Dim form As Form = TryCast(parent, Form)
                  If form.IsMdiChild Then
                     If Not form.Owner Is Nothing Then
                        rect = Rectangle.Intersect(rect, form.Owner.RectangleToScreen(form.Owner.ClientRectangle))
                     ElseIf Not form.Parent Is Nothing Then
                        rect = Rectangle.Intersect(rect, form.Parent.RectangleToScreen(form.Parent.ClientRectangle))
                     End If
                  End If
               End If

               parent = parent.Parent
            Loop

            rect.Height += 1
            rect.Width += 1

            saveClipRectangle = Cursor.Clip
            Cursor.Clip = rect
            isCursorClipped = True
         Else
            Cursor.Clip = saveClipRectangle
            isCursorClipped = False
            saveClipRectangle = Rectangle.Empty
         End If
      End Sub

      Private Shared Function FixRectangle(ByVal rect As Rectangle) As Rectangle
         If rect.Left > rect.Right Then
            rect = Rectangle.FromLTRB(rect.Right, rect.Top, rect.Left, rect.Bottom)
         End If
         If rect.Top > rect.Bottom Then
            rect = Rectangle.FromLTRB(rect.Left, rect.Bottom, rect.Right, rect.Top)
         End If

         Return rect
      End Function

      ' Draws the rubberband rectangle on the viewer
      Private Sub DrawRubberBandRectangle()
         Dim rect As Rectangle = FixRectangle(rubberBandingRectangle)
         rect.Width += 1
         rect.Height += 1
         rect = _viewer.RectangleToScreen(rect)
         ControlPaint.DrawReversibleFrame(rect, Color.Transparent, FrameStyle.Thick)

         isRubberBandingRectangleDrawn = Not isRubberBandingRectangleDrawn
      End Sub

      Private Sub _viewer_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
         _viewer.Focus()

         ' Cancel rubber banding if it is on
         If isRubberBanding Then
            EndRubberBanding()
         Else
            If _viewer.IsImageAvailable AndAlso e.Button = MouseButtons.Left Then
               ' See if we click is on the image
               Dim rect As Rectangle = _viewer.PhysicalViewRectangle
               If rect.Contains(e.X, e.Y) Then
                  ' Start the rubber banding
                  BeginRubberBanding(e.X, e.Y)
               End If
            End If
         End If
      End Sub

      Private Sub _viewer_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
         If isRubberBanding Then
            DrawRubberBandRectangle()

            rubberBandingRectangle = Rectangle.FromLTRB(rubberBandingRectangle.Left, rubberBandingRectangle.Top, e.X, e.Y)

            DrawRubberBandRectangle()
         End If
      End Sub

      Private Sub _viewer_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
         If isRubberBanding Then
            ' Save the rubberband rectangle
            Dim rect As Rectangle = rubberBandingRectangle

            EndRubberBanding()

            ' First, convert the rectangle to image coordinates

            rect = FixRectangle(rect)

            ' Must be at least 1 pixel in size
            If rect.Width > 1 AndAlso rect.Height > 1 Then
               ' Get the transform matrix
               ' This works even if the UseDpi property of the viewer is set to true
               Using transform As Matrix = _viewer.GetTransformWithDpi()
                  Dim t As Transformer = New Transformer(transform)
                  rect = Rectangle.Round(t.RectangleToLogical(rect))
                  Dim lrect As LeadRect = New LeadRect(rect.Left, rect.Top, rect.Width, rect.Height)

                  ' Add this rectangle as a region
                  ' Note: no conversion or xform is needed
                  _viewer.Image.AddRectangleToRegion(Nothing, lrect, RasterRegionCombineMode.Set)
               End Using
            End If
         End If
      End Sub

      Private Sub _viewer_LostFocus(ByVal sender As Object, ByVal e As EventArgs)
         If isRubberBanding Then
            EndRubberBanding()
         End If
      End Sub
   End Class

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
class MyForm4 : Form
   {
      private RasterImageViewer _viewer;
      // Are we currently rubber banding
      private bool isRubberBanding;
      // The current rubberband rectangle
      private Rectangle rubberBandingRectangle;
      // Flag to indicates if the rubber band rectangle is drawn
      private bool isRubberBandingRectangleDrawn;
      // Did we clicp the cursor?
      private bool isCursorClipped;
      private Rectangle saveClipRectangle;

      public MyForm4(string title)
      {
         Text = title;
         Size = new Size(750, 450);

         // Create the raster viewer
         _viewer = new RasterImageViewer();
         _viewer.DoubleBuffer = true;
         _viewer.Dock = DockStyle.Fill;

         // Load an image into the viewer
         RasterCodecs codecs = new RasterCodecs();
         _viewer.Image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp"));
         codecs.Dispose();

         Controls.Add(_viewer);
         _viewer.BringToFront();
         StartRubberBanding();
      }

      // Call this to start rubber banding on the viewer
      private void StartRubberBanding()
      {
         // Subclass to the event we need
         _viewer.MouseDown += new MouseEventHandler(_viewer_MouseDown);
         _viewer.MouseMove += new MouseEventHandler(_viewer_MouseMove);
         _viewer.MouseUp += new MouseEventHandler(_viewer_MouseUp);
         _viewer.LostFocus += new EventHandler(_viewer_LostFocus);
      }

      // Call this to stop rubber banding on the viewer
      private void StopRubberBanding()
      {
         // Un-subclass to the events
         _viewer.MouseDown -= new MouseEventHandler(_viewer_MouseDown);
         _viewer.MouseMove -= new MouseEventHandler(_viewer_MouseMove);
         _viewer.MouseUp -= new MouseEventHandler(_viewer_MouseUp);
         _viewer.LostFocus -= new EventHandler(_viewer_LostFocus);

         isRubberBanding = false;
         rubberBandingRectangle = Rectangle.Empty;
         isRubberBandingRectangleDrawn = false;
         isCursorClipped = false;
         saveClipRectangle = Rectangle.Empty;
      }

      // Begins the rubber banding operation
      private void BeginRubberBanding(int x, int y)
      {
         rubberBandingRectangle = Rectangle.FromLTRB(x, y, x, y);

         isRubberBanding = true;
         _viewer.Capture = true;

         // Clip the cursor
         ClipCursor(true);

         DrawRubberBandRectangle();
      }

      // Ends the rubber banding
      private void EndRubberBanding()
      {
         _viewer.Capture = false;
         isRubberBanding = false;

         if (isRubberBandingRectangleDrawn)
            DrawRubberBandRectangle();

         if (isCursorClipped)
            ClipCursor(false);
      }

      private void ClipCursor(bool clip)
      {
         if (clip)
         {
            Rectangle rect = Rectangle.Intersect(FixRectangle(_viewer.PhysicalViewRectangle), _viewer.ClientRectangle);
            rect = _viewer.RectangleToScreen(rect);
            Control parent = _viewer.Parent;
            while (parent != null)
            {
               rect = Rectangle.Intersect(rect, _viewer.Parent.RectangleToScreen(_viewer.Parent.ClientRectangle));
               if (parent is Form)
               {
                  Form form = parent as Form;
                  if (form.IsMdiChild)
                  {
                     if (form.Owner != null)
                        rect = Rectangle.Intersect(rect, form.Owner.RectangleToScreen(form.Owner.ClientRectangle));
                     else if (form.Parent != null)
                        rect = Rectangle.Intersect(rect, form.Parent.RectangleToScreen(form.Parent.ClientRectangle));
                  }
               }

               parent = parent.Parent;
            }

            rect.Height += 1;
            rect.Width += 1;

            saveClipRectangle = Cursor.Clip;
            Cursor.Clip = rect;
            isCursorClipped = true;
         }
         else
         {
            Cursor.Clip = saveClipRectangle;
            isCursorClipped = false;
            saveClipRectangle = Rectangle.Empty;
         }
      }

      private static Rectangle FixRectangle(Rectangle rect)
      {
         if (rect.Left > rect.Right)
            rect = Rectangle.FromLTRB(rect.Right, rect.Top, rect.Left, rect.Bottom);
         if (rect.Top > rect.Bottom)
            rect = Rectangle.FromLTRB(rect.Left, rect.Bottom, rect.Right, rect.Top);

         return rect;
      }

      // Draws the rubberband rectangle on the viewer
      private void DrawRubberBandRectangle()
      {
         Rectangle rect = FixRectangle(rubberBandingRectangle);
         rect.Width++;
         rect.Height++;
         rect = _viewer.RectangleToScreen(rect);
         ControlPaint.DrawReversibleFrame(rect, Color.Transparent, FrameStyle.Thick);

         isRubberBandingRectangleDrawn = !isRubberBandingRectangleDrawn;
      }

      private void _viewer_MouseDown(object sender, MouseEventArgs e)
      {
         _viewer.Focus();

         // Cancel rubber banding if it is on
         if (isRubberBanding)
            EndRubberBanding();
         else
         {
            if (_viewer.IsImageAvailable && e.Button == MouseButtons.Left)
            {
               // See if we click is on the image
               Rectangle rect = _viewer.PhysicalViewRectangle;
               if (rect.Contains(e.X, e.Y))
               {
                  // Start the rubber banding
                  BeginRubberBanding(e.X, e.Y);
               }
            }
         }
      }

      private void _viewer_MouseMove(object sender, MouseEventArgs e)
      {
         if (isRubberBanding)
         {
            DrawRubberBandRectangle();

            rubberBandingRectangle = Rectangle.FromLTRB(
               rubberBandingRectangle.Left,
               rubberBandingRectangle.Top,
               e.X,
               e.Y);

            DrawRubberBandRectangle();
         }
      }

      private void _viewer_MouseUp(object sender, MouseEventArgs e)
      {
         if (isRubberBanding)
         {
            // Save the rubberband rectangle
            Rectangle rect = rubberBandingRectangle;

            EndRubberBanding();

            // First, convert the rectangle to image coordinates

            rect = FixRectangle(rect);

            // Must be at least 1 pixel in size
            if (rect.Width > 1 && rect.Height > 1)
            {
               // Get the transform matrix
               // This works even if the UseDpi property of the viewer is set to true
               using (Matrix transform = _viewer.GetTransformWithDpi())
               {
                  Transformer t = new Transformer(transform);
                  rect = Rectangle.Round(t.RectangleToLogical(rect));
                  LeadRect lrect = new LeadRect(rect.Left, rect.Top, rect.Width, rect.Height);

                  lrect = _viewer.Image.RectangleToImage(RasterViewPerspective.TopLeft, lrect);

                  // Add this rectangle as a region
                  // Note: no conversion or xform is needed
                  _viewer.Image.AddRectangleToRegion(null, lrect, RasterRegionCombineMode.Set);
               }
            }
         }
      }

      private void _viewer_LostFocus(object sender, EventArgs e)
      {
         if (isRubberBanding)
         {
            EndRubberBanding();
         }
      }
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

RasterImageViewer Class
RasterImageViewer Members

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.