Ejemplo n.º 1
0
        private Image InnerCaptureUrl(string baseUrl, TimeSpan timeOut)
        {
            Image result = null;

            object lockThread = new object();

            using (ApplicationContext applicationContext = new ApplicationContext())
            {
                using (GeckoWebBrowser webBrowser = new GeckoWebBrowser()
                {
                    Size = BrowserSize,
                    Visible = true,
                    TabIndex = 0,
                    UseHttpActivityObserver = false
                })
                {
                    using (GeckoTriggerInstaller installer = new GeckoTriggerInstaller(webBrowser))
                    {
                        Exception readException = null;

                        installer.FireEvent += delegate
                        {
                            try
                            {
                                result = ReadBitmap(webBrowser, CaptureZone, new GeckoDocumentAdapter(webBrowser));

                                webBrowser.Stop();
                            }
                            catch (Exception exception)
                            {
                                // Rethrow in principal
                                readException = exception;
                            }
                            finally
                            {
                                lock (lockThread)
                                {
                                    // Release the lock
                                    Monitor.Pulse(lockThread);
                                }
                            }
                        };

                        webBrowser.NavigationError += (sender, args) =>
                        {
                            readException = new CaptureEngineException($"Navigation error : {((GeckoWebBrowser) sender).StatusText}", CaptureEngineState.NavigationError);

                            lock (lockThread)
                            {
                                // Release the lock as loading sends an error
                                Monitor.Pulse(lockThread);
                            }
                        };

                        Trigger.Install(installer);

                        InitializeLanguage();

                        InitializeCookies();

                        InitializeUserAgent();


                        webBrowser.Navigate(baseUrl);

                        ApplicationExtensions.Run(applicationContext, webBrowser, lockThread, timeOut);

                        if (readException != null)
                        {
                            throw readException;
                        }

                        if (result == null)
                        {
                            throw new CaptureEngineException("Time out exception", CaptureEngineState.Timeout);
                        }
                    }
                }
            }

            return(result);
        }