←Select platform

Shape Property

Summary

Shape to use with this rubber band mode.

Syntax
C#
C++/CLI
public virtual ImageViewerRubberBandShape Shape { get; set; } 
public:  
   virtual property ImageViewerRubberBandShape^ Shape 
   { 
      ImageViewerRubberBandShape^ get() 
      void set(ImageViewerRubberBandShape^ value) 
   } 

Property Value

The shape to use with this rubber band mode. Default value is ImageViewerRubberBandShape.Rectangle.

Remarks

You cannot change the shape if the mode is already working. Doing so will throw an exception.

Example
C#
using Leadtools; 
using Leadtools.Controls; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
 
public ImageViewerForm _form = new ImageViewerForm(); 
public ComboBox _rubberBandShapesComboBox = new ComboBox(); 
public ImageViewer _imageViewer; 
 
public void ImageViewerRubberBandInteractiveModeExample() 
{ 
   // Get the ImageViewer control from the form 
   _imageViewer = _form.ImageViewer; 
 
   // Load an image 
   using (var codecs = new RasterCodecs()) 
      _imageViewer.Image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "image1.cmp")); 
 
   // Add rubber band shape ComboBox to the form 
   _form.Controls.Add(_rubberBandShapesComboBox); 
   _rubberBandShapesComboBox.BringToFront(); 
 
   // Add the shapes to the ComboBox 
   foreach (ImageViewerRubberBandShape shape in Enum.GetValues(typeof(ImageViewerRubberBandShape))) 
      _rubberBandShapesComboBox.Items.Add(shape); 
 
   // Create the interactive mode  
   ImageViewerRubberBandInteractiveMode rubberBandMode = new ImageViewerRubberBandInteractiveMode(); 
   rubberBandMode.BackgroundBrush = null; 
   rubberBandMode.BorderPen = new Pen(Color.FromArgb(0xEF, 128, 128, 0)) { DashStyle = DashStyle.Dash }; 
 
   // Set the default shape as selected in the ComboBox 
   _rubberBandShapesComboBox.SelectedItem = rubberBandMode.Shape; 
 
   // Set the selected ComboBox item as the shape for the interactive mode 
   _rubberBandShapesComboBox.SelectedIndexChanged += (sender, e) => rubberBandMode.Shape = (ImageViewerRubberBandShape)_rubberBandShapesComboBox.SelectedItem; 
 
   //  
   rubberBandMode.RubberBandStarted += (object sender, ImageViewerRubberBandEventArgs e) => 
   { 
      Debug.WriteLine("Rubber-Band started at " + e.Points[0].X + ", " + e.Points[0].Y); 
   }; 
 
   rubberBandMode.RubberBandDelta += (object sender, ImageViewerRubberBandEventArgs e) => 
   { 
      int left = e.Points[0].X; 
      int top = e.Points[0].Y; 
      LeadPoint lastPoint = e.Points[e.Points.Count - 1]; 
      int width = lastPoint.X > left ? lastPoint.X - left : left - lastPoint.X; 
      int height = lastPoint.Y > top ? lastPoint.Y - top : top - lastPoint.Y; 
 
      _form.Text = "Rubber-Band Region: " + left + ", " + top + ", " + width + ", " + height; 
   }; 
 
   // Handle drawing the shape when rubber band selection is complete 
   rubberBandMode.RubberBandCompleted += (object sender, ImageViewerRubberBandEventArgs 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); 
                     else 
                        path.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); 
                        else 
                           firstPoint = 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(); 
} 
 
// Draw a round rectangle 
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 rectangle 
      path.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 little 
   int 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; 
} 
 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
Requirements

Target Platforms

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

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