/// <summary> /// Get an ICapture object, so the plugin can modify this /// </summary> /// <returns></returns> public ICapture GetCapture(Image imageToCapture) { Capture capture = new Capture(imageToCapture); capture.CaptureDetails = new CaptureDetails(); capture.CaptureDetails.CaptureMode = CaptureMode.Import; capture.CaptureDetails.Title = "Imported"; return capture; }
/// <summary> /// This method will use User32 code to capture the specified captureBounds from the screen /// </summary> /// <param name="capture">ICapture where the captured Bitmap will be stored</param> /// <param name="captureBounds">Rectangle with the bounds to capture</param> /// <returns>A Capture Object with a part of the Screen as an Image</returns> public static ICapture CaptureRectangle(ICapture capture, Rectangle captureBounds) { if (capture == null) { capture = new Capture(); } capture.Image = CaptureRectangle(captureBounds); capture.Location = captureBounds.Location; if (capture.CaptureDetails != null) { ((Bitmap)capture.Image).SetResolution(capture.CaptureDetails.DpiX, capture.CaptureDetails.DpiY); } if (capture.Image == null) { return null; } return capture; }
void Contextmenu_window_Click(object sender, EventArgs e) { ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender; try { WindowDetails windowToCapture = (WindowDetails)clickedItem.Tag; ICapture capture = new Capture(); using (Graphics graphics = Graphics.FromHwnd(this.Handle)) { capture.CaptureDetails.DpiX = graphics.DpiY; capture.CaptureDetails.DpiY = graphics.DpiY; } windowToCapture.Restore(); windowToCapture = CaptureForm.SelectCaptureWindow(windowToCapture); if (windowToCapture != null) { capture = CaptureForm.CaptureWindow(windowToCapture, capture, coreConf.WindowCaptureMode); this.Activate(); WindowDetails.ToForeground(this.Handle); if (capture != null && capture.Image != null) { surface.AddBitmapContainer((Bitmap)capture.Image, 100, 100); } } if (capture != null) { capture.Dispose(); } } catch (Exception exception) { LOG.Error(exception); } }
public static Image AnnotateImage(Image img, bool allowSave, string configPath, Action<Image> clipboardCopyRequested, Action<Image> imageUploadRequested) { if (!IniConfig.isInitialized) { IniConfig.AllowSave = allowSave; IniConfig.Init(configPath); } using (Image cloneImage = (Image)img.Clone()) using (ICapture capture = new Capture { Image = cloneImage }) using (Surface surface = new Surface(capture)) using (ImageEditorForm editor = new ImageEditorForm(surface, true)) { editor.ClipboardCopyRequested += clipboardCopyRequested; editor.ImageUploadRequested += imageUploadRequested; if (editor.ShowDialog() == DialogResult.OK) { using (img) { return editor.GetImageForExport(); } } } return img; }
/// <summary> /// Capture the supplied Window /// </summary> /// <param name="windowToCapture">Window to capture</param> /// <param name="captureForWindow">The capture to store the details</param> /// <param name="windowCaptureMode">What WindowCaptureMode to use</param> /// <returns></returns> public static ICapture CaptureWindow(WindowDetails windowToCapture, ICapture captureForWindow, WindowCaptureMode windowCaptureMode) { if (captureForWindow == null) { captureForWindow = new Capture(); } Rectangle windowRectangle = windowToCapture.WindowRectangle; // When Vista & DWM (Aero) enabled bool dwmEnabled = DWM.isDWMEnabled(); // get process name to be able to exclude certain processes from certain capture modes using (Process process = windowToCapture.Process) { bool isAutoMode = windowCaptureMode == WindowCaptureMode.Auto; // For WindowCaptureMode.Auto we check: // 1) Is window IE, use IE Capture // 2) Is Windows >= Vista & DWM enabled: use DWM // 3) Otherwise use GDI (Screen might be also okay but might lose content) if (isAutoMode) { if (conf.IECapture && IECaptureHelper.IsIEWindow(windowToCapture)) { try { ICapture ieCapture = IECaptureHelper.CaptureIE(captureForWindow, windowToCapture); if (ieCapture != null) { return ieCapture; } } catch (Exception ex) { LOG.WarnFormat("Problem capturing IE, skipping to normal capture. Exception message was: {0}", ex.Message); } } // Take default screen windowCaptureMode = WindowCaptureMode.Screen; // Change to GDI, if allowed if (!windowToCapture.isMetroApp && WindowCapture.IsGdiAllowed(process)) { if (!dwmEnabled && isWPF(process)) { // do not use GDI, as DWM is not enabled and the application uses PresentationFramework.dll -> isWPF LOG.InfoFormat("Not using GDI for windows of process {0}, as the process uses WPF", process.ProcessName); } else { windowCaptureMode = WindowCaptureMode.GDI; } } // Change to DWM, if enabled and allowed if (dwmEnabled) { if (windowToCapture.isMetroApp || WindowCapture.IsDwmAllowed(process)) { windowCaptureMode = WindowCaptureMode.Aero; } } } else if (windowCaptureMode == WindowCaptureMode.Aero || windowCaptureMode == WindowCaptureMode.AeroTransparent) { if (!dwmEnabled || (!windowToCapture.isMetroApp && !WindowCapture.IsDwmAllowed(process))) { // Take default screen windowCaptureMode = WindowCaptureMode.Screen; // Change to GDI, if allowed if (WindowCapture.IsGdiAllowed(process)) { windowCaptureMode = WindowCaptureMode.GDI; } } } else if (windowCaptureMode == WindowCaptureMode.GDI && !WindowCapture.IsGdiAllowed(process)) { // GDI not allowed, take screen windowCaptureMode = WindowCaptureMode.Screen; } LOG.InfoFormat("Capturing window with mode {0}", windowCaptureMode); bool captureTaken = false; windowRectangle.Intersect(captureForWindow.ScreenBounds); // Try to capture while (!captureTaken) { ICapture tmpCapture = null; switch (windowCaptureMode) { case WindowCaptureMode.GDI: if (WindowCapture.IsGdiAllowed(process)) { if (windowToCapture.Iconic) { // Restore the window making sure it's visible! windowToCapture.Restore(); } else { windowToCapture.ToForeground(); } tmpCapture = windowToCapture.CaptureGDIWindow(captureForWindow); if (tmpCapture != null) { // check if GDI capture any good, by comparing it with the screen content int blackCountGDI = ImageHelper.CountColor(tmpCapture.Image, Color.Black, false); int GDIPixels = tmpCapture.Image.Width * tmpCapture.Image.Height; int blackPercentageGDI = (blackCountGDI * 100) / GDIPixels; if (blackPercentageGDI >= 1) { int screenPixels = windowRectangle.Width * windowRectangle.Height; using (ICapture screenCapture = new Capture()) { screenCapture.CaptureDetails = captureForWindow.CaptureDetails; if (WindowCapture.CaptureRectangleFromDesktopScreen(screenCapture, windowRectangle) != null) { int blackCountScreen = ImageHelper.CountColor(screenCapture.Image, Color.Black, false); int blackPercentageScreen = (blackCountScreen * 100) / screenPixels; if (screenPixels == GDIPixels) { // "easy compare", both have the same size // If GDI has more black, use the screen capture. if (blackPercentageGDI > blackPercentageScreen) { LOG.Debug("Using screen capture, as GDI had additional black."); // changeing the image will automatically dispose the previous tmpCapture.Image = screenCapture.Image; // Make sure it's not disposed, else the picture is gone! screenCapture.NullImage(); } } else if (screenPixels < GDIPixels) { // Screen capture is cropped, window is outside of screen if (blackPercentageGDI > 50 && blackPercentageGDI > blackPercentageScreen) { LOG.Debug("Using screen capture, as GDI had additional black."); // changeing the image will automatically dispose the previous tmpCapture.Image = screenCapture.Image; // Make sure it's not disposed, else the picture is gone! screenCapture.NullImage(); } } else { // Use the GDI capture by doing nothing LOG.Debug("This should not happen, how can there be more screen as GDI pixels?"); } } } } } } if (tmpCapture != null) { captureForWindow = tmpCapture; captureTaken = true; } else { // A problem, try Screen windowCaptureMode = WindowCaptureMode.Screen; } break; case WindowCaptureMode.Aero: case WindowCaptureMode.AeroTransparent: if (windowToCapture.isMetroApp || WindowCapture.IsDwmAllowed(process)) { tmpCapture = windowToCapture.CaptureDWMWindow(captureForWindow, windowCaptureMode, isAutoMode); } if (tmpCapture != null) { captureForWindow = tmpCapture; captureTaken = true; } else { // A problem, try GDI windowCaptureMode = WindowCaptureMode.GDI; } break; default: // Screen capture if (windowToCapture.Iconic) { // Restore the window making sure it's visible! windowToCapture.Restore(); } else { windowToCapture.ToForeground(); } try { captureForWindow = WindowCapture.CaptureRectangleFromDesktopScreen(captureForWindow, windowRectangle); captureTaken = true; } catch (Exception e) { LOG.Error("Problem capturing", e); return null; } break; } } } if (captureForWindow != null) { if (windowToCapture != null) { captureForWindow.CaptureDetails.Title = windowToCapture.Text; } } return captureForWindow; }
/// <summary> /// Make Capture with specified destinations /// </summary> /// <param name="mode">CaptureMode</param> /// <param name="captureMouseCursor">bool false if the mouse should not be captured, true if the configuration should be checked</param> /// <param name="captureDestinations">List<CaptureDestination> with destinations</param> public void MakeCapture(CaptureMode mode, bool captureMouseCursor, List<CaptureDestination> captureDestinations) { Capture passingCapture = new Capture(); passingCapture.CaptureDetails.CaptureDestinations = captureDestinations; MakeCapture(mode, captureMouseCursor, passingCapture); }
/// <summary> /// Capture the supplied Window /// </summary> /// <param name="windowToCapture">Window to capture</param> /// <param name="captureForWindow">The capture to store the details</param> /// <param name="windowCaptureMode">What WindowCaptureMode to use</param> /// <returns></returns> public static ICapture CaptureWindow(WindowDetails windowToCapture, ICapture captureForWindow, WindowCaptureMode windowCaptureMode) { if (captureForWindow == null) { captureForWindow = new Capture(); } Rectangle windowRectangle = windowToCapture.WindowRectangle; if (windowToCapture.Iconic) { // Restore the window making sure it's visible! windowToCapture.Restore(); } // When Vista & DWM (Aero) enabled bool dwmEnabled = DWM.isDWMEnabled(); // get process name to be able to exclude certain processes from certain capture modes Process process = windowToCapture.Process; bool isAutoMode = windowCaptureMode == WindowCaptureMode.Auto; // For WindowCaptureMode.Auto we check: // 1) Is window IE, use IE Capture // 2) Is Windows >= Vista & DWM enabled: use DWM // 3) Otherwise use GDI (Screen might be also okay but might lose content) if (isAutoMode) { if (conf.IECapture && windowToCapture.ClassName == "IEFrame") { try { ICapture ieCapture = IECaptureHelper.CaptureIE(captureForWindow); if (ieCapture != null) { return ieCapture; } } catch (Exception ex) { LOG.WarnFormat("Problem capturing IE, skipping to normal capture. Exception message was: {0}", ex.Message); } } // Take default screen windowCaptureMode = WindowCaptureMode.Screen; // Change to GDI, if allowed if (conf.isGDIAllowed(process)) { if (!dwmEnabled && isWPF(process)) { // do not use GDI, as DWM is not enabled and the application uses PresentationFramework.dll -> isWPF LOG.InfoFormat("Not using GDI for windows of process {0}, as the process uses WPF", process.ProcessName); } else { windowCaptureMode = WindowCaptureMode.GDI; } } // Change to DWM, if enabled and allowed if (dwmEnabled) { if (conf.isDWMAllowed(process)) { windowCaptureMode = WindowCaptureMode.Aero; } } } else if (windowCaptureMode == WindowCaptureMode.Aero || windowCaptureMode == WindowCaptureMode.AeroTransparent) { if (!dwmEnabled || !conf.isDWMAllowed(process)) { // Take default screen windowCaptureMode = WindowCaptureMode.Screen; // Change to GDI, if allowed if (conf.isGDIAllowed(process)) { windowCaptureMode = WindowCaptureMode.GDI; } } } else if (windowCaptureMode == WindowCaptureMode.GDI && !conf.isGDIAllowed(process)) { // GDI not allowed, take screen windowCaptureMode = WindowCaptureMode.Screen; } LOG.InfoFormat("Capturing window with mode {0}", windowCaptureMode); bool captureTaken = false; // Try to capture while (!captureTaken) { if (windowCaptureMode == WindowCaptureMode.GDI) { ICapture tmpCapture = null; if (conf.isGDIAllowed(process)) { tmpCapture = windowToCapture.CaptureWindow(captureForWindow); } if (tmpCapture != null) { captureForWindow = tmpCapture; captureTaken = true; } else { // A problem, try Screen windowCaptureMode = WindowCaptureMode.Screen; } } else if (windowCaptureMode == WindowCaptureMode.Aero || windowCaptureMode == WindowCaptureMode.AeroTransparent) { ICapture tmpCapture = null; if (conf.isDWMAllowed(process)) { tmpCapture = windowToCapture.CaptureDWMWindow(captureForWindow, windowCaptureMode, isAutoMode); } if (tmpCapture != null) { captureForWindow = tmpCapture; captureTaken = true; } else { // A problem, try GDI windowCaptureMode = WindowCaptureMode.GDI; } } else { // Screen capture windowRectangle.Intersect(captureForWindow.ScreenBounds); try { captureForWindow = WindowCapture.CaptureRectangle(captureForWindow, windowRectangle); captureTaken = true; } catch (Exception e) { LOG.Error("Problem capturing", e); return null; } } } if (captureForWindow != null && windowToCapture != null) { captureForWindow.CaptureDetails.Title = windowToCapture.Text; ((Bitmap)captureForWindow.Image).SetResolution(captureForWindow.CaptureDetails.DpiX, captureForWindow.CaptureDetails.DpiY); } return captureForWindow; }
/// <summary> /// Make capture of window /// </summary> /// <param name="window">WindowDetails of the window to capture</param> /// <param name="captureMouseCursor">bool false if the mouse should not be captured, true if the configuration should be checked</param> public void MakeCapture(WindowDetails window, CaptureHandler captureHandler) { Capture passingCapture = new Capture(); passingCapture.CaptureDetails.CaptureHandler = captureHandler; selectedCaptureWindow = window; MakeCapture(CaptureMode.ActiveWindow, false, passingCapture); }
/// <summary> /// Make Capture with default destinations /// </summary> /// <param name="mode">CaptureMode</param> /// <param name="captureMouseCursor">bool false if the mouse should not be captured, true if the configuration should be checked</param> public void MakeCapture(CaptureMode mode, bool captureMouseCursor, CaptureHandler captureHandler) { Capture passingCapture = new Capture(); passingCapture.CaptureDetails.CaptureHandler = captureHandler; MakeCapture(mode, captureMouseCursor, passingCapture); }
// This is also an ICapture Interface implementation public void HandleCapture(Capture capture) { this.capture = capture; HandleCapture(); }
/// <summary> /// Make Capture with default destinations /// </summary> /// <param name="mode">CaptureMode</param> /// <param name="captureMouseCursor">bool false if the mouse should not be captured, true if the configuration should be checked</param> public void MakeCapture(CaptureMode mode, bool captureMouseCursor) { Capture passingCapture = new Capture(); MakeCapture(mode, captureMouseCursor, passingCapture); }
/// <summary> /// Process a bitmap like it was captured /// </summary> /// <param name="bitmap">The bitmap to process</param> public void HandleCapture(Bitmap bitmap) { Capture capture = new Capture(bitmap); HandleCapture(capture); }
/// <summary> /// Capture the supplied Window /// </summary> /// <param name="windowToCapture">Window to capture</param> /// <param name="captureForWindow">The capture to store the details</param> /// <param name="windowCaptureMode">What WindowCaptureMode to use</param> /// <returns></returns> public static ICapture CaptureWindow(WindowDetails windowToCapture, ICapture captureForWindow, WindowCaptureMode windowCaptureMode) { if (captureForWindow == null) { captureForWindow = new Capture(); } Rectangle windowRectangle = windowToCapture.ClientRectangle; if (windowToCapture.Iconic) { // Restore the window making sure it's visible! windowToCapture.Restore(); } // When Vista & DWM (Aero) enabled bool dwmEnabled = DWM.isDWMEnabled(); // For WindowCaptureMode.Auto we check: // 1) Is window IE, use IE Capture // 2) Is Windows >= Vista & DWM enabled: use DWM // 3) Otherwise use GDI (Screen might be also okay but might lose content) if (windowCaptureMode == WindowCaptureMode.Auto) { if (conf.IECapture && windowToCapture.ClassName == "IEFrame") { try { ICapture ieCapture = IECaptureHelper.CaptureIE(captureForWindow); if (ieCapture != null) { return ieCapture; } } catch (Exception ex) { LOG.WarnFormat("Problem capturing IE, skipping to normal capture. Exception message was: {0}", ex.Message); } } // Take default GDI windowCaptureMode = WindowCaptureMode.GDI; // Take DWM if enabled if (dwmEnabled) { windowCaptureMode = WindowCaptureMode.Aero; } } else if (windowCaptureMode == WindowCaptureMode.Aero || windowCaptureMode == WindowCaptureMode.AeroTransparent) { if (!dwmEnabled) { windowCaptureMode = WindowCaptureMode.GDI; } } LOG.DebugFormat("Capturing window with mode {0}", windowCaptureMode); switch (windowCaptureMode) { case WindowCaptureMode.GDI: // GDI captureForWindow = windowToCapture.CaptureWindow(captureForWindow); break; case WindowCaptureMode.Aero: case WindowCaptureMode.AeroTransparent: // DWM captureForWindow = windowToCapture.CaptureDWMWindow(captureForWindow, windowCaptureMode); break; case WindowCaptureMode.Screen: default: // Screen capture windowRectangle.Intersect(captureForWindow.ScreenBounds); try { captureForWindow = WindowCapture.CaptureRectangle(captureForWindow, windowRectangle); } catch (Exception e) { LOG.Error("Problem capturing", e); return null; } break; } captureForWindow.CaptureDetails.Title = windowToCapture.Text; ((Bitmap)captureForWindow.Image).SetResolution(captureForWindow.CaptureDetails.DpiX, captureForWindow.CaptureDetails.DpiY); return captureForWindow; }
private void Contextmenu_window_Click(object sender, EventArgs e) { ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender; try { WindowDetails windowToCapture = (WindowDetails)clickedItem.Tag; ICapture capture = new Capture(); using (Graphics graphics = Graphics.FromHwnd(this.Handle)) { capture.CaptureDetails.DpiX = graphics.DpiY; capture.CaptureDetails.DpiY = graphics.DpiY; } windowToCapture.Restore(); windowToCapture = CaptureHelper.SelectCaptureWindow(windowToCapture); if (windowToCapture != null) { capture = CaptureHelper.CaptureWindow(windowToCapture, capture, coreConf.WindowCaptureMode); this.Activate(); WindowDetails.ToForeground(this.Handle); if (capture != null && capture.Image != null) { bool addShadow = false; if (addShadow) { Point offset = new Point(6, 6); using (Bitmap shadowImage = ImageHelper.CreateShadow(capture.Image, 1f, 7, offset, PixelFormat.Format32bppArgb)) { surface.AddBitmapContainer(shadowImage, 100, 100); } } else { surface.AddBitmapContainer((Bitmap)capture.Image, 100, 100); } } } if (capture != null) { capture.Dispose(); } } catch (Exception exception) { LOG.Error(exception); } }
public static Image AnnotateImage(Image img, string imgPath, bool allowSave, string configPath, Action<Image> clipboardCopyRequested, Action<Image> imageUploadRequested, Action<Image, string> imageSaveRequested, Func<Image, string, string> imageSaveAsRequested, Action<Image> printImageRequested) { if (!IniConfig.isInitialized) { IniConfig.AllowSave = allowSave; IniConfig.Init(configPath); } using (Image cloneImage = img != null ? (Image)img.Clone() : LoadImage(imgPath)) using (ICapture capture = new Capture { Image = cloneImage }) using (Surface surface = new Surface(capture)) using (ImageEditorForm editor = new ImageEditorForm(surface, true)) { editor.IsTaskWork = img != null; editor.SetImagePath(imgPath); editor.ClipboardCopyRequested += clipboardCopyRequested; editor.ImageUploadRequested += imageUploadRequested; editor.ImageSaveRequested += imageSaveRequested; editor.ImageSaveAsRequested += imageSaveAsRequested; editor.PrintImageRequested += printImageRequested; DialogResult result = editor.ShowDialog(); if (result == DialogResult.OK && editor.IsTaskWork) { using (img) { return editor.GetImageForExport(); } } if (result == DialogResult.Abort) { return null; } } return img; }
/// <summary> /// Make Capture with file name /// </summary> /// <param name="filename">List<CaptureDestination> with destinations</param> public void MakeCapture(string filename) { Capture passingCapture = new Capture(); passingCapture.CaptureDetails.Filename = filename; MakeCapture(CaptureMode.File, false, passingCapture); }
/// <summary> /// This method will use User32 code to capture the specified captureBounds from the screen /// </summary> /// <param name="capture">ICapture where the captured Bitmap will be stored</param> /// <param name="captureBounds">Rectangle with the bounds to capture</param> /// <returns>A Capture Object with a part of the Screen as an Image</returns> public static ICapture CaptureRectangleFromDesktopScreen(ICapture capture, Rectangle captureBounds) { if (capture == null) { capture = new Capture(); } capture.Image = CaptureRectangle(captureBounds); capture.Location = captureBounds.Location; return capture.Image == null ? null : capture; }
/// <summary> /// Perform Actions after capturing image/text/file objects /// </summary> public void PerformActions() { foreach (Software app in Engine.ConfigUI.ConfigActions.ActionsApps) { if (app.Enabled) { if (IsValidActionImage(app) && app.Name == Engine.zImageAnnotator) { try { // Compatibility fixes string LANGUAGE_PATH = Path.Combine(Application.StartupPath, @"Languages"); if (!Directory.Exists(LANGUAGE_PATH)) Directory.CreateDirectory(LANGUAGE_PATH); if (Greenshot.MainForm.instance == null) Greenshot.MainForm.Start(new string[0]); CoreConfiguration coreConfiguration = IniConfig.GetIniSection<CoreConfiguration>(); coreConfiguration.OutputFileFilenamePattern = "${title}"; coreConfiguration.OutputFilePath = Engine.ImagesDir; ICapture capture = new GreenshotPlugin.Core.Capture(); capture.Image = TempImage; capture.CaptureDetails.Filename = Info.LocalFilePath; capture.CaptureDetails.Title = Path.GetFileNameWithoutExtension(capture.CaptureDetails.Filename); capture.CaptureDetails.AddMetaData("file", capture.CaptureDetails.Filename); capture.CaptureDetails.AddMetaData("source", "file"); var surface = new Greenshot.Drawing.Surface(capture); var editor = new Greenshot.ImageEditorForm(surface, WorkflowConfig.DestConfig.Outputs.Contains( OutputEnum.LocalDisk)) { Icon = Resources.zss_tray }; editor.SetImagePath(Info.LocalFilePath); editor.Visible = false; editor.ShowDialog(); TempImage = editor.GetImageForExport(); } catch (Exception ex) { DebugHelper.WriteException(ex, "ImageEdit"); } } else if (IsValidActionImage(app) && app.Name == Engine.zImageEffects) { var effects = new ImageEffects.ImageEffectsGUI(TempImage); effects.ShowDialog(); TempImage = effects.GetImageForExport(); } else if (File.Exists(app.Path)) { if (IsValidActionOCR(app)) { app.OpenFile(OCRFilePath); OCRText = File.ReadAllText(OCRFilePath); } else if (IsValidActionText(app)) { app.OpenFile(Info.LocalFilePath); TempText = File.ReadAllText(TempText); } else if (IsValidActionImage(app)) { WriteImage(TempImage); app.OpenFile(Info.LocalFilePath); } else if (IsValidActionFile(app)) { app.OpenFile(Info.LocalFilePath); } } DebugHelper.WriteLine(string.Format("Performed Actions using {0}.", app.Name)); } } }
/// <summary> /// This method will call the CaptureRectangle with the screenbounds, therefor Capturing the whole screen. /// </summary> /// <returns>A Capture Object with the Screen as an Image</returns> public static ICapture CaptureScreen(ICapture capture) { if (capture == null) { capture = new Capture(); } return CaptureRectangle(capture, capture.ScreenBounds); }
/// <summary> /// This method will use User32 code to capture the specified captureBounds from the screen /// </summary> /// <param name="capture">ICapture where the captured Bitmap will be stored</param> /// <param name="captureBounds">Rectangle with the bounds to capture</param> /// <returns>A Capture Object with a part of the Screen as an Image</returns> public static ICapture CaptureRectangle(ICapture capture, Rectangle captureBounds) { if (capture == null) { capture = new Capture(); } Image capturedImage = null; // If the CaptureHandler has a handle use this, otherwise use the CaptureRectangle here if (CaptureHandler.CaptureScreenRectangle != null) { try { capturedImage = CaptureHandler.CaptureScreenRectangle(captureBounds); } catch { } } // If no capture, use the normal screen capture if (capturedImage == null) { capturedImage = CaptureRectangle(captureBounds); } capture.Image = capturedImage; capture.Location = captureBounds.Location; return capture.Image == null ? null : capture; }
/// <summary> /// This method will capture the current Cursor by using User32 Code /// </summary> /// <returns>A Capture Object with the Mouse Cursor information in it.</returns> public static ICapture CaptureCursor(ICapture capture) { LOG.Debug("Capturing the mouse cursor."); if (capture == null) { capture = new Capture(); } int x,y; CursorInfo cursorInfo = new CursorInfo(); IconInfo iconInfo; cursorInfo.cbSize = Marshal.SizeOf(cursorInfo); if (User32.GetCursorInfo(out cursorInfo)) { if (cursorInfo.flags == User32.CURSOR_SHOWING) { using (SafeIconHandle safeIcon = User32.CopyIcon(cursorInfo.hCursor)) { if (User32.GetIconInfo(safeIcon, out iconInfo)) { Point cursorLocation = User32.GetCursorLocation(); // Allign cursor location to Bitmap coordinates (instead of Screen coordinates) x = cursorLocation.X - iconInfo.xHotspot - capture.ScreenBounds.X; y = cursorLocation.Y - iconInfo.yHotspot - capture.ScreenBounds.Y; // Set the location capture.CursorLocation = new Point(x, y); using (Icon icon = Icon.FromHandle(safeIcon.DangerousGetHandle())) { capture.Cursor = icon; } if (iconInfo.hbmMask != IntPtr.Zero) { DeleteObject(iconInfo.hbmMask); } if (iconInfo.hbmColor != IntPtr.Zero) { DeleteObject(iconInfo.hbmColor); } } } } } return capture; }