public virtual Image GetRegionImage() { if (regionFillPath != null) { Image img; Rectangle regionArea = Rectangle.Round(regionFillPath.GetBounds()); Rectangle newRegionArea = Rectangle.Intersect(regionArea, ScreenRectangle0Based); using (GraphicsPath gp = (GraphicsPath)regionFillPath.Clone()) { MoveGraphicsPath(gp, -Math.Max(0, regionArea.X), -Math.Max(0, regionArea.Y)); img = CaptureHelpers.CropImage(SurfaceImage, newRegionArea, gp); if (Config.DrawBorder) { GraphicsPath gpOutline; if (regionDrawPath != null) { gpOutline = regionDrawPath; } else { gpOutline = regionFillPath; } using (GraphicsPath gp2 = (GraphicsPath)gpOutline.Clone()) { MoveGraphicsPath(gp2, -Math.Max(0, regionArea.X), -Math.Max(0, regionArea.Y)); img = CaptureHelpers.DrawOutline(img, gp2); } } } if (Config.DrawChecker) { img = CaptureHelpers.DrawCheckers(img); } return(img); } return(null); }
/// <summary>Captures a screenshot of a window using Windows GDI. Captures transparency.</summary> /// <param name="handle">handle of the window to capture</param> /// <returns>the captured window image</returns> private static Image CaptureWindowWithGDI(Workflow wfgdi, IntPtr handle, out Rectangle windowRect) { Image windowImageGdi = null; Bitmap whiteBGImage = null, blackBGImage = null, white2BGImage = null; if (wfgdi.ActiveWindowTryCaptureChildren) { windowRect = new WindowRectangle(handle).CalculateWindowRectangle(); } else { windowRect = CaptureHelpers.GetWindowRectangle(handle); } try { using (new Freeze(wfgdi, handle)) using (Form form = new Form()) { form.BackColor = Color.White; form.FormBorderStyle = FormBorderStyle.None; form.ShowInTaskbar = false; int offset = wfgdi.ActiveWindowIncludeShadows && !NativeMethods.IsWindowMaximized(handle) ? 20 : 0; windowRect.Inflate(offset, offset); windowRect.Intersect(CaptureHelpers.GetScreenBounds()); NativeMethods.ShowWindow(form.Handle, (int)WindowShowStyle.ShowNormalNoActivate); NativeMethods.SetWindowPos(form.Handle, handle, windowRect.X, windowRect.Y, windowRect.Width, windowRect.Height, NativeMethods.SWP_NOACTIVATE); Application.DoEvents(); whiteBGImage = (Bitmap)Screenshot.CaptureRectangleNative2(windowRect); form.BackColor = Color.Black; Application.DoEvents(); blackBGImage = (Bitmap)Screenshot.CaptureRectangleNative2(windowRect); if (!wfgdi.ActiveWindowGDIFreezeWindow) { form.BackColor = Color.White; Application.DoEvents(); white2BGImage = (Bitmap)Screenshot.CaptureRectangleNative2(windowRect); } } if (wfgdi.ActiveWindowGDIFreezeWindow || whiteBGImage.AreBitmapsEqual(white2BGImage)) { windowImageGdi = HelpersLib.GraphicsHelper.Core.ComputeOriginal(whiteBGImage, blackBGImage); } else { windowImageGdi = (Image)whiteBGImage.Clone(); } } finally { if (whiteBGImage != null) { whiteBGImage.Dispose(); } if (blackBGImage != null) { blackBGImage.Dispose(); } if (white2BGImage != null) { white2BGImage.Dispose(); } } if (windowImageGdi != null) { Rectangle windowRectCropped = HelpersLib.GraphicsHelper.Core.GetCroppedArea((Bitmap)windowImageGdi); windowImageGdi = CaptureHelpers.CropImage(windowImageGdi, windowRectCropped); if (wfgdi.DrawCursor) { #if DEBUG DebugHelper.WriteLine("Fixed cursor position (before): " + windowRect.ToString()); #endif windowRect.X += windowRectCropped.X; windowRect.Y += windowRectCropped.Y; #if DEBUG DebugHelper.WriteLine("Fixed cursor position (after): " + windowRect.ToString()); #endif } } return(windowImageGdi); }