/// <summary>
        /// Deactivates this action.
        /// </summary>
        public override void Deactivate()
        {
            CustomSelectionTool visualTool = (CustomSelectionTool)VisualTool;

            visualTool.SelectionChanged -= customSelectionTool_SelectionChanged;

            base.Deactivate();
        }
        /// <summary>
        /// Activates this action.
        /// </summary>
        public override void Activate()
        {
            base.Activate();

            CustomSelectionTool visualTool = (CustomSelectionTool)VisualTool;

            visualTool.SelectionChanged +=
                new EventHandler <CustomSelectionChangedEventArgs>(customSelectionTool_SelectionChanged);
        }
        /// <summary>
        /// Starts the preview.
        /// </summary>
        public void StartPreview()
        {
            // if current tool is CustomSelectionTool
            if (_viewer.VisualTool is CustomSelectionTool)
            {
                CustomSelectionTool selectionTool = (CustomSelectionTool)_viewer.VisualTool;
                RectangleF          selectionBBox = RectangleF.Empty;
                if (selectionTool.Selection != null)
                {
                    selectionBBox = selectionTool.Selection.GetBoundingBox();
                }
                if (selectionBBox.Width >= 1 && selectionBBox.Height >= 1)
                {
                    _selectionRegionView          = selectionTool.Selection.View;
                    _processedSelectionRegionView = new SelectionRegionViewWithImageProcessingPreview();
                    _processedSelectionRegionView.UseViewerZoomForProcessing = UseCurrentViewerZoomWhenPreviewProcessing;
                    selectionTool.Selection.View = _processedSelectionRegionView;
                    _isPreviewStarted            = true;
                    return;
                }
            }

            // set current tool to ImageProcessingTool
            _viewerTool         = _viewer.VisualTool;
            _rectangularPreview = new ImageProcessingTool();
            Rectangle imageRect = new System.Drawing.Rectangle(0, 0, _viewer.Image.Width, _viewer.Image.Height);
            Rectangle rect      = imageRect;

            if (_viewer.VisualTool is RectangularSelectionTool)
            {
                RectangularSelectionTool rectangularSelection = (RectangularSelectionTool)_viewer.VisualTool;
                if (!rectangularSelection.Rectangle.Size.IsEmpty)
                {
                    if (_viewer.VisualTool is RectangularSelectionToolWithCopyPaste)
                    {
                        rect = rectangularSelection.Rectangle;
                    }
                    _rectangularSelectionToolRect = rectangularSelection.Rectangle;
                }
            }
            _rectangularPreview.UseViewerZoomForPreviewProcessing = UseCurrentViewerZoomWhenPreviewProcessing;
            _viewer.VisualTool            = _rectangularPreview;
            _rectangularPreview.Rectangle = rect;
            if (rect == imageRect)
            {
                _rectangularPreview.InteractionController = null;
            }
            _isPreviewStarted = true;
        }
