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

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

Вот код C#/VB.NET, демонстрирующий, как использовать визуальный инструмент эллиптического выделения в WPF просмотрщике изображений:
class WpfCustomSelectionToolEllipticalExample
{
    public void RunExample(Vintasoft.Imaging.Wpf.UI.WpfImageViewer viewer)
    {
        // create the CustomSelectionTool object
        Vintasoft.Imaging.Wpf.UI.VisualTools.WpfCustomSelectionTool selectionTool = 
            new Vintasoft.Imaging.Wpf.UI.VisualTools.WpfCustomSelectionTool();
        // 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.Wpf.UI.VisualTools.WpfEllipticalSelectionRegion(
            new System.Windows.Rect(100, 200, 100, 200));

        // create rectangular transformer
        Vintasoft.Imaging.Wpf.UI.VisualTools.UserInteraction.WpfPointBasedObjectRectangularTransformer transformer =
            new Vintasoft.Imaging.Wpf.UI.VisualTools.UserInteraction.WpfPointBasedObjectRectangularTransformer(
                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 WpfCustomSelectionToolEllipticalExample
    Public Sub RunExample(viewer As Vintasoft.Imaging.Wpf.UI.WpfImageViewer)
        ' create the CustomSelectionTool object
        Dim selectionTool As New Vintasoft.Imaging.Wpf.UI.VisualTools.WpfCustomSelectionTool()
        ' 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.Wpf.UI.VisualTools.WpfEllipticalSelectionRegion(New System.Windows.Rect(100, 200, 100, 200))

        ' create rectangular transformer
        Dim transformer As New Vintasoft.Imaging.Wpf.UI.VisualTools.UserInteraction.WpfPointBasedObjectRectangularTransformer(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



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


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

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


Вот код C#/VB.NET, демонстрирующий, как создать выделение на основе кривых Безье, размыть выделенную область изображения, программно изменить выделение и инвертировать выделенную область изображения в WPF просмотрщике изображений:
using Vintasoft.Imaging.Wpf;

class WpfCustomSelectionToolProcessingExample
{
    public void RunExample(Vintasoft.Imaging.Wpf.UI.WpfImageViewer viewer)
    {
        // create the CustomSelectionTool object
        Vintasoft.Imaging.Wpf.UI.VisualTools.WpfCustomSelectionTool selectionTool =
            new Vintasoft.Imaging.Wpf.UI.VisualTools.WpfCustomSelectionTool();
        // 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.Wpf.UI.VisualTools.WpfCurvilinearSelectionRegion(
            new System.Windows.Point[] {
            new System.Windows.Point(100, 100), new System.Windows.Point(100, 200),
            new System.Windows.Point(200, 200), new System.Windows.Point(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.Wpf.UI.VisualTools.UserInteraction.WpfPointBasedObjectPointTransformer transformer =
            new Vintasoft.Imaging.Wpf.UI.VisualTools.UserInteraction.WpfPointBasedObjectPointTransformer(
                selectionTool.Selection);
        // set point-based object transformer as transformer of current selection
        selectionTool.Selection.TransformInteractionController = transformer;

        // insert point to selection
        transformer.InsertPoint(new System.Windows.Point(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.Wpf.UI.VisualTools.WpfSelectionRegionBase selectionRegion,
        Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase command)
    {
        System.Windows.Media.PathGeometry pathGeometry = selectionRegion.GetAsPathGeometry();
        using (Vintasoft.Imaging.Drawing.IGraphicsPath graphicsPath = WpfObjectConverter.CreateGraphicsPath(pathGeometry))
        {
            Vintasoft.Imaging.ImageProcessing.ProcessPathCommand processPath =
                new Vintasoft.Imaging.ImageProcessing.ProcessPathCommand(command, graphicsPath);
            processPath.ExecuteInPlace(image);
        }
    }

}

Imports Vintasoft.Imaging.Wpf

Class WpfCustomSelectionToolProcessingExample
    Public Sub RunExample(viewer As Vintasoft.Imaging.Wpf.UI.WpfImageViewer)
        ' create the CustomSelectionTool object
        Dim selectionTool As New Vintasoft.Imaging.Wpf.UI.VisualTools.WpfCustomSelectionTool()
        ' 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.Wpf.UI.VisualTools.WpfCurvilinearSelectionRegion(New System.Windows.Point() {New System.Windows.Point(100, 100), New System.Windows.Point(100, 200), New System.Windows.Point(200, 200), New System.Windows.Point(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.Wpf.UI.VisualTools.UserInteraction.WpfPointBasedObjectPointTransformer(selectionTool.Selection)
        ' set point-based object transformer as transformer of current selection
        selectionTool.Selection.TransformInteractionController = transformer

        ' insert point to selection
        transformer.InsertPoint(New System.Windows.Point(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.Wpf.UI.VisualTools.WpfSelectionRegionBase, command As Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase)
        Dim pathGeometry As System.Windows.Media.PathGeometry = selectionRegion.GetAsPathGeometry()
        Using graphicsPath As Vintasoft.Imaging.Drawing.IGraphicsPath = WpfObjectConverter.CreateGraphicsPath(pathGeometry)
            Dim processPath As New Vintasoft.Imaging.ImageProcessing.ProcessPathCommand(command, graphicsPath)
            processPath.ExecuteInPlace(image)
        End Using
    End Sub

End Class