ImageToViewerRectangle Method


Converts a rectangle in image coordinates to viewer coordinates.


public Rectangle ImageToViewerRectangle( 
   Rectangle rect, 
   bool accountForViewPerspective 
Rectangle ImageToViewerRectangle(  
   Rectangle rect, 
   bool accountForViewPerspective 
   Rectangle^ ImageToViewerRectangle( 
      Rectangle^ rect, 
      bool accountForViewPerspective 


The rectangle in image coordinates.

true if  rect is already in the image view-perspective. false if  rect is in top-left coordinates.

Return Value

A System.Drawing.Rectangle object containing the rectangle coordinates.


This method converts a rectangle from image (logical) to viewer (physical) coordinates. The viewer coordinates is what you are currently seeing on the screen. It contains the current scale and scroll values (as well as any padding if set). For example, when you use the RasterViewerInteractiveMode.UserRectangle, the value obtained from the RasterViewerRectangleEventArgs.Rectangle parameter to the InteractiveUserRectangle event is in physical coordinates. If you need to convert this value to image coordinates (for example, to use it as a region or to find the exact pixel coordinate of this rectangle in the image) then you should use the ViewerToImageRectangle method.

When  accountForViewPerspective is set to true, it is assumed that  rect is already in the correct view perspective of the as the current image (RasterImage.ViewPerspective), therefore, no conversion to top-left (which what the physical coordinates are always in) is performed. If however this value is false, then this method will convert  rect to top-left coordinates before converting it to physical coordinates.

To convert a rectangle from viewer (physical) to image (logical) coordinates, use the ViewerToImageRectangle method.

To convert a point from image to viewer coordinates and back, use ImageToViewerPoint and ViewerToImagePoint.


using Leadtools.WinForms; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Drawing; 
public void InteractiveUserRectangleExample(RasterImageViewer viewer) 
   // Set the interactive mode of the viewer to user-defined rectangle 
   viewer.InteractiveMode = RasterViewerInteractiveMode.UserRectangle; 
   // Subscribe to the InteractiveUserRectangle event to know when the user has finished drawing a new rectangle 
   viewer.InteractiveUserRectangle += new EventHandler<RasterViewerRectangleEventArgs>(viewer_InteractiveUserRectangle); 
private void viewer_InteractiveUserRectangle(object sender, RasterViewerRectangleEventArgs e) 
   // Check the status of the event 
   if (e.Status == RasterViewerInteractiveStatus.End && !e.Cancel) 
      // The user has finished drawing a new rectangle (and the operation has not been canceled) 
      // Get the rectangle and convert it to image coordinates (with view-perspective) 
      RasterImageViewer viewer = sender as RasterImageViewer; 
      Rectangle rect = e.Rectangle; 
      // The user rectangle might have negative width or height (if the user starts the drawing from 
      // bottom-left corner for example), check for that 
      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); 
      // Account for the view perspective of the image 
      rect = viewer.ViewerToImageRectangle(rect, true); 
      // Set this as the region and invert the colors 
      RasterImage image = viewer.Image; 
      LeadRect ltRect = new LeadRect(rect.Left, rect.Top, rect.Width, rect.Height); 
      image.AddRectangleToRegion(null, ltRect, RasterRegionCombineMode.Set); 
      InvertCommand cmd = new InvertCommand(); 
      // Remove the region 


Target Platforms

See Also


RasterImageViewer Class

RasterImageViewer Members

Help Version 22.0.2023.2.9
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.WinForms Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.