VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
In This Topic
WpfCustomSelectionTool class
In This Topic
WpfCustomSelectionTool class is intended for selection the region of arbitrary shape on the image. The class is derived from WpfUserInteractionVisualTool class and implements the selection region in a form of interactive object, allowing to create the selection of arbitrary shape with transformation ability.

Before starting to work with the tool is necessary to define the selection region, that the tool should build, this can be done using WpfCustomSelectionTool.Selection property.

Here is C#/VB.NET code that demonstrates how to use an elliptical selection visual tool in WPF image viewer:
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



The class stores information about the ready-built selection region for each image in WPF image viewer, and so the selection region does not disappear while navigating from one image to another in WPF image viewer. For example, we may build an elliptical selection on the first image, then navigate to the second image and build a selection from several polygons, then navigate back to the first image to make sure it contains elliptical selection.


Selection region

WpfSelectionRegionBase class, the base class for selection regions, allows to:
Here is a list of standard selection regions:
If necessary, the appearance, the principle of construction and/or transformation of standard selection region can be overridden; and of course you can create your own selection region from scratch.


Here is C#/VB.NET code that demonstrates how to create a selection on base of Bezier curves, blur the selected area of image, change the selection programmatically and invert the selected area of image in WPF image viewer:
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