Beispiel #1
0
        private bool CaptureActiveWindow()
        {
            bool presupplied = false;

            Log.Debug("CaptureActiveWindow");
            if (_selectedCaptureWindow != null)
            {
                Log.Debug("Using supplied window");
                presupplied = true;
            }
            else
            {
                _selectedCaptureWindow = WindowDetails.GetActiveWindow();
                if (_selectedCaptureWindow != null)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("Capturing window: {0} with {1}", _selectedCaptureWindow.Text, _selectedCaptureWindow.WindowRectangle);
                    }
                }
            }
            if (_selectedCaptureWindow == null || (!presupplied && _selectedCaptureWindow.Iconic))
            {
                Log.Warn("No window to capture!");
                // Nothing to capture, code up in the stack will capture the full screen
                return(false);
            }
            if (!presupplied && _selectedCaptureWindow != null && _selectedCaptureWindow.Iconic)
            {
                // Restore the window making sure it's visible!
                // This is done mainly for a screen capture, but some applications like Excel and TOAD have weird behaviour!
                _selectedCaptureWindow.Restore();
            }
            _selectedCaptureWindow = SelectCaptureWindow(_selectedCaptureWindow);
            if (_selectedCaptureWindow == null)
            {
                Log.Warn("No window to capture, after SelectCaptureWindow!");
                // Nothing to capture, code up in the stack will capture the full screen
                return(false);
            }
            // Fix for Bug #3430560
            CoreConfig.LastCapturedRegion = _selectedCaptureWindow.WindowRectangle;
            bool returnValue = CaptureWindow(_selectedCaptureWindow, _capture, CoreConfig.WindowCaptureMode) != null;

            return(returnValue);
        }
