Exemple #1
0
        /// <summary>
        /// Changes the active visual tool in composite visual tool.
        /// </summary>
        /// <param name="compositeVisualTool">The composite visual tool.</param>
        /// <param name="activeTool">The active tool.</param>
        /// <returns>
        /// <b>True</b> if active visual tool is changed; otherwise, <b>false</b>.
        /// </returns>
        private bool ChangeActiveTool(CompositeVisualTool compositeVisualTool, VisualTool activeTool)
        {
            if (activeTool == null)
            {
                compositeVisualTool.ActiveTool = null;

                return(true);
            }
            else
            {
                foreach (VisualTool tool in compositeVisualTool)
                {
                    if (tool == activeTool)
                    {
                        compositeVisualTool.ActiveTool = activeTool;
                        return(true);
                    }
                    else if (tool is CompositeVisualTool)
                    {
                        CompositeVisualTool nestedCompositeTool = (CompositeVisualTool)tool;

                        if (ChangeActiveTool(nestedCompositeTool, activeTool))
                        {
                            compositeVisualTool.ActiveTool = nestedCompositeTool;
                            return(true);
                        }
                    }
                }

                return(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates visual tool actions, which allow to enable/disable visual tools (<see cref="ScrollPages"/> and
        /// the composite visual tool with <see cref="RectangularSelectionTool"/> and <see cref="ScrollPages"/>) 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 scroll pages in image viewer
            VisualToolAction scrollPagesVisualToolAction = new VisualToolAction(
                new ScrollPages(),
                "Scroll Pages",
                "Scroll Pages",
                GetIcon("ScrollPagesTool.png"));

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

            // create visual tool, which allows to select rectangular area in image viewer and scroll pages in image viewer
            CompositeVisualTool selectionAndScrollPages = new CompositeVisualTool(
                new RectangularSelectionTool(), new ScrollPages());
            // create action, which allows to select rectangular area in image viewer and scroll pages in image viewer
            VisualToolAction rectangularSelectionAndScrollPagesVisualToolAction = new VisualToolAction(
                selectionAndScrollPages,
                selectionAndScrollPages.Name,
                selectionAndScrollPages.Name,
                GetIcon("SelectionScrollingTool.png"));

            // add the action to the toolstrip
            toolStrip.AddAction(rectangularSelectionAndScrollPagesVisualToolAction);
        }
Exemple #3
0
        /// <summary>
        /// Returns the visual tool, which is associated with specified visual tool action.
        /// </summary>
        /// <param name="visualToolAction">The visual tool action.</param>
        /// <returns>
        /// The visual tool.
        /// </returns>
        protected virtual VisualTool GetVisualTool(VisualToolAction visualToolAction)
        {
            // if mandatory visual tool is not specified
            if (MandatoryVisualTool == null)
            {
                return(visualToolAction.VisualTool);
            }

            // get current visual tool
            VisualTool result = visualToolAction.VisualTool;

            // if visual tool must be created
            if (!_visualToolItemToVisualTool.TryGetValue(visualToolAction, out result))
            {
                // if action visual tool is not specified
                if (visualToolAction.VisualTool == null)
                {
                    // use mandatory visual tool
                    result = MandatoryVisualTool;
                }
                else
                {
                    // create composite visual tool: mandatory visual tool + action visual tool
                    CompositeVisualTool compositeTool = new CompositeVisualTool(
                        MandatoryVisualTool, visualToolAction.VisualTool);
                    // set active visual tool
                    compositeTool.ActiveTool = visualToolAction.VisualTool;
                    result = compositeTool;
                }

                // save current visual tool
                _visualToolItemToVisualTool.Add(visualToolAction, result);
            }

            return(result);
        }
Exemple #4
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);
        }