ScreenToGif.Modern.ApplyActionToFrames C# (CSharp) Method

ApplyActionToFrames() private method

Apply desired option on selected frame(s)
The purpose of this function is that we can apply almost every action/option to the selected frame(s), so if you want to use it please be sure that the desired action is added to [_actionEnum] Enum.
/// Thrown when we have an invalid value of pickerValue /// Thrown when we have an unknown option [optionType]
private ApplyActionToFrames ( string actionLabel, ActionEnum actionType, float pickerValue, object param = null ) : void
actionLabel string /// Name of option used to display error message
actionType ActionEnum /// Type of option referenced by an enumerator
pickerValue float /// Value of picker used for Pixelate and Blur filter
param object
return void
        private void ApplyActionToFrames(string actionLabel, ActionEnum actionType, float pickerValue = 0, object param = null)
        {
            IList listIndexSelectedFrames;

            this.Invoke((Action)ResetUndoProp);

            int actualFrame = 0;

            this.Invoke((Action)delegate
            { actualFrame = trackBar.Value; });

            #region Apply Action to the Selected Frames

            if (tvFrames.IsFrameSelected(out listIndexSelectedFrames, actualFrame))
            {
                int removedCount = 0;
                bool overwrite = true;

                for (int i = 0; i < listIndexSelectedFrames.Count; i++)
                {
                    var frameIndex = (int)listIndexSelectedFrames[i];
                    var currentFrame = _listFramesEdit[frameIndex - removedCount].From();

                    #region Switch ActionType

                    switch (actionType)
                    {
                        case ActionEnum.Pixelate:
                            currentFrame = ImageUtil.Pixelate(currentFrame,
                                                        new Rectangle(0, 0, currentFrame.Width,
                                                        currentFrame.Height), Convert.ToInt32(pickerValue));
                            break;

                        case ActionEnum.Blur:
                            currentFrame = ImageUtil.Blur(currentFrame,
                                                        new Rectangle(0, 0, currentFrame.Width,
                                                        currentFrame.Height), Convert.ToInt32(pickerValue));
                            break;

                        case ActionEnum.Grayscale:
                            currentFrame =
                                        ImageUtil.Grayscale(currentFrame);
                            break;

                        case ActionEnum.Color:
                            currentFrame =
                                        ImageUtil.Colorize(currentFrame, (Color)param);
                            break;

                        case ActionEnum.Negative:
                            currentFrame =
                                        ImageUtil.Negative(currentFrame);
                            break;

                        case ActionEnum.Sepia:
                            currentFrame =
                                        ImageUtil.SepiaTone(currentFrame);
                            break;

                        case ActionEnum.Border:
                            currentFrame =
                                        ImageUtil.Border(currentFrame, pickerValue, (Color) param);
                            break;
                        case ActionEnum.Delete:
                            #region Delete

                            //index - 1 to delete the right frame.
                            _listFramesEdit.RemoveAt(frameIndex - removedCount);
                            _listDelayEdit.RemoveAt(frameIndex - removedCount);
                            tvFrames.Remove(1);
                            trackBar.Maximum = _listFramesEdit.Count - 1;
                            removedCount++;
                            overwrite = false;

                            #endregion
                            break;

                        case ActionEnum.Speed:
                            #region Speed

                            int value = Convert.ToInt32(_listDelayEdit[frameIndex] * pickerValue);

                            if (value >= 10 && value <= 2500)
                            {
                                _listDelayEdit[frameIndex] = value;
                            }
                            else if (value < 10) //Minimum
                            {
                                _listDelayEdit[frameIndex] = 10;
                            }
                            else if (value > 2500) //Maximum
                            {
                                _listDelayEdit[frameIndex] = 2500;
                            }

                            #endregion
                            break;

                        case ActionEnum.Caption:

                            #region Caption

                            if (param == null) break;

                            currentFrame = _listFramesEdit[frameIndex].From();

                            using (Graphics imgGr = Graphics.FromImage(currentFrame))
                            {
                                var graphPath = new GraphicsPath();

                                var fSt = (int)Settings.Default.fontCaption.Style;
                                var fF = Settings.Default.fontCaption.FontFamily;

                                StringFormat sFr = StringFormat.GenericDefault;

                                #region Horizontal Alignment

                                if (Settings.Default.captionHorizontalAlign == StringAlignment.Near)
                                {
                                    sFr.Alignment = StringAlignment.Near;
                                }
                                else if (Settings.Default.captionHorizontalAlign == StringAlignment.Center)
                                {
                                    sFr.Alignment = StringAlignment.Center;
                                }
                                else
                                {
                                    sFr.Alignment = StringAlignment.Far;
                                }

                                #endregion

                                #region Vertical Alignment

                                if (Settings.Default.captionVerticalAlign == StringAlignment.Near)
                                {
                                    sFr.LineAlignment = StringAlignment.Near;
                                }
                                else if (Settings.Default.captionVerticalAlign == StringAlignment.Center)
                                {
                                    sFr.LineAlignment = StringAlignment.Center;
                                }
                                else
                                {
                                    sFr.LineAlignment = StringAlignment.Far;
                                }

                                #endregion

                                #region Draw the string using a specific sizing type. Percentage of the height or Points

                                if (Settings.Default.fontSizeAsPercentage)
                                {
                                    graphPath.AddString(param.ToString(), fF, fSt, (currentFrame.Height * Settings.Default.fontCaptionPercentage),
                                        new Rectangle(new Point(0, 0), currentFrame.Size), sFr);
                                }
                                else
                                {
                                    graphPath.AddString(param.ToString(), fF, fSt, Settings.Default.fontCaption.Size, new Rectangle(new Point(0, 0), currentFrame.Size), sFr);
                                }

                                #endregion

                                #region Draw the path to the surface

                                if (Settings.Default.captionUseOutline)
                                {
                                    imgGr.DrawPath(new Pen(Settings.Default.captionOutlineColor,
                                            Settings.Default.captionOutlineThick), graphPath);
                                }
                                else
                                {
                                    imgGr.DrawPath(new Pen(Color.Transparent), graphPath);
                                }

                                #endregion

                                #region Fill the path with a solid color or a hatch brush

                                if (Settings.Default.captionUseHatch)
                                {
                                    imgGr.FillPath(new HatchBrush(Settings.Default.captionHatch, Settings.Default.captionHatchColor,
                                            Settings.Default.fontCaptionColor), graphPath);
                                }
                                else
                                {
                                    imgGr.FillPath(new SolidBrush(Settings.Default.fontCaptionColor), graphPath);
                                }

                                #endregion
                            }

                            #endregion
                            break;
                        case ActionEnum.Export:

                            #region Export

                            if (param.Equals(ImageFormat.Png))
                            {
                                currentFrame.Save(actionLabel + " (" + frameIndex + ").png", ImageFormat.Png);
                            }
                            else
                            {
                                currentFrame.Save(actionLabel + " (" + frameIndex + ").jpg", ImageFormat.Jpeg);
                            }

                            #endregion
                            break;
                        case ActionEnum.AddText:

                            #region AddText

                            Brush textBrush = new SolidBrush(Settings.Default.forecolorInsertText);

                            using (var myGraphic = Graphics.FromImage(currentFrame))
                            {
                                //Define the rectangle size by taking in consideration [X] and [Y] of 
                                // [_pointTextPosition] so the text matches the Bitmap l
                                var rectangleSize = new Size(currentFrame.Width - _pointTextPosition.X,
                                    currentFrame.Height - _pointTextPosition.Y);

                                //Insert text in the specified Point
                                myGraphic.DrawString(param.ToString(), Settings.Default.fontInsertText, textBrush, new Rectangle(_pointTextPosition,
                                    rectangleSize), new StringFormat());
                            }

                            #endregion
                            break;
                        case ActionEnum.FlipRotate:

                            currentFrame.RotateFlip((RotateFlipType)param);

                            break;
                        case ActionEnum.FreeDraw:

                            #region FreeDraw

                            using (var graphics = Graphics.FromImage(currentFrame))
                            {
                                graphics.DrawImage((Image)param, 0, 0);
                            }

                            #endregion
                            break;
                        case ActionEnum.Progress:

                            #region Progress

                            using (Graphics imgGr = Graphics.FromImage(currentFrame))
                            {
                                float actualValue = frameIndex + 1;
                                float maxValue = _listFramesEdit.Count;

                                Brush fillBrush = null;

                                #region Pen

                                float thickness = Settings.Default.progressThickAsPercentage ? Settings.Default.progressThickPercentage : Settings.Default.progressThickness;

                                if (Settings.Default.progressUseHatch)
                                {
                                    fillBrush = new HatchBrush(Settings.Default.progressHatch, Settings.Default.progressColor, Color.Transparent);
                                }
                                else
                                {
                                    fillBrush = new SolidBrush(Settings.Default.progressColor);
                                }

                                #endregion

                                #region Position

                                var rectangle = new Rectangle();

                                if (Settings.Default.progressPosition.Equals('T'))
                                {
                                    // 100 * 50 / 100
                                    float width = ((100 * actualValue) / maxValue) / 100;
                                    rectangle = new Rectangle(0, 0, (int)(currentFrame.Size.Width * width), (int)Settings.Default.progressThickness);
                                }
                                else if (Settings.Default.progressPosition.Equals('B'))
                                {
                                    float width = ((100 * actualValue) / maxValue) / 100;
                                    rectangle = new Rectangle(0, (int)(currentFrame.Size.Height - Settings.Default.progressThickness), (int)(currentFrame.Size.Width * width), (int)Settings.Default.progressThickness);
                                }
                                else if (Settings.Default.progressPosition.Equals('L'))
                                {
                                    float height = ((100 * actualValue) / maxValue) / 100;
                                    rectangle = new Rectangle(0, 0, (int)Settings.Default.progressThickness, (int)(currentFrame.Size.Height * height));
                                }
                                else
                                {
                                    float height = ((100 * actualValue) / maxValue) / 100;
                                    rectangle = new Rectangle((int)(currentFrame.Size.Width - Settings.Default.progressThickness), 0, (int)Settings.Default.progressThickness, (int)(currentFrame.Size.Height * height));
                                }

                                imgGr.FillRectangle(fillBrush, rectangle);

                                #endregion
                            }

                            #endregion

                            break;
                    }

                    #endregion

                    if (overwrite)
                    {
                        File.Delete(_listFramesEdit[frameIndex - removedCount]);

                        currentFrame.Save(_listFramesEdit[frameIndex - removedCount]);
                        currentFrame.Dispose();
                    }

                    GC.Collect(1);
                }

                this.Invoke((Action)delegate
                {
                    trackBar_ValueChanged(null, null);

                    //Highlight the node.
                    tvFrames.Focus();
                    tvFrames.SelectedNode =
                        tvFrames.Nodes[_parentNodeLabel].Nodes[trackBar.Value];
                });
            }

            #endregion
        }
Modern