Beispiel #2
0
        private void SetDpi()
        {
            // Workaround for proble with DPI retrieval, the FromHwnd activates the window...
            WindowDetails previouslyActiveWindow = WindowDetails.GetActiveWindow();

            // Workaround for changed DPI settings in Windows 7
            using (Graphics graphics = Graphics.FromHwnd(MainForm.Instance.Handle)) {
                _capture.CaptureDetails.DpiX = graphics.DpiX;
                _capture.CaptureDetails.DpiY = graphics.DpiY;
            }
            // Set previouslyActiveWindow as foreground window
            previouslyActiveWindow?.ToForeground(false);
            if (_capture.CaptureDetails != null)
            {
                ((Bitmap)_capture.Image)?.SetResolution(_capture.CaptureDetails.DpiX, _capture.CaptureDetails.DpiY);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Here the logic for capturing the IE Content is located
        /// </summary>
        /// <param name="capture">ICapture where the capture needs to be stored</param>
        /// <param name="windowToCapture">window to use</param>
        /// <returns>ICapture with the content (if any)</returns>
        public static ICapture CaptureIE(ICapture capture, WindowDetails windowToCapture)
        {
            if (windowToCapture == null)
            {
                windowToCapture = WindowDetails.GetActiveWindow();
            }
            // Show backgroundform after retrieving the active window..
            BackgroundForm backgroundForm = new BackgroundForm(Language.GetString(LangKey.contextmenu_captureie), Language.GetString(LangKey.wait_ie_capture));

            backgroundForm.Show();
            //BackgroundForm backgroundForm = BackgroundForm.ShowAndWait(language.GetString(LangKey.contextmenu_captureie), language.GetString(LangKey.wait_ie_capture));
            try {
                //Get IHTMLDocument2 for the current active window
                DocumentContainer documentContainer = CreateDocumentContainer(windowToCapture);

                // Nothing found
                if (documentContainer == null)
                {
                    LOG.Debug("Nothing to capture found");
                    return(null);
                }

                try {
                    LOG.DebugFormat("Window class {0}", documentContainer.ContentWindow.ClassName);
                    LOG.DebugFormat("Window location {0}", documentContainer.ContentWindow.Location);
                } catch (Exception ex) {
                    LOG.Warn("Error while logging information.", ex);
                }

                // bitmap to return
                Bitmap returnBitmap = null;
                Size   pageSize     = Size.Empty;
                try {
                    pageSize     = PrepareCapture(documentContainer, capture);
                    returnBitmap = capturePage(documentContainer, capture, pageSize);
                } catch (Exception captureException) {
                    LOG.Error("Exception found, ignoring and returning nothing! Error was: ", captureException);
                }
                // TODO: Enable when the elements are usable again.
                // Capture the element on the page
                //try {
                //    if (configuration.IEFieldCapture && capture.CaptureDetails.HasDestination("Editor")) {
                //        // clear the current elements, as they are for the window itself
                //        capture.Elements.Clear();
                //        CaptureElement documentCaptureElement = documentContainer.CreateCaptureElements(pageSize);
                //        foreach(DocumentContainer frameDocument in documentContainer.Frames) {
                //            try {
                //                CaptureElement frameCaptureElement = frameDocument.CreateCaptureElements(Size.Empty);
                //                if (frameCaptureElement != null) {
                //                    documentCaptureElement.Children.Add(frameCaptureElement);
                //                }
                //            } catch (Exception ex) {
                //                LOG.Warn("An error occurred while creating the capture elements: ", ex);
                //            }
                //        }
                //        capture.AddElement(documentCaptureElement);
                //        // Offset the elements, as they are "back offseted" later...
                //        Point windowLocation = documentContainer.ContentWindow.WindowRectangle.Location;
                //        capture.MoveElements(-(capture.ScreenBounds.Location.X-windowLocation.X), -(capture.ScreenBounds.Location.Y-windowLocation.Y));
                //    }
                //} catch (Exception elementsException) {
                //    LOG.Warn("An error occurred while creating the capture elements: ", elementsException);
                //}


                if (returnBitmap == null)
                {
                    return(null);
                }

                // Store the bitmap for further processing
                capture.Image = returnBitmap;
                try {
                    // Store the location of the window
                    capture.Location = documentContainer.ContentWindow.Location;

                    // The URL is available unter "document2.url" and can be used to enhance the meta-data etc.
                    capture.CaptureDetails.AddMetaData("url", documentContainer.Url);
                    // Store the title of the page
                    if (documentContainer.Name != null)
                    {
                        capture.CaptureDetails.Title = documentContainer.Name;
                    }
                    else
                    {
                        capture.CaptureDetails.Title = windowToCapture.Text;
                    }
                } catch (Exception ex) {
                    LOG.Warn("Problems getting some attributes...", ex);
                }

                // Store the URL of the page
                if (documentContainer.Url != null)
                {
                    try {
                        Uri uri = new Uri(documentContainer.Url);
                        capture.CaptureDetails.AddMetaData("URL", uri.OriginalString);
                        // As the URL can hardly be used in a filename, the following can be used
                        if (!string.IsNullOrEmpty(uri.Scheme))
                        {
                            capture.CaptureDetails.AddMetaData("URL_SCHEME", uri.Scheme);
                        }
                        if (!string.IsNullOrEmpty(uri.DnsSafeHost))
                        {
                            capture.CaptureDetails.AddMetaData("URL_HOSTNAME", uri.DnsSafeHost);
                        }
                        if (!string.IsNullOrEmpty(uri.AbsolutePath))
                        {
                            capture.CaptureDetails.AddMetaData("URL_PATH", uri.AbsolutePath);
                        }
                        if (!string.IsNullOrEmpty(uri.Query))
                        {
                            capture.CaptureDetails.AddMetaData("URL_QUERY", uri.Query);
                        }
                        if (!string.IsNullOrEmpty(uri.UserInfo))
                        {
                            capture.CaptureDetails.AddMetaData("URL_USER", uri.UserInfo);
                        }
                        capture.CaptureDetails.AddMetaData("URL_PORT", uri.Port.ToString());
                    } catch (Exception e) {
                        LOG.Warn("Exception when trying to use url in metadata " + documentContainer.Url, e);
                    }
                }
                try {
                    // Only move the mouse to correct for the capture offset
                    capture.MoveMouseLocation(-documentContainer.ViewportRectangle.X, -documentContainer.ViewportRectangle.Y);
                    // Used to be: capture.MoveMouseLocation(-(capture.Location.X + documentContainer.CaptureOffset.X), -(capture.Location.Y + documentContainer.CaptureOffset.Y));
                } catch (Exception ex) {
                    LOG.Warn("Error while correcting the mouse offset.", ex);
                }
            } finally {
                // Always close the background form
                backgroundForm.CloseDialog();
            }
            return(capture);
        }
Beispiel #4
0
 /// <summary>
 /// Here the logic for capturing the IE Content is located
 /// </summary>
 /// <param name="capture">ICapture where the capture needs to be stored</param>
 /// <returns>ICapture with the content (if any)</returns>
 public static ICapture CaptureIE(ICapture capture)
 {
     return(CaptureIE(capture, WindowDetails.GetActiveWindow()));
 }
        /// <summary>
        /// Here the logic for capturing the IE Content is located
        /// </summary>
        /// <param name="capture">ICapture where the capture needs to be stored</param>
        /// <returns>ICapture with the content (if any)</returns>
        public static ICapture CaptureIE(ICapture capture)
        {
            WindowDetails activeWindow = WindowDetails.GetActiveWindow();

            // Show backgroundform after retrieving the active window..
            BackgroundForm backgroundForm = new BackgroundForm(language.GetString(LangKey.contextmenu_captureie), language.GetString(LangKey.wait_ie_capture));

            backgroundForm.Show();
            //BackgroundForm backgroundForm = BackgroundForm.ShowAndWait(language.GetString(LangKey.contextmenu_captureie), language.GetString(LangKey.wait_ie_capture));
            try {
                //Get IHTMLDocument2 for the current active window
                DocumentContainer documentContainer = GetDocument(activeWindow);

                // Nothing found
                if (documentContainer == null)
                {
                    LOG.Debug("Nothing to capture found");
                    return(null);
                }
                LOG.DebugFormat("Window class {0}", documentContainer.ContentWindow.ClassName);
                LOG.DebugFormat("Window location {0}", documentContainer.ContentWindow.Location);

                // The URL is available unter "document2.url" and can be used to enhance the meta-data etc.
                capture.CaptureDetails.AddMetaData("url", documentContainer.Url);

                // bitmap to return
                Bitmap returnBitmap = null;
                try {
                    returnBitmap = capturePage(documentContainer, capture);
                } catch (Exception e) {
                    LOG.Error("Exception found, ignoring and returning nothing! Error was: ", e);
                }

                if (returnBitmap == null)
                {
                    return(null);
                }

                // Store the bitmap for further processing
                capture.Image = returnBitmap;
                // Store the location of the window
                capture.Location = documentContainer.ContentWindow.Location;
                // Store the title of the Page
                if (documentContainer.Name != null)
                {
                    capture.CaptureDetails.Title = documentContainer.Name;
                }
                else
                {
                    capture.CaptureDetails.Title = activeWindow.Text;
                }

                // Only move the mouse to correct for the capture offset
                capture.MoveMouseLocation(-documentContainer.ViewportRectangle.X, -documentContainer.ViewportRectangle.Y);
                // Used to be: capture.MoveMouseLocation(-(capture.Location.X + documentContainer.CaptureOffset.X), -(capture.Location.Y + documentContainer.CaptureOffset.Y));
            } finally {
                // Always close the background form
                backgroundForm.CloseDialog();
            }
            return(capture);
        }
        /// <summary>
        /// Make Capture with specified destinations
        /// </summary>
        private void MakeCapture()
        {
            // Experimental code
            if (screenCapture != null)
            {
                screenCapture.Stop();
                screenCapture = null;
                return;
            }

            LOG.Debug(String.Format("Capturing with mode {0} and using Cursor {1}", captureMode, captureMouseCursor));
            capture.CaptureDetails.CaptureMode = captureMode;

            // Get the windows details in a seperate thread
            windowDetailsThread = PrepareForCaptureWithFeedback();

            // Add destinations if no-one passed a handler
            if (capture.CaptureDetails.CaptureDestinations == null || capture.CaptureDetails.CaptureDestinations.Count == 0)
            {
                AddConfiguredDestination();
            }

            // Workaround for proble with DPI retrieval, the FromHwnd activates the window...
            WindowDetails previouslyActiveWindow = WindowDetails.GetActiveWindow();

            // Workaround for changed DPI settings in Windows 7
            using (Graphics graphics = Graphics.FromHwnd(MainForm.instance.Handle))
            {
                capture.CaptureDetails.DpiX = graphics.DpiX;
                capture.CaptureDetails.DpiY = graphics.DpiY;
            }
            if (previouslyActiveWindow != null)
            {
                // Set previouslyActiveWindow as foreground window
                previouslyActiveWindow.ToForeground();
            }

            // Delay for the Context menu
            if (conf.CaptureDelay > 0)
            {
                System.Threading.Thread.Sleep(conf.CaptureDelay);
            }
            else
            {
                conf.CaptureDelay = 0;
            }

            // Allways capture Mousecursor, only show when needed
            capture = WindowCapture.CaptureCursor(capture);
            capture.CursorVisible = false;
            // Check if needed
            if (captureMouseCursor && captureMode != CaptureMode.Clipboard && captureMode != CaptureMode.File)
            {
                capture.CursorVisible = conf.CaptureMousepointer;
            }

            switch (captureMode)
            {
            case CaptureMode.Window:
                capture = WindowCapture.CaptureScreen(capture);
                capture.CaptureDetails.AddMetaData("source", "Screen");
                CaptureWithFeedback();
                break;

            case CaptureMode.ActiveWindow:
                if (CaptureActiveWindow())
                {
                    if (windowDetailsThread != null)
                    {
                        windowDetailsThread.Join();
                    }
                    // Capture worked, offset mouse according to screen bounds and capture location
                    capture.MoveMouseLocation(capture.ScreenBounds.Location.X - capture.Location.X, capture.ScreenBounds.Location.Y - capture.Location.Y);
                    capture.MoveElements(capture.ScreenBounds.Location.X - capture.Location.X, capture.ScreenBounds.Location.Y - capture.Location.Y);
                    capture.CaptureDetails.AddMetaData("source", "Window");
                }
                else
                {
                    captureMode = CaptureMode.FullScreen;
                    capture     = WindowCapture.CaptureScreen(capture);
                    capture.CaptureDetails.AddMetaData("source", "Screen");
                    capture.CaptureDetails.Title = "Screen";
                }
                HandleCapture();
                break;

            case CaptureMode.IE:
                if (IECaptureHelper.CaptureIE(capture) != null)
                {
                    capture.CaptureDetails.AddMetaData("source", "Internet Explorer");
                    HandleCapture();
                }
                break;

            case CaptureMode.FullScreen:
                // Check how we need to capture the screen
                bool captureTaken = false;
                switch (screenCaptureMode)
                {
                case ScreenCaptureMode.Auto:
                    Point mouseLocation = WindowCapture.GetCursorLocation();
                    foreach (Screen screen in Screen.AllScreens)
                    {
                        if (screen.Bounds.Contains(mouseLocation))
                        {
                            capture      = WindowCapture.CaptureRectangle(capture, screen.Bounds);
                            captureTaken = true;
                            break;
                        }
                    }
                    break;

                case ScreenCaptureMode.Fixed:
                    if (conf.ScreenToCapture > 0 && conf.ScreenToCapture <= Screen.AllScreens.Length)
                    {
                        capture      = WindowCapture.CaptureRectangle(capture, Screen.AllScreens[conf.ScreenToCapture].Bounds);
                        captureTaken = true;
                    }
                    break;

                case ScreenCaptureMode.FullScreen:
                    // Do nothing, we take the fullscreen capture automatically
                    break;
                }
                if (!captureTaken)
                {
                    capture = WindowCapture.CaptureScreen(capture);
                }
                HandleCapture();
                break;

            case CaptureMode.Clipboard:
                Image  clipboardImage = null;
                string text           = "Clipboard";
                if (ClipboardHelper.ContainsImage())
                {
                    clipboardImage = ClipboardHelper.GetImage();
                }
                if (clipboardImage != null)
                {
                    if (capture != null)
                    {
                        capture.Image = clipboardImage;
                    }
                    else
                    {
                        capture = new Capture(clipboardImage);
                    }
                    string title = ClipboardHelper.GetText();
                    if (title == null || title.Trim().Length == 0)
                    {
                        title = "Clipboard";
                    }
                    capture.CaptureDetails.Title = title;
                    capture.CaptureDetails.AddMetaData("source", "Clipboard");
                    // Force Editor, keep picker
                    if (capture.CaptureDetails.HasDestination(Destinations.PickerDestination.DESIGNATION))
                    {
                        capture.CaptureDetails.ClearDestinations();
                        capture.CaptureDetails.AddDestination(DestinationHelper.GetDestination(Destinations.EditorDestination.DESIGNATION));
                        capture.CaptureDetails.AddDestination(DestinationHelper.GetDestination(Destinations.PickerDestination.DESIGNATION));
                    }
                    else
                    {
                        capture.CaptureDetails.ClearDestinations();
                        capture.CaptureDetails.AddDestination(DestinationHelper.GetDestination(Destinations.EditorDestination.DESIGNATION));
                    }
                    HandleCapture();
                }
                else
                {
                    MessageBox.Show("Couldn't create bitmap from : " + text);
                }
                break;

            case CaptureMode.File:
                Bitmap fileBitmap = null;
                string filename   = capture.CaptureDetails.Filename;
                if (!string.IsNullOrEmpty(filename))
                {
                    try
                    {
                        fileBitmap = ImageHelper.LoadBitmap(filename);
                    }
                    catch (Exception e)
                    {
                        LOG.Error(e.Message, e);
                        MessageBox.Show(Language.GetFormattedString(LangKey.error_openfile, filename));
                    }
                }
                if (fileBitmap != null)
                {
                    capture.CaptureDetails.Title = Path.GetFileNameWithoutExtension(filename);
                    capture.CaptureDetails.AddMetaData("file", filename);
                    capture.CaptureDetails.AddMetaData("source", "file");
                    if (capture != null)
                    {
                        capture.Image = fileBitmap;
                    }
                    else
                    {
                        capture = new Capture(fileBitmap);
                    }
                    // Force Editor, keep picker, this is currently the only usefull destination
                    if (capture.CaptureDetails.HasDestination(PickerDestination.DESIGNATION))
                    {
                        capture.CaptureDetails.ClearDestinations();
                        capture.CaptureDetails.AddDestination(DestinationHelper.GetDestination(EditorDestination.DESIGNATION));
                        capture.CaptureDetails.AddDestination(DestinationHelper.GetDestination(PickerDestination.DESIGNATION));
                    }
                    else
                    {
                        capture.CaptureDetails.ClearDestinations();
                        capture.CaptureDetails.AddDestination(DestinationHelper.GetDestination(EditorDestination.DESIGNATION));
                    }
                    HandleCapture();
                }
                break;

            case CaptureMode.LastRegion:
                if (!RuntimeConfig.LastCapturedRegion.IsEmpty)
                {
                    capture = WindowCapture.CaptureRectangle(capture, RuntimeConfig.LastCapturedRegion);
                    if (windowDetailsThread != null)
                    {
                        windowDetailsThread.Join();
                    }
                    capture.CaptureDetails.AddMetaData("source", "screen");
                    HandleCapture();
                }
                break;

            case CaptureMode.Region:
                // Check if a region is pre-supplied!
                if (Rectangle.Empty.Equals(captureRect))
                {
                    capture = WindowCapture.CaptureScreen(capture);
                    capture.CaptureDetails.AddMetaData("source", "screen");
                    CaptureWithFeedback();
                }
                else
                {
                    capture = WindowCapture.CaptureRectangle(capture, captureRect);
                    capture.CaptureDetails.AddMetaData("source", "screen");
                    HandleCapture();
                }
                break;

            case CaptureMode.Video:
                capture = WindowCapture.CaptureScreen(capture);
                // Set the capturemode to be window
                captureMode = CaptureMode.Window;
                capture.CaptureDetails.AddMetaData("source", "Video");
                CaptureWithFeedback();
                break;

            default:
                LOG.Warn("Unknown capture mode: " + captureMode);
                break;
            }
            if (windowDetailsThread != null)
            {
                windowDetailsThread.Join();
            }
            if (capture != null)
            {
                LOG.Debug("Disposing capture");
                capture.Dispose();
            }
        }