(Leadtools.Controls)

フレームを表示

ImageViewerRubberBandInteractiveModeクラス




メンバ 
ビューワの上でシェイプを描画します。
オブジェクトモデル
構文
public class ImageViewerRubberBandInteractiveMode : ImageViewerInteractiveMode 
'Declaration
 
Public Class ImageViewerRubberBandInteractiveMode 
   Inherits ImageViewerInteractiveMode
解説

ImageViewerRubberBandInteractiveModeImageViewerInteractiveModeから派生し、InteractiveServiceの以下のイベントにサブスクライブします。

ImageViewerRubberBandInteractiveModeは、以下のように機能します:

  1. DragStartedが受け取られるとき、レンダリングはラバーバンドシェイプを描画するために呼び出されます。以下のプロパティは、このシェイプの外観をカスタマイズするのに用いられます:ShapeBorderPenBackgroundBrushとイベントRubberBandStartedは、発生されます。

    独自のクラスを派生し、レンダリングをオーバーライドしてシェイプの描画をカスタマイズできます。

  2. DragDeltaを受け取ると、レンダリングが呼び出されてシェイプが更新されRubberBandDeltaが発生します。

  3. DragCompletedが受け取られるとき、RubberBandCompletedは発生されます。

ImageViewerRubberBandInteractiveMode対話モードは、ビューワ(シェイプの描画、移動、削除以外で)の上で、任意のアクションを実行しません。必要とされる任意のカスタム操作を実装することは、ユーザーまでです。たとえばimage上で任意のリージョンを選ぶなどです。ImageViewerZoomToInteractiveModeImageViewerRubberBandInteractiveModeから派生し、RubberBandCompletedイベントを受け取るとImageViewer.ZoomToRectを呼び出します。

ビューワ上にラバーバンドを描画した後は、通常別のユーザー操作が続いてアクションを実行します。ほとんどの場合、ラバーバンドは「作業」領域に限定されます。RestrictToWorkBoundsは、この動作を制御できます。

詳細については、「画像ビューワの対話モード」を参照してください。

サンプル

このサンプルは、ImageViewerRubberBandInteractiveModeを使って画像の任意の領域を選択します。ユーザーが領域の選択を完了すると、境界線が黄色の青い四角形が画像の上に描画されます。

ImageViewerサンプルからサンプル関数内のコードをすべて削除し("// TODO:ここにサンプルコードを追加します"というコメントを検索する)、以下のコードを挿入します。

Copy Code  
Imports Leadtools
     Imports Leadtools.Controls
     Imports Leadtools.Codecs
     Imports Leadtools.Drawing
     Imports Leadtools.ImageProcessing
     Imports Leadtools.ImageProcessing.Color

     Public Shared Function AddRoundRect(ByVal path As GraphicsPath, ByVal bounds As LeadRect, ByVal xRadius As Integer, ByVal yRadius As Integer) As Boolean
        If bounds.Width < 1 OrElse bounds.Height < 1 Then
           Return False
        End If
        If xRadius < 1 OrElse yRadius < 1 Then
           ' Add a rectangle
           path.AddRectangle(New Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height))
           Return True
        End If

        Dim x As Integer = bounds.X
        Dim y As Integer = bounds.Y
        Dim width As Integer = bounds.Width
        Dim height As Integer = bounds.Height

        ' adapt horizontal and vertical diameter if the rectangle is too little
        Dim xDiameter As Integer = xRadius * 2
        Dim yDiameter As Integer = yRadius * 2
        If width < (xDiameter) Then
           xDiameter = width
        End If
        If height < (yDiameter) Then
           yDiameter = height
        End If
        xRadius = xDiameter \ 2
        yRadius = yDiameter \ 2

        Dim xw As Integer = x + width
        Dim yh As Integer = y + height
        Dim xwr As Integer = xw - xRadius
        Dim yhr As Integer = yh - yRadius
        Dim xr As Integer = x + xRadius
        Dim yr As Integer = y + yRadius
        Dim xwr2 As Integer = xw - xDiameter
        Dim yhr2 As Integer = 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
     End Function

     <TestMethod>
     Public 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.Shape

        AddHandler _rubberBandShapesComboBox.SelectedIndexChanged, Sub(sender, e) rubberBandMode.Shape = CType(_rubberBandShapesComboBox.SelectedItem,  _
ImageViewerRubberBandShape)

        AddHandler rubberBandMode.RubberBandCompleted,
            Sub(sender, e)
               If e.IsCanceled Then
                  Return
               End If

               If rubberBandMode.Item Is Nothing Then
                  Return
               End If

               Dim points As LeadPoint() = New LeadPoint(e.Points.Count - 1) {}
               Dim i As Integer = 0
               Do While i < points.Length
                  points(i) = LeadPoint.Create(e.Points(i).X, e.Points(i).Y)
                  i += 1
               Loop

               Dim min As LeadPoint = LeadPoint.Empty
               Dim max As LeadPoint = LeadPoint.Empty

               i = 0
               Do While i < points.Length
                  points(i) = _imageViewer.ConvertPoint(rubberBandMode.Item, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, points(i))
                  If i = 0 Then
                     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)
                  End If
                  i += 1
               Loop

               Dim center As LeadPoint = LeadPoint.Create(min.X + (max.X - min.X) \ 2, min.Y + (max.Y - min.Y) \ 2)

               Dim rasterItem As ImageViewerItem = rubberBandMode.Item
               Dim image As RasterImage = rasterItem.Image
               Dim hdc As IntPtr = RasterImagePainter.CreateLeadDC(image)
               Using graphics As Graphics = graphics.FromHdc(hdc)
                  Using path As GraphicsPath = New GraphicsPath()
                     Select Case rubberBandMode.Shape
                        Case ImageViewerRubberBandShape.Rectangle, ImageViewerRubberBandShape.Ellipse
                           Dim 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 Then
                              path.AddRectangle(rc)
                           Else
                              path.AddEllipse(rc)
                           End If

                        Case ImageViewerRubberBandShape.RoundRectangle
                           Dim radius As LeadSize = rubberBandMode.RoundRectangleRadius
                           Dim 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.Freehand
                           Dim firstPoint As Boolean = True
                           Dim lastPoint As LeadPoint = LeadPoint.Empty

                           For Each pt As LeadPoint In points
                              If (Not firstPoint) Then
                                 path.AddLine(lastPoint.X, lastPoint.Y, pt.X, pt.Y)
                              Else
                                 firstPoint = False
                              End If

                              lastPoint = pt
                           Next pt

                        Case Else
                     End Select

                     path.CloseFigure()

                     If image.Width > 1000 OrElse image.Height > 1000 Then
                        Using pen As Pen = New Pen(Color.Yellow, 8)
                           graphics.DrawPath(pen, path)
                        End Using
                     Else
                        graphics.DrawPath(Pens.Yellow, path)
                     End If
                  End Using
               End Using

               RasterImagePainter.DeleteLeadDC(hdc)
               _imageViewer.Invalidate()
            End Sub

        rubberBandMode.AutoItemMode = ImageViewerAutoItemMode.AutoSet
        rubberBandMode.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
using Leadtools;
using Leadtools.Controls;
using Leadtools.Codecs;
using Leadtools.Drawing;
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 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;
}

[TestMethod]
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);
                     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();
}
必要条件

ターゲットプラットホーム

参照

参照

ImageViewerRubberBandInteractiveModeメンバ
Leadtools.Controls名前空間