Esempio n. 1
0
        /// <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;
        }
Esempio n. 2
0
        /// <summary>
        /// Captures a screenshot of a window using Windows GDI
        /// </summary>
        /// <param name="handle">handle of the window to capture</param>
        /// <returns>the captured window image</returns>
        public static Image CaptureWithGDI(IntPtr handle)
        {
            FileSystem.AppendDebug("Capturing with GDI");
            Rectangle windowRect;

            if (Engine.conf.ActiveWindowTryCaptureChildren)
            {
                windowRect = new WindowRectangle(handle).CalculateWindowRectangle();
            }
            else
            {
                windowRect = NativeMethods.GetWindowRectangle(handle);
            }

            windowRect = NativeMethods.MaximizedWindowFix(handle, windowRect);

            FileSystem.AppendDebug("Window rectangle: " + windowRect.ToString());

            Image windowImage = null;

            if (Engine.conf.ActiveWindowClearBackground)
            {
                windowImage = CaptureWindowWithTransparencyGDI(handle, windowRect);
            }

            if (windowImage == null)
            {
                using (new Freeze(handle))
                {
                    windowImage = CaptureRectangle(windowRect);
                }

                if (Engine.conf.ActiveWindowCleanTransparentCorners)
                {
                    Image result = RemoveCorners(handle, windowImage, null, windowRect);
                    if (result != null)
                    {
                        windowImage = result;
                    }
                }

                if (Engine.conf.ActiveWindowIncludeShadows)
                {
                    // Draw shadow manually to be able to have shadows in every case
                    windowImage = GraphicsMgr.AddBorderShadow((Bitmap)windowImage, true);
                }

                if (Engine.conf.ShowCursor)
                {
                    DrawCursor(windowImage, windowRect.Location);
                }
            }

            return windowImage;
        }