VintaSoft Imaging .NET SDK 15.2: Документация для .NET разработчика
В этом разделе
Класс CustomSelectionTool
В этом разделе
Класс CustomSelectionTool предназначен для выделения области произвольной формы на изображении. Класс является производным от класса UserInteractionVisualTool и реализует область выделения в виде интерактивного объекта, позволяющего создавать выделение произвольной формы с возможностью трансформации.

Перед началом работы с инструментом необходимо определить область выделения, которую должен построить инструмент, это можно сделать с помощью свойства CustomSelectionTool.Selection.

Вот код C#/VB.NET, демонстрирующий использование визуального инструмента эллиптического выделения в WinForms просмотрщике изображений:
class CustomSelectionToolEllipticalExample
{
    public void RunExample(Vintasoft.Imaging.UI.ImageViewer viewer)
    {
        // create the CustomSelectionTool object
        Vintasoft.Imaging.UI.VisualTools.CustomSelectionTool selectionTool = 
            new Vintasoft.Imaging.UI.VisualTools.CustomSelectionTool();
        // set the created tool as the current tool of the ImageViewer
        viewer.VisualTool = selectionTool;
        // set current selection to elliptical selection
        selectionTool.Selection = new Vintasoft.Imaging.UI.VisualTools.EllipticalSelectionRegion(
            new System.Drawing.Rectangle(100, 200, 100, 200));

        // create rectangular transformer
        Vintasoft.Imaging.UI.VisualTools.UserInteraction.PointBasedObjectRectangularTransformer transformer =
            new Vintasoft.Imaging.UI.VisualTools.UserInteraction.PointBasedObjectRectangularTransformer(
                selectionTool.Selection);
        // set rectangular transformer as transformer of current selection
        selectionTool.Selection.TransformInteractionController = transformer;

        // move up the selection to 100 pixels
        transformer.Translate(0, -100);
        // rotate the selection to 30 degree
        transformer.Rotate(30);
    }
}

Class CustomSelectionToolEllipticalExample
    Public Sub RunExample(viewer As Vintasoft.Imaging.UI.ImageViewer)
        ' create the CustomSelectionTool object
        Dim selectionTool As New Vintasoft.Imaging.UI.VisualTools.CustomSelectionTool()
        ' set the created tool as the current tool of the ImageViewer
        viewer.VisualTool = selectionTool
        ' set current selection to elliptical selection
        selectionTool.Selection = New Vintasoft.Imaging.UI.VisualTools.EllipticalSelectionRegion(New System.Drawing.Rectangle(100, 200, 100, 200))

        ' create rectangular transformer
        Dim transformer As New Vintasoft.Imaging.UI.VisualTools.UserInteraction.PointBasedObjectRectangularTransformer(selectionTool.Selection)
        ' set rectangular transformer as transformer of current selection
        selectionTool.Selection.TransformInteractionController = transformer

        ' move up the selection to 100 pixels
        transformer.Translate(0, -100)
        ' rotate the selection to 30 degree
        transformer.Rotate(30)
    End Sub
End Class



Класс хранит информацию о готовой области выделения для каждого изображения в просмотрщике изображений, чтобы область выделения не исчезала при переходе от одного изображения к другому в просмотрщике изображений. Например, мы можем создать эллиптическое выделение на первом изображении, затем перейти ко второму изображению и создать выделение из нескольких многоугольников, а затем вернуться к первому изображению, чтобы убедиться, что оно содержит эллиптическое выделение.


Регион выделения

Класс SelectionRegionBase является базовым классом для регионов выделения и позволяет:
Вот список стандартных областей выделения:
При необходимости внешний вид, принцип построения и/или трансформации стандартной области выделения можно переопределить; и, конечно же, вы можете создать свою собственную область выбора с нуля.


