/// <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);
        }
Esempio n. 2
0
        /// <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;
        }
        /// <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;
        }