/// <summary> /// Shows an overlay over the screen that allows the user to select a /// region, of which the image is captured and returned. /// </summary> /// <returns>A <see cref="Screenshot"/> with the selected region.</returns> public static Screenshot FromRegion() { try { Screenshot screenshot = new Screenshot(); screenshot.Source = ScreenshotSource.RegionCapture; RegionOverlay overlay = new RegionOverlay(); if (overlay.ShowDialog() == DialogResult.OK) { Rectangle rect = overlay.SelectedRegion; if (rect.Width > 0 && rect.Height > 0) { screenshot.Bitmap = new Bitmap(rect.Width, rect.Height); using (Graphics g = Graphics.FromImage(screenshot.Bitmap)) { g.Clear(ThatFuckingColor); g.CopyFromScreen(rect.X, rect.Y, 0, 0, new Size(rect.Width, rect.Height), CopyPixelOperation.SourceCopy); } return screenshot; } } } catch (Exception ex) { Trace.WriteLine(ex); System.Media.SystemSounds.Exclamation.Play(); } return null; }
/// <summary> /// Creates a new <see cref="Superscrot.Screenshot"/> instance based on /// the specified image file. /// </summary> /// <returns> /// A <see cref="Superscrot.Screenshot"/> based on the specified image file. /// </returns> public static Screenshot FromFile(string path) { try { Screenshot screenshot = new Screenshot(); screenshot.Source = ScreenshotSource.File; screenshot.OriginalFileName = path; using (var stream = new FileStream(path, FileMode.Open)) { using (var image = Image.FromStream(stream)) { screenshot.Bitmap = new Bitmap(image); } } return screenshot; } catch (Exception ex) { Trace.WriteLine(ex); System.Media.SystemSounds.Exclamation.Play(); return null; } }
/// <summary> /// Creates a new <see cref="Superscrot.Screenshot"/> instance based on /// the image data on the clipboard. /// </summary> /// <returns> /// A <see cref="Superscrot.Screenshot"/> based on the clipboard image. /// </returns> /// <exception cref="System.InvalidOperationException"> /// The clipboard is empty or does not contain an image. /// </exception> public static Screenshot FromClipboard() { if (!Clipboard.ContainsImage()) throw new InvalidOperationException(SR.NoClipboardImage); try { Screenshot screenshot = new Screenshot(); screenshot.Source = ScreenshotSource.Clipboard; screenshot.Bitmap = (Bitmap)Clipboard.GetImage(); return screenshot; } catch (Exception ex) { Trace.WriteLine(ex); System.Media.SystemSounds.Exclamation.Play(); return null; } }
/// <summary> /// Retrieves an image containing a screenshot of the user's entire desktop. /// </summary> /// <returns> /// A new <see cref="Screenshot"/> with an image containg a screenshot /// of all screens combined. /// </returns> public static Screenshot FromDesktop() { try { var screenshot = new Screenshot(); screenshot.Source = ScreenshotSource.Desktop; var bounds = GetDesktopBounds(); screenshot.Bitmap = new Bitmap(bounds.Width, bounds.Height); using (Graphics g = Graphics.FromImage(screenshot.Bitmap)) { foreach (var screen in Screen.AllScreens) { var destination = new Point( screen.Bounds.X + Math.Abs(bounds.Left), screen.Bounds.Y + Math.Abs(bounds.Top)); using (var screenBitmap = CopyFromScreen(screen)) { g.DrawImageUnscaled(screenBitmap, destination); } } } return screenshot; } catch (Exception ex) { Trace.WriteLine(ex); System.Media.SystemSounds.Exclamation.Play(); } return null; }
/// <summary> /// Retrieves an image with the contents of the active window. /// </summary> /// <returns>A <see cref="Screenshot"/> with the active window capture.</returns> public static Screenshot FromActiveWindow() { try { Screenshot screenshot = new Screenshot(); screenshot.Source = ScreenshotSource.WindowCapture; using (var window = NativeWindow.ForegroundWindow()) { screenshot.WindowTitle = window.Caption; screenshot.WindowOwner = window.ToString(); screenshot.Bitmap = new Bitmap(window.Width, window.Height); using (Graphics g = Graphics.FromImage(screenshot.Bitmap)) { g.Clear(ThatFuckingColor); g.CopyFromScreen(window.Location, Point.Empty, window.Size, CopyPixelOperation.SourceCopy); } return screenshot; } } catch (Exception ex) { Trace.WriteLine(ex); System.Media.SystemSounds.Exclamation.Play(); } return null; }