Draws a shape on the viewer.
public class ImageViewerRubberBandInteractiveMode : ImageViewerInteractiveMode Public Class ImageViewerRubberBandInteractiveModeInherits ImageViewerInteractiveMode
public ref class ImageViewerRubberBandInteractiveMode : ImageViewerInteractiveMode The ImageViewerRubberBandInteractiveMode derives from the ImageViewerInteractiveMode and subscribes to the following events of the InteractiveService:
The ImageViewerRubberBandInteractiveMode works as follows:
When DragStarted is received, InteractiveEventArgs.IsHandled is set to true and Render is called to to draw the rubber band shape Shape, BorderPen, and BackgroundBrush are used to customize the appearance of the shape. The RubberBandStarted event is also fired. Note that if you can derive your own class and override Render to customize the rendering of the shape.
When DragDelta is received, InteractiveEventArgs.IsHandled is set to true and Render is called to update the shape. RubberBandDelta is also fired.
When DragCompleted is received, InteractiveEventArgs.IsHandled is set to true and RubberBandCompleted is fired.
The ImageViewerRubberBandInteractiveMode interactive mode does not perform any action on the viewer (besides drawing, moving and then removing the shape). It is up to the user to implement any custom operation required, (for example, to select a region of interest on the image). ImageViewerZoomToInteractiveMode derives from ImageViewerRubberBandInteractiveMode and calls ZoomToRect event. Drawing a rubber-band on the viewer is usually followed by another user operation to perform the action. In most cases, the rubber band is to be restricted on the "work" area. The RestrictToWorkBounds can control this behavior.
For more information, refer to Image Viewer Interactive Modes.
This example will use ImageViewerRubberBandInteractiveMode to select a region of interest in the image. When the user finishes selecting the area, the example will draw a blue with yellow border rectangle on the image.
Start with the ImageViewer example, remove all the code inside the example function (search for the "// TODO: add example code here" comment) and insert the following code:
using Leadtools;using Leadtools.Controls;using Leadtools.Codecs;using Leadtools.Drawing;using LeadtoolsExamples.Common;using Leadtools.ImageProcessing;using Leadtools.ImageProcessing.Color;public static bool AddRoundRect(GraphicsPath path, LeadRect bounds, int xRadius, int yRadius){if (bounds.Width < 1 || bounds.Height < 1)return false;if (xRadius < 1 || yRadius < 1){// Add a rectanglepath.AddRectangle(new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height));return true;}int x = bounds.X;int y = bounds.Y;int width = bounds.Width;int height = bounds.Height;// adapt horizontal and vertical diameter if the rectangle is too littleint xDiameter = xRadius * 2;int yDiameter = yRadius * 2;if (width < (xDiameter))xDiameter = width;if (height < (yDiameter))yDiameter = height;xRadius = xDiameter / 2;yRadius = yDiameter / 2;int xw = x + width;int yh = y + height;int xwr = xw - xRadius;int yhr = yh - yRadius;int xr = x + xRadius;int yr = y + yRadius;int xwr2 = xw - xDiameter;int yhr2 = yh - yDiameter;path.StartFigure();path.AddArc(x, y, xDiameter, yDiameter, 180, 90);path.AddLine(xr, y, xwr, y);path.AddArc(xwr2, y, xDiameter, yDiameter, 270, 90);path.AddLine(xw, yr, xw, yhr);path.AddArc(xwr2, yhr2, xDiameter, yDiameter, 0, 90);path.AddLine(xwr, yh, xr, yh);path.AddArc(x, yhr2, xDiameter, yDiameter, 90, 90);path.AddLine(x, yhr, x, yr);path.CloseFigure();return true;}public void ImageViewerRubberBandInteractiveMode_Example(){ImageViewerRubberBandInteractiveMode rubberBandMode = new ImageViewerRubberBandInteractiveMode();foreach (ImageViewerRubberBandShape shape in Enum.GetValues(typeof(ImageViewerRubberBandShape)))_rubberBandShapesComboBox.Items.Add(shape);_rubberBandShapesComboBox.SelectedItem = rubberBandMode.Shape;_rubberBandShapesComboBox.SelectedIndexChanged += (sender, e) => rubberBandMode.Shape = (ImageViewerRubberBandShape)_rubberBandShapesComboBox.SelectedItem;rubberBandMode.RubberBandCompleted += (sender, e) =>{if (e.IsCanceled)return;if (rubberBandMode.Item == null)return;LeadPoint[] points = new LeadPoint[e.Points.Count];for (var i = 0; i < points.Length; i++)points[i] = LeadPoint.Create(e.Points[i].X, e.Points[i].Y);LeadPoint min = LeadPoint.Empty;LeadPoint max = LeadPoint.Empty;for (int i = 0; i < points.Length; i++){points[i] = _imageViewer.ConvertPoint(rubberBandMode.Item, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, points[i]);if (i == 0){min = points[i];max = points[i];}else{min.X = Math.Min(min.X, points[i].X);min.Y = Math.Min(min.Y, points[i].Y);max.X = Math.Max(max.X, points[i].X);max.Y = Math.Max(max.Y, points[i].Y);}}LeadPoint center = LeadPoint.Create(min.X + (max.X - min.X) / 2, min.Y + (max.Y - min.Y) / 2);ImageViewerItem rasterItem = rubberBandMode.Item;RasterImage image = rasterItem.Image;IntPtr hdc = RasterImagePainter.CreateLeadDC(image);using (Graphics graphics = Graphics.FromHdc(hdc)){using (GraphicsPath path = new GraphicsPath()){switch (rubberBandMode.Shape){case ImageViewerRubberBandShape.Rectangle:case ImageViewerRubberBandShape.Ellipse:{LeadRect rect = LeadRect.Normalize(LeadRect.FromLTRB(points[0].X, points[0].Y, points[1].X, points[1].Y));Rectangle rc = new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);if (rubberBandMode.Shape == ImageViewerRubberBandShape.Rectangle)path.AddRectangle(rc);elsepath.AddEllipse(rc);}break;case ImageViewerRubberBandShape.RoundRectangle:{LeadSize radius = rubberBandMode.RoundRectangleRadius;LeadRect rect = LeadRect.Normalize(LeadRect.FromLTRB(points[0].X, points[0].Y, points[1].X, points[1].Y));AddRoundRect(path, rect, radius.Width, radius.Height);}break;case ImageViewerRubberBandShape.Freehand:{bool firstPoint = true;LeadPoint lastPoint = LeadPoint.Empty;foreach (LeadPoint pt in points){if (!firstPoint)path.AddLine(lastPoint.X, lastPoint.Y, pt.X, pt.Y);elsefirstPoint = false;lastPoint = pt;}}break;default:break;}path.CloseFigure();if (image.Width > 1000 || image.Height > 1000){using (Pen pen = new Pen(Color.Yellow, 8))graphics.DrawPath(pen, path);}else{graphics.DrawPath(Pens.Yellow, path);}}}RasterImagePainter.DeleteLeadDC(hdc);_imageViewer.Invalidate();};rubberBandMode.AutoItemMode = ImageViewerAutoItemMode.AutoSet;rubberBandMode.ItemPart = ImageViewerItemPart.Image;_imageViewer.InteractiveModes.BeginUpdate();_imageViewer.InteractiveModes.Add(rubberBandMode);ImageViewerAutoPanInteractiveMode autopan = new ImageViewerAutoPanInteractiveMode();autopan.PanDelay = 100;_imageViewer.InteractiveModes.Add(autopan);_imageViewer.InteractiveModes.EndUpdate();}
Imports LeadtoolsImports Leadtools.ControlsImports Leadtools.CodecsImports Leadtools.DrawingImports Leadtools.ImageProcessingImports Leadtools.ImageProcessing.ColorPublic Shared Function AddRoundRect(ByVal path As GraphicsPath, ByVal bounds As LeadRect, ByVal xRadius As Integer, ByVal yRadius As Integer) As BooleanIf bounds.Width < 1 OrElse bounds.Height < 1 ThenReturn FalseEnd IfIf xRadius < 1 OrElse yRadius < 1 Then' Add a rectanglepath.AddRectangle(New Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height))Return TrueEnd IfDim x As Integer = bounds.XDim y As Integer = bounds.YDim width As Integer = bounds.WidthDim height As Integer = bounds.Height' adapt horizontal and vertical diameter if the rectangle is too littleDim xDiameter As Integer = xRadius * 2Dim yDiameter As Integer = yRadius * 2If width < (xDiameter) ThenxDiameter = widthEnd IfIf height < (yDiameter) ThenyDiameter = heightEnd IfxRadius = xDiameter \ 2yRadius = yDiameter \ 2Dim xw As Integer = x + widthDim yh As Integer = y + heightDim xwr As Integer = xw - xRadiusDim yhr As Integer = yh - yRadiusDim xr As Integer = x + xRadiusDim yr As Integer = y + yRadiusDim xwr2 As Integer = xw - xDiameterDim yhr2 As Integer = yh - yDiameterpath.StartFigure()path.AddArc(x, y, xDiameter, yDiameter, 180, 90)path.AddLine(xr, y, xwr, y)path.AddArc(xwr2, y, xDiameter, yDiameter, 270, 90)path.AddLine(xw, yr, xw, yhr)path.AddArc(xwr2, yhr2, xDiameter, yDiameter, 0, 90)path.AddLine(xwr, yh, xr, yh)path.AddArc(x, yhr2, xDiameter, yDiameter, 90, 90)path.AddLine(x, yhr, x, yr)path.CloseFigure()Return TrueEnd FunctionPublic Sub ImageViewerRubberBandInteractiveMode_Example()Dim rubberBandMode As ImageViewerRubberBandInteractiveMode = New ImageViewerRubberBandInteractiveMode()For Each shape As ImageViewerRubberBandShape In System.Enum.GetValues(GetType(ImageViewerRubberBandShape))_rubberBandShapesComboBox.Items.Add(shape)Next shape_rubberBandShapesComboBox.SelectedItem = rubberBandMode.ShapeAddHandler _rubberBandShapesComboBox.SelectedIndexChanged, Sub(sender, e) rubberBandMode.Shape = CType(_rubberBandShapesComboBox.SelectedItem,ImageViewerRubberBandShape)AddHandler rubberBandMode.RubberBandCompleted,Sub(sender, e)If e.IsCanceled ThenReturnEnd IfIf rubberBandMode.Item Is Nothing ThenReturnEnd IfDim points As LeadPoint() = New LeadPoint(e.Points.Count - 1) {}Dim i As Integer = 0Do While i < points.Lengthpoints(i) = LeadPoint.Create(e.Points(i).X, e.Points(i).Y)i += 1LoopDim min As LeadPoint = LeadPoint.EmptyDim max As LeadPoint = LeadPoint.Emptyi = 0Do While i < points.Lengthpoints(i) = _imageViewer.ConvertPoint(rubberBandMode.Item, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, points(i))If i = 0 Thenmin = points(i)max = points(i)Elsemin.X = Math.Min(min.X, points(i).X)min.Y = Math.Min(min.Y, points(i).Y)max.X = Math.Max(max.X, points(i).X)max.Y = Math.Max(max.Y, points(i).Y)End Ifi += 1LoopDim center As LeadPoint = LeadPoint.Create(min.X + (max.X - min.X) \ 2, min.Y + (max.Y - min.Y) \ 2)Dim rasterItem As ImageViewerItem = rubberBandMode.ItemDim image As RasterImage = rasterItem.ImageDim hdc As IntPtr = RasterImagePainter.CreateLeadDC(image)Using graphics As Graphics = graphics.FromHdc(hdc)Using path As GraphicsPath = New GraphicsPath()Select Case rubberBandMode.ShapeCase ImageViewerRubberBandShape.Rectangle, ImageViewerRubberBandShape.EllipseDim rect As LeadRect = LeadRect.Normalize(LeadRect.FromLTRB(points(0).X, points(0).Y, points(1).X, points(1).Y))Dim rc As Rectangle = New Rectangle(rect.X, rect.Y, rect.Width, rect.Height)If rubberBandMode.Shape = ImageViewerRubberBandShape.Rectangle Thenpath.AddRectangle(rc)Elsepath.AddEllipse(rc)End IfCase ImageViewerRubberBandShape.RoundRectangleDim radius As LeadSize = rubberBandMode.RoundRectangleRadiusDim rect As LeadRect = LeadRect.Normalize(LeadRect.FromLTRB(points(0).X, points(0).Y, points(1).X, points(1).Y))AddRoundRect(path, rect, radius.Width, radius.Height)Case ImageViewerRubberBandShape.FreehandDim firstPoint As Boolean = TrueDim lastPoint As LeadPoint = LeadPoint.EmptyFor Each pt As LeadPoint In pointsIf (Not firstPoint) Thenpath.AddLine(lastPoint.X, lastPoint.Y, pt.X, pt.Y)ElsefirstPoint = FalseEnd IflastPoint = ptNext ptCase ElseEnd Selectpath.CloseFigure()If image.Width > 1000 OrElse image.Height > 1000 ThenUsing pen As Pen = New Pen(Color.Yellow, 8)graphics.DrawPath(pen, path)End UsingElsegraphics.DrawPath(Pens.Yellow, path)End IfEnd UsingEnd UsingRasterImagePainter.DeleteLeadDC(hdc)_imageViewer.Invalidate()End SubrubberBandMode.AutoItemMode = ImageViewerAutoItemMode.AutoSetrubberBandMode.ItemPart = ImageViewerItemPart.Image_imageViewer.InteractiveModes.BeginUpdate()_imageViewer.InteractiveModes.Add(rubberBandMode)Dim autopan As ImageViewerAutoPanInteractiveMode = New ImageViewerAutoPanInteractiveMode()autopan.PanDelay = 100_imageViewer.InteractiveModes.Add(autopan)_imageViewer.InteractiveModes.EndUpdate()End Sub
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
