Image Viewer Interactive Modes

The ImageViewer class supports rich built-in and fully customizable and extensible user-interface interaction support for panning, zooming, the magnify glass, rubber-banding and many more operations. The class supports mouse, touch screen and/or multi-touch input.

To use an interactive mode, create an instance of one of the types derived from the ImageViewerInteractiveMode and add it to the ImageViewer.InteractiveModes collection. For example:

// Create an instance of Pan/Zoom interactive mode 
ImageViewerPanZoomInteractiveMode mode = new ImageViewerPanZoomInteractiveMode(); 
// Add it to the viewer 
imageViewer.InteractiveModes.Add(mode); 

When the viewer is set up like the example above, clicking and dragging the mouse over the viewer surface will pan the image. CTRL-clicking and dragging will zoom the image in and out. Similarly, on devices that support touch input, pressing down and dragging will pan the image while dragging with two fingers will zoom in and out.

The ImageViewer supports the following built-in and fully functional interactive modes:

Interactive Mode Description
ImageViewerActiveItemInteractiveMode

Changes the active item upon user mouse click or touch tap

ImageViewerAddRegionInteractiveMode

Adds a region to the image

ImageViewerAutoPanInteractiveMode

Automatically pans the image to a direction when the user is dragging near the edge of the viewer

ImageViewerCenterAtInteractiveMode

Centers (and optionally zooms) the image around the user's mouse click or touch tap

ImageViewerDragInteractiveMode

Allows items to be dragged within an ImageViewer or between ImageViewers

ImageViewerFloaterInteractiveMode

Support for dragging and resizing the floater of an image using mouse or touch input

ImageViewerMagnifyGlassInteractiveMode

Magnifies the portion of the image under the mouse or touch

ImageViewerNoneInteractiveMode

Empty interactive mode

ImageViewerPagerInteractiveMode

Flips the pages of a multipage image when the user drags across the viewer surface

ImageViewerPanZoomInteractiveMode

Provides an interactive pan and zoom user interface functionality to an ImageViewer

ImageViewerRubberBandInteractiveMode

Draws a temporary rectangle on top of the image using mouse or touch input. Can be used to perform any extra functionality such as drawing a region of interest for a user-defined operation

ImageViewerSelectItemsInteractiveMode

Provides single- and multi-selection support using mouse or touch input

ImageViewerSpyGlassInteractiveMode

Generic spy glass interactive mode

ImageViewerZoomAtInteractiveMode

Zooms the image around the user's mouse click or touch tap

ImageViewerZoomToInteractiveMode

Zooms to the image rectangle created by the user using mouse or touch input

Customizing Mode Behavior

These interactive modes have extra functionality to further customize the behavior as well as being fully extensible for further detailed operations.

The ImageViewer.InteractiveModes property is a collection. Hence, you can add as many modes to the viewer as desired. This allows you to:

The following example will be used by all the code snippets below to demonstrate how to use this collection.

// Create a new image viewer instance using the default constructor 
ImageViewer imageViewer = new ImageViewer(); 
// Set some properties 
imageViewer.Dock = DockStyle.Fill; 
// Add it to the form 
this.Controls.Add(imageViewer); 
// Load an image into it 
using (RasterCodecs codecs = new RasterCodecs()) 
   imageViewer.Image = codecs.Load(@"C:\LEADTOOLS23\Resources\Images\Ocr1.tif", 1); 

The behavior resulting from the input action depends on several settings, as follows:

Mode Order

The IsEnabled Value

The MouseButtons Value

The CanStartWork Value

The following sections describe each of these factors in more detail.

The Order of the Modes in the Collection

The mode at the top of the collection (at index 0) will have the first chance of handling the action. If the interactive mode determines it should handle the event, it will set InteractiveEventArgs.IsHandled to true to indicate this to any remaining modes. The next mode in the collection then views the event, and so on with every registered interactive mode. A mode can be implemented to disregard the value of InteractiveEventArgs.IsHandled or to not set the value to true when handling the event so that multiple actions can result from one event. See the documentation for each of the built-in interactive modes for more information on how that particular mode reads or sets this property.

Add the following code to the example above:

