/// <summary>
        /// Function to return the file path of a captured image. ImageFormat is based on length of the image.
        /// </summary>
        /// <param name="img">The actual image</param>
        /// <param name="filePath">The path to where the image will be saved</param>
        /// <returns>Returns the file path to a screenshot</returns>
        public static string SaveImage(ref WorkerTask task)
        {
            Image img = task.MyImage;
            string filePath = task.LocalFilePath;

            if (!string.IsNullOrEmpty(filePath))
            {
                img = ImageEffects.ApplySizeChanges(img);
                img = ImageEffects.ApplyScreenshotEffects(img);
                if (task.Job != WorkerTask.Jobs.UploadFromClipboard || !Engine.conf.WatermarkExcludeClipboardUpload)
                {
                    img = ImageEffects.ApplyWatermark(img);
                }

                long size = (long)Engine.conf.SwitchAfter * 1024;

                MemoryStream ms = null;

                GraphicsMgr.SaveImageToMemoryStreamOptions opt = new GraphicsMgr.SaveImageToMemoryStreamOptions(img, Engine.zImageFileFormat);
                opt.GIFQuality = Engine.conf.GIFQuality;
                opt.JpgQuality = Engine.conf.JpgQuality;
                opt.MakeJPGBackgroundWhite = Engine.conf.MakeJPGBackgroundWhite;

                try
                {
                    ms = GraphicsMgr.SaveImageToMemoryStream(opt);

                    if (ms.Length > size && size != 0)
                    {
                        opt.MyImageFileFormat = Engine.zImageFileFormatSwitch;
                        ms = GraphicsMgr.SaveImageToMemoryStream(opt);
                        filePath = Path.ChangeExtension(filePath, Engine.zImageFileFormatSwitch.Extension);
                    }

                    if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    }

                    int retry = 3;
                    while (retry > 0 && !File.Exists(filePath))
                    {
                        using (FileStream fi = File.Create(filePath))
                        {
                            if (retry < 3) { System.Threading.Thread.Sleep(1000); }
                            FileSystem.AppendDebug(string.Format("Writing image {0}x{1} to {2}", img.Width, img.Height, filePath));
                            ms.WriteTo(fi);
                            retry--;
                        }
                    }
                }
                catch (Exception ex)
                {
                    FileSystem.AppendDebug("Error while saving image", ex);
                }
                finally
                {
                    if (ms != null) ms.Dispose();
                }
            }

            task.UpdateLocalFilePath(filePath);
            return filePath;
        }
        /// <summary>
        /// Function to return the file path of a captured image. ImageFormat is based on length of the image.
        /// </summary>
        /// <param name="img">The actual image</param>
        /// <param name="filePath">The path to where the image will be saved</param>
        /// <returns>Returns the file path to a screenshot</returns>
        public static string SaveImage(ref WorkerTask task)
        {
            Image  img      = task.MyImage;
            string filePath = task.LocalFilePath;

            if (!string.IsNullOrEmpty(filePath))
            {
                img = ImageEffects.ApplySizeChanges(img);
                img = ImageEffects.ApplyScreenshotEffects(img);
                if (task.Job != WorkerTask.Jobs.UploadFromClipboard || !Engine.conf.WatermarkExcludeClipboardUpload)
                {
                    img = ImageEffects.ApplyWatermark(img);
                }

                long size = (long)Engine.conf.SwitchAfter * 1024;

                MemoryStream ms = null;

                GraphicsMgr.SaveImageToMemoryStreamOptions opt = new GraphicsMgr.SaveImageToMemoryStreamOptions(img, Engine.zImageFileFormat);
                opt.GIFQuality             = Engine.conf.GIFQuality;
                opt.JpgQuality             = Engine.conf.JpgQuality;
                opt.MakeJPGBackgroundWhite = Engine.conf.MakeJPGBackgroundWhite;

                try
                {
                    ms = GraphicsMgr.SaveImageToMemoryStream(opt);

                    if (ms.Length > size && size != 0)
                    {
                        opt.MyImageFileFormat = Engine.zImageFileFormatSwitch;
                        ms       = GraphicsMgr.SaveImageToMemoryStream(opt);
                        filePath = Path.ChangeExtension(filePath, Engine.zImageFileFormatSwitch.Extension);
                    }

                    if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    }

                    int retry = 3;
                    while (retry > 0 && !File.Exists(filePath))
                    {
                        using (FileStream fi = File.Create(filePath))
                        {
                            if (retry < 3)
                            {
                                System.Threading.Thread.Sleep(1000);
                            }
                            FileSystem.AppendDebug(string.Format("Writing image {0}x{1} to {2}", img.Width, img.Height, filePath));
                            ms.WriteTo(fi);
                            retry--;
                        }
                    }
                }
                catch (Exception ex)
                {
                    FileSystem.AppendDebug("Error while saving image", ex);
                }
                finally
                {
                    if (ms != null)
                    {
                        ms.Dispose();
                    }
                }
            }

            task.UpdateLocalFilePath(filePath);
            return(filePath);
        }