public static Image CaptureRectangle(Rectangle rect) { if (RemoveOutsideScreenArea) { Rectangle bounds = CaptureHelpers.GetScreenBounds(); rect = Rectangle.Intersect(bounds, rect); } Image img = CaptureRectangleNative(rect); if (DrawCursor) { Point cursorOffset = CaptureHelpers.FixScreenCoordinates(rect.Location); CaptureHelpers.DrawCursorToImage(img, cursorOffset); } return(img); }
/// <summary>Captures a screenshot of a window using the Windows DWM</summary> /// <param name="handle">handle of the window to capture</param> /// <returns>the captured window image with or without cursor</returns> public static Image CaptureWithDWM(Workflow wfdwm, IntPtr handle) { DebugHelper.WriteLine("Capturing with DWM"); Image windowImageDwm = null; Bitmap redBGImage = null; Rectangle windowRect = CaptureHelpers.GetWindowRectangle(handle); if (windowRect.Width == 0) { System.Threading.Thread.Sleep(250); windowRect = CaptureHelpers.GetWindowRectangle(handle); // try again } if (windowRect.Width > 0 && NativeMethods.IsDWMEnabled()) { if (wfdwm.ActiveWindowDwmUseCustomBackground) { windowImageDwm = CaptureWindowWithDWM(handle, windowRect, out redBGImage, wfdwm.ActiveWindowDwmBackColor); } else if (wfdwm.ActiveWindowClearBackground) { windowImageDwm = CaptureWindowWithDWM(handle, windowRect, out redBGImage, Color.White); } } if (windowImageDwm == null) { DebugHelper.WriteLine("Standard capture (no transparency)"); windowImageDwm = Screenshot.CaptureRectangleNative(windowRect); } Image result = RemoveCorners(handle, windowImageDwm, redBGImage, windowRect); if (result != null) { windowImageDwm = result; } if (wfdwm.ActiveWindowIncludeShadows) { // Draw shadow manually to be able to have shadows in every case windowImageDwm = HelpersLib.GraphicsHelper.Core.AddBorderShadow((Bitmap)windowImageDwm, true); if (wfdwm.DrawCursor) { Point shadowOffset = HelpersLib.GraphicsHelper.Core.ShadowOffset; #if DEBUG DebugHelper.WriteLine("Fixed cursor position (before): " + windowRect.ToString()); #endif windowRect.X -= shadowOffset.X; windowRect.Y -= shadowOffset.Y; #if DEBUG DebugHelper.WriteLine("Fixed cursor position (after): " + windowRect.ToString()); #endif } } if (wfdwm.DrawCursor) { CaptureHelpers.DrawCursorToImage(windowImageDwm, windowRect.Location); } return(windowImageDwm); }
public static Image CaptureWindowTransparent(IntPtr handle) { if (handle.ToInt32() > 0) { Rectangle rect = CaptureHelpers.GetWindowRectangle(handle); Bitmap whiteBackground = null, blackBackground = null, whiteBackground2 = null; MyCursor cursor = null; bool isTransparent = false; try { if (DrawCursor) { cursor = NativeMethods.CaptureCursor(); } using (Form form = new Form()) { form.BackColor = Color.White; form.FormBorderStyle = FormBorderStyle.None; form.ShowInTaskbar = false; if (CaptureShadow && !NativeMethods.IsZoomed(handle) && NativeMethods.IsDWMEnabled()) { const int offset = 20; rect.Inflate(offset, offset); rect.Intersect(CaptureHelpers.GetScreenBounds()); } NativeMethods.ShowWindow(form.Handle, (int)WindowShowStyle.ShowNormalNoActivate); if (!NativeMethods.SetWindowPos(form.Handle, handle, rect.X, rect.Y, rect.Width, rect.Height, NativeMethods.SWP_NOACTIVATE)) { form.Close(); DebugHelper.WriteLine("Transparent capture failed. Reason: SetWindowPos fail."); return(CaptureWindow(handle)); } Thread.Sleep(10); Application.DoEvents(); whiteBackground = (Bitmap)Screenshot.CaptureRectangleNative(rect); form.BackColor = Color.Black; Application.DoEvents(); blackBackground = (Bitmap)Screenshot.CaptureRectangleNative(rect); form.BackColor = Color.White; Application.DoEvents(); whiteBackground2 = (Bitmap)Screenshot.CaptureRectangleNative(rect); form.Close(); } Bitmap transparentImage; if (IsImagesEqual(whiteBackground, whiteBackground2)) { transparentImage = CreateTransparentImage(whiteBackground, blackBackground); isTransparent = true; } else { DebugHelper.WriteLine("Transparent capture failed. Reason: Images not equal."); transparentImage = whiteBackground2; } if (cursor != null) { Point cursorOffset = CaptureHelpers.FixScreenCoordinates(rect.Location); CaptureHelpers.DrawCursorToImage(cursor, transparentImage, cursorOffset); } if (isTransparent) { transparentImage = TrimTransparent(transparentImage); if (!CaptureShadow) { TrimShadow(transparentImage); } } return(transparentImage); } finally { if (whiteBackground != null) { whiteBackground.Dispose(); } if (blackBackground != null) { blackBackground.Dispose(); } if (isTransparent && whiteBackground2 != null) { whiteBackground2.Dispose(); } if (cursor != null) { cursor.Dispose(); } } } return(null); }