// Add Pan/Zoom mode first 
ImageViewerPanZoomInteractiveMode panZoomMode = new ImageViewerPanZoomInteractiveMode(); 
imageViewer.InteractiveModes.Add(panZoomMode); 
// And Rubber Band mode second 
ImageViewerRubberBandInteractiveMode rubberBandMode = new ImageViewerRubberBandInteractiveMode(); 
imageViewer.InteractiveModes.Add(rubberBandMode); 

Run the example and notice that only pan/zoom is usable: rubber-banding does not work. Now change the code as follows:

// And Rubber band mode first 
ImageViewerRubberBandInteractiveMode rubberBandMode = new ImageViewerRubberBandInteractiveMode(); 
imageViewer.InteractiveModes.Add(rubberBandMode); 
// Add Pan/Zoom mode second 
ImageViewerPanZoomInteractiveMode panZoomMode = new ImageViewerPanZoomInteractiveMode(); 
imageViewer.InteractiveModes.Add(panZoomMode); 

Run the example and notice now that you can draw rubber bands but cannot pan/zoom anymore.

You can also change the order by using the collection to add/remove and insert items in different locations.

The Value of ImageViewerInteractiveMode.IsEnabled

Modes that are disabled will never run and ignore all events. IsEnabled is true by default.

Change the code in the example as follows:

// Add Rubber band first 
ImageViewerRubberBandInteractiveMode rubberBandMode = new ImageViewerRubberBandInteractiveMode(); 
// But disable it 
rubberBandMode.IsEnabled = false; 
imageViewer.InteractiveModes.Add(rubberBandMode); 
// Add Pan/Zoom mode second 
ImageViewerPanZoomInteractiveMode panZoomMode = new ImageViewerPanZoomInteractiveMode(); 
imageViewer.InteractiveModes.Add(panZoomMode); 

Run the example and even though rubber band is the first mode, it is not usable: pan zoom is.

This is handled by each mode through the use of the InteractiveEventArgs.IsHandled property.

The Value of ImageViewerInteractiveMode.MouseButtons

The 'MouseButtons' are used when the device supports mouse input. This value is MouseButtons.Left, but you can change it to attach an interactive mode to a different button.

Change the code in the example as follows:

// Add Rubber band first 
ImageViewerRubberBandInteractiveMode rubberBandMode = new ImageViewerRubberBandInteractiveMode(); 
// Assign it to left mouse button (this is the default) 
rubberBandMode.MouseButtons = MouseButtons.Left; 
imageViewer.InteractiveModes.Add(rubberBandMode); 
// Add Pan/Zoom mode second 
ImageViewerPanZoomInteractiveMode panZoomMode = new ImageViewerPanZoomInteractiveMode(); 
// Assign to the right mouse button 
panZoomMode.MouseButtons = MouseButtons.Right; 
imageViewer.InteractiveModes.Add(panZoomMode); 

Run the example. Now using the left mouse button will draw a rubber band while using the right mouse button will pan and zoom.

The order of the modes and the state of IsEnabled still applies when multiple modes are attached to the same mouse button.

Note that in touch devices that do not support a mouse, the touch input is treated as a left mouse button. Attaching a mode to any other mouse button will have no effect. However, if the application is designed to support both mouse and touch events (for example, a web application) then you can assign modes to left and right mouse buttons for extra functionality when the application is running in a desktop browser that supports mouse or both mouse and touch.

You can also set an interactive mode MouseButtons property to a combination of values. For example MouseButtons.Left | MouseButtons.Right. This will instruct the mode to run when either mouse button is pressed.

Some interactive modes can run when the value of MouseButtons is MouseButtons.None. For example:

// Add auto-pan attached to left mouse button 
ImageViewerAutoPanInteractiveMode autoPanMode = new ImageViewerAutoPanInteractiveMode(); 
autoPanMode.MouseButtons = MouseButtons.Left; 
imageViewer.InteractiveModes.Add(autoPanMode); 

Run the example and click and move the mouse to the edge of the viewer and see how it pans the image to that direction. Now change as follows:

// Add auto-pan, not attached to any button 
ImageViewerAutoPanInteractiveMode autoPanMode = new ImageViewerAutoPanInteractiveMode(); 
autoPanMode.MouseButtons = MouseButtons.None; 
imageViewer.InteractiveModes.Add(autoPanMode); 

Run the example and see how auto-pan works now when no mouse button is pressed.

The Value of ImageViewerInteractiveMode.CanStartWork