Вот код C#/VB.NET, демонстрирующий, как создать выделение на основе кривых Безье, размыть выделенную область изображения, программно изменить выделение и инвертировать выделенную область изображения в WinForms просмотрщике изображений:
class CustomSelectionToolProcessingExample
{
    public void RunExample(Vintasoft.Imaging.UI.ImageViewer viewer)
    {
        // create the CustomSelectionTool object
        Vintasoft.Imaging.UI.VisualTools.CustomSelectionTool selectionTool = 
            new Vintasoft.Imaging.UI.VisualTools.CustomSelectionTool();
        // set the created tool as the current tool of the ImageViewer
        viewer.VisualTool = selectionTool;
        // set current selection to curvilinear selection
        selectionTool.Selection = new Vintasoft.Imaging.UI.VisualTools.CurvilinearSelectionRegion(
            new System.Drawing.PointF[] { 
            new System.Drawing.PointF(100, 100), new System.Drawing.PointF(100, 200),
            new System.Drawing.PointF(200, 200), new System.Drawing.PointF(200, 100) });

        // execute blur command using selection
        ExecuteProcessing(viewer.Image, selectionTool.Selection, 
            new Vintasoft.Imaging.ImageProcessing.Effects.MotionBlurCommand());

        // create point-based object transformer
        Vintasoft.Imaging.UI.VisualTools.UserInteraction.PointBasedObjectPointTransformer transformer =
            new Vintasoft.Imaging.UI.VisualTools.UserInteraction.PointBasedObjectPointTransformer(
                selectionTool.Selection);
        // set point-based object transformer as transformer of current selection
        selectionTool.Selection.TransformInteractionController = transformer;

        // insert point to selection
        transformer.InsertPoint(new System.Drawing.PointF(150, 150), 0);

        // translate selection points
        transformer.TranslatePoints(100, 100);

        // execute invert command using selection
        ExecuteProcessing(viewer.Image, selectionTool.Selection, 
            new Vintasoft.Imaging.ImageProcessing.Color.InvertCommand());

        // clear selection
        selectionTool.Selection = null;
    }

    // Executes image processing command using selection region.
    private void ExecuteProcessing(
        Vintasoft.Imaging.VintasoftImage image,
        Vintasoft.Imaging.UI.VisualTools.SelectionRegionBase selectionRegion,
        Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase command)
    {
        using (System.Drawing.Drawing2D.GraphicsPath path = selectionRegion.GetAsGraphicsPath())
        {
            Vintasoft.Imaging.ImageProcessing.ProcessPathCommand processPath = 
                new Vintasoft.Imaging.ImageProcessing.ProcessPathCommand(command, new Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsPath(path, false));
            processPath.ExecuteInPlace(image);
        }
    }

}

Class CustomSelectionToolProcessingExample
    Public Sub RunExample(viewer As Vintasoft.Imaging.UI.ImageViewer)
        ' create the CustomSelectionTool object
        Dim selectionTool As New Vintasoft.Imaging.UI.VisualTools.CustomSelectionTool()
        ' set the created tool as the current tool of the ImageViewer
        viewer.VisualTool = selectionTool
        ' set current selection to curvilinear selection
        selectionTool.Selection = New Vintasoft.Imaging.UI.VisualTools.CurvilinearSelectionRegion(New System.Drawing.PointF() {New System.Drawing.PointF(100, 100), New System.Drawing.PointF(100, 200), New System.Drawing.PointF(200, 200), New System.Drawing.PointF(200, 100)})

        ' execute blur command using selection
        ExecuteProcessing(viewer.Image, selectionTool.Selection, New Vintasoft.Imaging.ImageProcessing.Effects.MotionBlurCommand())

        ' create point-based object transformer
        Dim transformer As New Vintasoft.Imaging.UI.VisualTools.UserInteraction.PointBasedObjectPointTransformer(selectionTool.Selection)
        ' set point-based object transformer as transformer of current selection
        selectionTool.Selection.TransformInteractionController = transformer

        ' insert point to selection
        transformer.InsertPoint(New System.Drawing.PointF(150, 150), 0)

        ' translate selection points
        transformer.TranslatePoints(100, 100)

        ' execute invert command using selection
        ExecuteProcessing(viewer.Image, selectionTool.Selection, New Vintasoft.Imaging.ImageProcessing.Color.InvertCommand())

        ' clear selection
        selectionTool.Selection = Nothing
    End Sub

    ' Executes image processing command using selection region.
    Private Sub ExecuteProcessing(image As Vintasoft.Imaging.VintasoftImage, selectionRegion As Vintasoft.Imaging.UI.VisualTools.SelectionRegionBase, command As Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase)
        Using path As System.Drawing.Drawing2D.GraphicsPath = selectionRegion.GetAsGraphicsPath()
            Dim processPath As New Vintasoft.Imaging.ImageProcessing.ProcessPathCommand(command, New Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsPath(path, False))
            processPath.ExecuteInPlace(image)
        End Using
    End Sub

End Class