VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
In This Topic
CustomSelectionTool class
In This Topic
CustomSelectionTool class is intended for selection the region of arbitrary shape on the image. The class is derived from UserInteractionVisualTool 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 CustomSelectionTool.Selection property.

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



The class stores information about the ready-built selection region for each image in image viewer, and so the selection region does not disappear while navigating from one image to another in 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

SelectionRegionBase 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 WinForms image viewer:
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