This virtual method is called by the base class to check the standard conditions (such as the IsEnabled and MouseButtons property described above) as well as any further custom condition such as the value of WorkOnBounds, Item, ItemPart.

Change the code in the example to use multiple items:

// Create a new image viewer instance with a vertical layout 
ImageViewerViewLayout viewLayout = new ImageViewerVerticalViewLayout(); 
ImageViewer imageViewer = new ImageViewer(viewLayout); 
// Set some properties 
imageViewer.Dock = DockStyle.Fill; 
imageViewer.BackColor = Color.Bisque; 
// Add a border (need some padding as well) 
imageViewer.ImageBorderThickness = 1; 
// Add some padding 
imageViewer.ItemPadding = new Padding(80); 
// Use a border around the items so we can see them 
imageViewer.ItemBackgroundColor = Color.Yellow; 
imageViewer.ItemBorderThickness = 1; 
// Add it to the form 
this.Controls.Add(imageViewer); 
imageViewer.BringToFront(); 
 
// Add a couple of items 
imageViewer.Items.AddFromImageFile(@"C:\LEADTOOLS23\Resources\Images\sample1.png", 1); 
imageViewer.Items.AddFromImageFile(@"C:\LEADTOOLS23\Resources\Images\sample2.png", 1); 

Now add the following interactive mode:

ImageViewerRubberBandInteractiveMode mode = new ImageViewerRubberBandInteractiveMode(); 
imageViewer.InteractiveModes.Add(mode); 

Notice how if you try to draw a rubber band outside the yellow item boxes, the mode does not run. This is because the default value of ItemPart is Content and the default value of WorkOnBounds is true. Meaning: Only work on the content (inside the item) part of an item. The mode will perform hit-testing on the item and if the condition is not met, it will not run.

Change the code as follows:

ImageViewerRubberBandInteractiveMode mode = new ImageViewerRubberBandInteractiveMode(); 
mode.WorkOnBounds = false; 
imageViewer.InteractiveModes.Add(mode); 

Now, even though the part is still Content, we instructed the mode to ignore this value and not perform hit-testing. Run the code and notice how you can pan/zoom on any part inside the viewer control.

To work on a specific part of the item, we need to perform the following steps:

For example, this code will make sure rubber band will only work on the image part of an item.

ImageViewerRubberBandInteractiveMode mode = new ImageViewerRubberBandInteractiveMode(); 
mode.AutoItemMode = ImageViewerAutoItemMode.AutoSet; 
mode.ItemPart = ImageViewerItemPart.Image; 
mode.WorkOnBounds = true; 
imageViewer.InteractiveModes.Add(mode); 

Finally, restrict the mode to work on a certain item (optional). The following code will allow rubber band to work on the second item (index of 1) only. Note that we set AutoItemMode to None so the mode does not change the Item property.

ImageViewerRubberBandInteractiveMode mode = new ImageViewerRubberBandInteractiveMode(); 
mode.AutoItemMode = ImageViewerAutoItemMode.None; 
mode.Item = imageViewer.Items[1]; 
mode.ItemPart = ImageViewerItemPart.Image; 
mode.WorkOnBounds = true; 
imageViewer.InteractiveModes.Add(mode); 

Run the example and notice that the rubber band only works on the second item.

Combining Interactive Modes

Some interactive modes can be combined with others to provide extra functionality. For example, ImageViewerAutoPanInteractiveMode can be combined with any other mode to allow auto-panning the viewer while the other mode is running.

Change the code as follows:

// Add auto-pan, also left button 
ImageViewerAutoPanInteractiveMode autoPanMode = new ImageViewerAutoPanInteractiveMode(); 
autoPanMode.MouseButtons = MouseButtons.Left; 
imageViewer.InteractiveModes.Add(autoPanMode); 
// Add rubber-band, left button 
ImageViewerRubberBandInteractiveMode rubberBandMode = new ImageViewerRubberBandInteractiveMode(); 
rubberBandMode.MouseButtons = MouseButtons.Left; 
imageViewer.InteractiveModes.Add(rubberBandMode); 

Run the example, and notice that when drawing a rubber band and moving towards the edge of the viewer, it will pan in that direction. This is the auto-pan performing extra functionality for the rubber-band.

This is possible because auto-pan does not set the value of InteractiveEventArgs.IsHandled to true. Hence, the event will propagate to the next interactive mode in the list: the rubber-band.

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

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