Example #4
0
        /// <summary>
        /// Creates visual tool actions, which allow to enable/disable visual tools (<see cref="RectangularSelectionTool"/> and <see cref="CustomSelectionTool"/>)
        /// in image viewer, and adds actions to the toolstrip.
        /// </summary>
        /// <param name="toolStrip">The toolstrip, where actions must be added.</param>
        public static void CreateActions(VisualToolsToolStrip toolStrip)
        {
            // create action, which allows to select rectangle on image in image viewer
            RectangularSelectionAction rectangularSelectionAction =
                new RectangularSelectionAction(
                    new RectangularSelectionToolWithCopyPaste(),
                    "Rectangular Selection",
                    "Rectangular Selection",
                    GetIcon("RectangularSelectionTool.png"));

            // add the action to the toolstrip
            toolStrip.AddAction(rectangularSelectionAction);


            // create the custom selection tool
            CustomSelectionTool elipticalSelection = new CustomSelectionTool();

            // set the elliptical selection as the current selection in the custom selection tool
            elipticalSelection.Selection = new EllipticalSelectionRegion();

            // create action, which allows to select the elliptical image region in an image viewer
            CustomSelectionAction ellipticalSelectionAction = new CustomSelectionAction(
                elipticalSelection,
                "Elliptical Selection",
                "Elliptical Selection",
                GetIcon("CustomSelectionToolEllipse.png"));

            // add the action to the toolstrip
            toolStrip.AddAction(ellipticalSelectionAction);


            // the default selection region type
            CustomSelectionRegionTypeAction defaultSelectedRegion = null;
            // create action, which allows to select the custom image region in an image viewer
            CustomSelectionAction customSelectionAction =
                new CustomSelectionAction(
                    new CustomSelectionTool(),
                    "Custom Selection",
                    "Custom Selection",
                    null,
                    CreateSelectionRegionTypeActions(out defaultSelectedRegion));

            // set the default selection region type
            customSelectionAction.SelectRegion(defaultSelectedRegion);
            // add the action to the toolstrip
            toolStrip.AddAction(customSelectionAction);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CustomSelectionAction"/> class.
 /// </summary>
 /// <param name="visualTool">The visual tool.</param>
 /// <param name="text">The action text.</param>
 /// <param name="toolTip">The action tool tip.</param>
 /// <param name="icon">The action icon.</param>
 /// <param name="subActions">The sub-actions of the action.</param>
 public CustomSelectionAction(
     CustomSelectionTool visualTool,
     string text,
     string toolTip,
     Image icon,
     params VisualToolAction[] subActions)
     : base(visualTool, text, toolTip, icon, subActions)
 {
     _activatedRegionTypeAction = null;
     if (subActions != null)
     {
         foreach (VisualToolAction subaction in subActions)
         {
             if (subaction is CustomSelectionRegionTypeAction)
             {
                 subaction.Activated += new EventHandler(Action_Activated);
             }
         }
     }
 }
 /// <summary>
 /// Stops the preview.
 /// </summary>
 public void StopPreview()
 {
     if (_viewer.VisualTool is CustomSelectionTool)
     {
         CustomSelectionTool selectionTool = (CustomSelectionTool)_viewer.VisualTool;
         if (selectionTool.Selection != null)
         {
             selectionTool.Selection.View = _selectionRegionView;
         }
     }
     else
     {
         _viewer.VisualTool = _viewerTool;
         if (_viewer.VisualTool is RectangularSelectionTool)
         {
             ((RectangularSelectionTool)_viewer.VisualTool).Rectangle = _rectangularSelectionToolRect;
         }
     }
     _isPreviewStarted = false;
 }
        /// <summary>
        /// Sets the current image processing command.
        /// </summary>
        /// <param name="command">The processing command.</param>
        public void SetCommand(ProcessingCommandBase command)
        {
            if (_isPreviewStarted)
            {
                if (command != null)
                {
                    command.ExpandSupportedPixelFormats = ExpandSupportedPixelFormats;
                }

                if (_viewer.VisualTool is CustomSelectionTool)
                {
                    CustomSelectionTool selectionTool = (CustomSelectionTool)_viewer.VisualTool;
                    _processedSelectionRegionView.ProcessingCommand = command;
                    selectionTool.InvalidateItem(selectionTool.Selection);
                }
                else
                {
                    _rectangularPreview.ProcessingCommand = command;
                }
            }
        }
        /// <summary>
        /// Selects the specified region.
        /// </summary>
        /// <param name="regionTypeAction">The region type action.</param>
        /// <returns>
        /// <b>True</b> - the specified region is selected; otherwise <b>false</b>.
        /// </returns>
        public bool SelectRegion(CustomSelectionRegionTypeAction regionTypeAction)
        {
            if (regionTypeAction == null || _activatedRegionTypeAction == regionTypeAction)
            {
                return(false);
            }

            if (_activatedRegionTypeAction != null)
            {
                _activatedRegionTypeAction.Deactivate();
            }

            _activatedRegionTypeAction = regionTypeAction;

            CustomSelectionTool visualTool = (CustomSelectionTool)VisualTool;

            visualTool.Selection = regionTypeAction.Region;

            SetIcon(regionTypeAction.Icon);

            return(true);
        }
Example #9
0
        /// <summary>
        /// Executes image processing command synchronously or asynchronously.
        /// </summary>
        /// <param name="command">Command to execute.</param>
        /// <param name="async">A value indicating whether to execute command asynchronously.</param>
        public bool ExecuteProcessingCommand(ProcessingCommandBase command, bool async)
        {
            RectangularSelectionToolWithCopyPaste rectSelectionTool = CompositeVisualTool.FindVisualTool <RectangularSelectionToolWithCopyPaste>(_viewer.VisualTool);
            CustomSelectionTool customSelectionTool = CompositeVisualTool.FindVisualTool <CustomSelectionTool>(_viewer.VisualTool);

            if (rectSelectionTool != null)
            {
                // set the region of interest
                Rectangle selectionRectangle = ViewerSelectionRectangle;
                ProcessingCommandWithRegion commandWorkWithRegion = command as ProcessingCommandWithRegion;
                if (commandWorkWithRegion != null)
                {
                    commandWorkWithRegion.RegionOfInterest = new RegionOfInterest(selectionRectangle.Left, selectionRectangle.Top, selectionRectangle.Width, selectionRectangle.Height);
                }
                else if (command is DrawImageCommand)
                {
                    ((DrawImageCommand)command).DestRect = selectionRectangle;
                }
                else if (!selectionRectangle.IsEmpty)
                {
                    MessageBox.Show("Selected image processing command cannot work with regions. Entire image will be processed.",
                                    "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (customSelectionTool != null)
            {
                RectangleF selectionBBox = RectangleF.Empty;
                if (customSelectionTool.Selection != null)
                {
                    selectionBBox = customSelectionTool.Selection.GetBoundingBox();
                }
                if (selectionBBox.Width >= 1 && selectionBBox.Height >= 1)
                {
                    if (command is ChangePixelFormatCommand ||
                        command is ChangePixelFormatToBgrCommand ||
                        command is ChangePixelFormatToBlackWhiteCommand ||
                        command is ChangePixelFormatToGrayscaleCommand ||
                        command is ChangePixelFormatToPaletteCommand ||
                        command is RotateCommand ||
                        command is ResampleCommand ||
                        command is ResizeCommand ||
                        !command.CanModifyImage)
                    {
                        MessageBox.Show("Selected image processing command cannot work with custom selection. Entire image will be processed.",
                                        "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        GraphicsPath path       = customSelectionTool.Selection.GetAsGraphicsPath();
                        RectangleF   pathBounds = path.GetBounds();
                        if (pathBounds.Width > 0 && pathBounds.Height > 0)
                        {
                            if (command is CropCommand)
                            {
                                // crop to custom selection
                                command = GetCropToPathCommand(path, pathBounds, (CropCommand)command);
                                // clear selection
                                customSelectionTool.Selection = null;
                            }
                            else
                            {
                                // process path
                                command = new ProcessPathCommand(command, path);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Selected path is empty.",
                                            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return(false);
                        }
                    }
                }
            }

            // get a reference to the image for processing
            VintasoftImage imageToProcess = _viewer.Image;

            ProcessingCommandBase executeCommand = command;

            if (_executeMultithread)
            {
                ParallelizingProcessingCommand parallelizingCommand = new ParallelizingProcessingCommand(command);
                if (command is ProcessingCommandWithRegion)
                {
                    parallelizingCommand.RegionOfInterest = ((ProcessingCommandWithRegion)command).RegionOfInterest;
                }
                executeCommand = parallelizingCommand;
            }

            if (UndoManager != null)
            {
                _imageProcessingUndoMonitor = new ImageProcessingUndoMonitor(UndoManager, executeCommand);
            }

            // subscribe to the events of the image processing command
            executeCommand.Started  += new EventHandler <ImageProcessingEventArgs>(command_Started);
            executeCommand.Progress += new EventHandler <ImageProcessingProgressEventArgs>(command_Progress);
            executeCommand.Finished += new EventHandler <ImageProcessedEventArgs>(command_Finished);

            executeCommand.ExpandSupportedPixelFormats = ExpandSupportedPixelFormats;
            executeCommand.RestoreSourcePixelFormat    = false;

            // specify that image processing command is working (several commands cannot work together)
            _isImageProcessingWorking = true;
            // get the start time of the image processing command
            _processingCommandStartTime = DateTime.Now;

            // if image processing command should be executed asynchronously
            if (async)
            {
                // start the image processing command asynchronously
                ProcessingCommandTask executor = new ProcessingCommandTask(executeCommand, imageToProcess);
                executor.ImageProcessingExceptionOccurs += new EventHandler(executor_ImageProcessingExceptionOccurs);
                Thread thread = new Thread(executor.Execute);
                thread.IsBackground = true;
                thread.Start();
            }
            // if image processing command should be executed synchronously
            else
            {
                try
                {
                    // execute the image processing command synchronously
                    executeCommand.ExecuteInPlace(imageToProcess);
                }
                catch (Exception ex)
                {
                    executor_ImageProcessingExceptionOccurs(this, EventArgs.Empty);
                    DemosTools.ShowErrorMessage(ex);
                    return(false);
                }
            }

            return(true);
        }