private void tbSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(SaveFolder))
            {
                SaveFolder = Path.GetTempPath();
            }

            SaveFileDialog sd = new SaveFileDialog
            {
                Filter             = "png files (*.png)|*.png|jpg files (*.jpg)|*.jpg",
                FilterIndex        = 1,
                FileName           = "",
                CheckFileExists    = false,
                OverwritePrompt    = false,
                AutoUpgradeEnabled = true,
                CheckPathExists    = true,
                InitialDirectory   = SaveFolder,
                RestoreDirectory   = true
            };
            var result = sd.ShowDialog();

            if (result != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            SavedImageFile = sd.FileName;
            try
            {
                CapturedBitmap?.Save(SavedImageFile);
            }
            catch (Exception ex)
            {
                Cancelled       = true;
                StatusText.Text = "Error saving image: " + ex.Message;
                return;
            }

            if (!string.IsNullOrEmpty(ResultFilePath))
            {
                File.WriteAllText(ResultFilePath, SavedImageFile);
            }

            Cancelled = false;

            if (AutoClose)
            {
                Close();
            }

            ExternalWindow?.Show();
            ExternalWindow?.Activate();

            if (WindowState != WindowState.Maximized && WindowState != WindowState.Minimized)
            {
                ScreenCaptureConfiguration.Current.WindowWidth  = this.Width;
                ScreenCaptureConfiguration.Current.WindowHeight = this.Width;
            }
        }
        internal void StopCapture(bool cancelCapture = false)
        {
            if (!IsMouseClickCapturing)
            {
                return;
            }

            CaptureTimer.Dispose();

            IsMouseClickCapturing = false;

            Overlay?.Close();
            WindowUtilities.DoEvents();

            //Point pt = GetMousePosition();
            //CurWindow = new WindowInfo(ScreenCapture.WindowFromPoint(new System.Drawing.Point((int)pt.X, (int)pt.Y)));

            Desktop.Topmost = true;
            Desktop.Activate();
            WindowUtilities.DoEvents();

            if (LastWindow != null)
            {
                var img = ScreenCapture.CaptureWindow(CurWindow.Rect);
                CapturedBitmap = new Bitmap(img);

                //CapturedBitmap = ScreenCapture.CaptureWindowBitmap(CurWindow.Handle) as Bitmap;
                Desktop.Close();
                if (CapturedBitmap != null)
                {
                    ImageCaptured.Source = ScreenCapture.BitmapToBitmapSource(CapturedBitmap);
                    StatusText.Text      = "Image capture from Screen: " + $"{CapturedBitmap.Width}x{CapturedBitmap.Height}";
                    ScreenCaptureForm_SizeChanged(this, null);
                }
                else
                {
                    StatusText.Text = "Image capture failed.";
                }
            }

            //Desktop.Topmost = false;
            Desktop?.Close();

            if (ExternalWindow != null)
            {
                ExternalWindow.Show();
                ExternalWindow.Activate();
            }

            if (cancelCapture)
            {
                CancelCapture();
            }
            else
            {
                Show();
                Activate();
            }
        }
        private void CancelCapture()
        {
            if (!string.IsNullOrEmpty(ResultFilePath))
            {
                File.WriteAllText(ResultFilePath, "Cancelled");
            }

            Cancelled = true;
            Close();

            ExternalWindow?.Activate();
        }
        private void CancelCapture(bool dontCloseForm = false)
        {
            if (!string.IsNullOrEmpty(ResultFilePath))
            {
                File.WriteAllText(ResultFilePath, "Cancelled");
            }

            Cancelled = true;

            if (!dontCloseForm)
            {
                Close();
                ExternalWindow?.Activate();
            }
            else
            {
                Show();
                Activate();
            }
        }
        private void tbSave_Click(object sender, RoutedEventArgs e)
        {
            if (CapturedBitmap == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(SaveFolder))
            {
                SaveFolder = Path.GetTempPath();
            }

            var link = FileSaver.SaveBitmapAndLinkInEditor(CapturedBitmap, noEditorEmbedding: true);

            if (link == null)
            {
                StatusBar.ShowStatusError("Failed to save the active image.");
                return;
            }

            SavedImageFile = link;

            ////var sd = new SaveFileDialog
            ////{
            ////    Filter = "png files (*.png)|*.png|jpg files (*.jpg)|*.jpg",
            ////    FilterIndex = 1,
            ////    FileName = "",
            ////    CheckFileExists = false,
            ////    OverwritePrompt = false,
            ////    AutoUpgradeEnabled = true,
            ////    CheckPathExists = true,
            ////    InitialDirectory = SaveFolder,
            ////    RestoreDirectory = true
            ////};
            ////var result = sd.ShowDialog();
            ////if (result != System.Windows.Forms.DialogResult.OK)
            ////    return;

            ////var ext = Path.GetExtension(sd.FileName);
            ////SavedImageFile = sd.FileName;
            ////try
            ////{
            ////    if (ext == ".jpg" || ext == "jpeg")
            ////        mmImageUtils.SaveJpeg(CapturedBitmap, SavedImageFile,
            ////                               mmApp.Configuration.Images.JpegImageCompressionLevel);
            ////    else
            ////        CapturedBitmap.Save(SavedImageFile);

            ////    if (ext == ".png" || ext == ".jpeg" || ext == ".jpg")
            ////        mmFileUtils.OptimizeImage(sd.FileName); // async
            ////}
            ////catch (Exception ex)
            ////{
            ////    Cancelled = true;
            ////    StatusBar.ShowStatusError("Error saving image: " + ex.Message);
            ////    return;
            ////}

            if (!string.IsNullOrEmpty(ResultFilePath))
            {
                File.WriteAllText(ResultFilePath, SavedImageFile);
            }

            Cancelled = false;

            if (AutoClose)
            {
                Close();
            }

            ExternalWindow?.Show();
            ExternalWindow?.Activate();

            if (WindowState != WindowState.Maximized && WindowState != WindowState.Minimized)
            {
                ScreenCaptureConfiguration.Current.WindowWidth  = this.Width;
                ScreenCaptureConfiguration.Current.WindowHeight = this.Width;
            }
        }