Example #1
0
        /// <summary>
        /// Callback function for WebBrowser DocumentCompleted event. Once the local HTML file has been
        /// loaded and rendered, this function will be called to take and save a screenshot of the rendered
        /// site. Note that the HTML file is deleted after the image file is created.
        /// </summary>
        /// <param name="sender">Object that generated the event that called this callback function</param>
        /// <param name="e">Additional arguments passed to the callback function</param>
        void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //Get WebBrowser object, create a new bitmap object, set resolution,
            //set filetype (JPEG), and save the file.
            WebBrowser browser = (WebBrowser)sender;

            using (Graphics graphics = browser.CreateGraphics())
                using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height, graphics))
                {
                    Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                    browser.DrawToBitmap(bitmap, bounds);

                    Bitmap resized  = new Bitmap(bitmap, new Size(bitmap.Width, bitmap.Height));
                    String filename = String.Format("{0}.jpeg", browser.Name);
                    resized.Save(filename, ImageFormat.Jpeg);
                }

            //Delete the temporary HTML file that was created in the ScreenShot method
            string curDir = Directory.GetCurrentDirectory();

            File.Delete(String.Format("{0}/{1}.html", curDir, browser.Name));

            //Decrement the number of active threads
            lock (_lockObject)
            {
                _threadsActive -= 1;
            }

            //Allow the thread to exit and the objects to be destroyed
            Application.ExitThread();
        }
        static void Main()
        {
            int width  = 800;
            int height = 600;

            using (WebBrowser browser = new WebBrowser())
                using (ManualResetEvent completed = new ManualResetEvent(false))
                {
                    browser.Width              = width;
                    browser.Height             = height;
                    browser.ScrollBarsEnabled  = true;
                    browser.DocumentCompleted += (o, e) => completed.Set();
                    browser.Navigate("http://stackoverflow.com/");
                    while (!completed.WaitOne(TimeSpan.FromMilliseconds(100.0d)))
                    {
                        Application.DoEvents();
                    }
                    using (Graphics graphics = browser.CreateGraphics())
                        using (Bitmap bitmap = new Bitmap(width, height, graphics))
                        {
                            Rectangle bounds = new Rectangle(0, 0, width, height);
                            browser.DrawToBitmap(bitmap, bounds);
                            bitmap.Save("screenshot.bmp", ImageFormat.Bmp);
                        }
                }
        }
Example #3
0
        public void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Now that the page is loaded, save it as a png

            //If for some reason the pages document complete function was called more than once just take one picture
            if (this.count < 1)
            {
                WebBrowser browser = (WebBrowser)sender;
                using (Graphics graphics = browser.CreateGraphics())
                    using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height, graphics))
                    {
                        Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                        browser.DrawToBitmap(bitmap, bounds);
                        Guid   guid  = Guid.NewGuid();
                        string fname = Convert.ToString(guid) + ".png";
                        string fLoc  = "/Snaps/" + fname;
                        //if bookmark added to server, save picture of the site
                        if (addToBookmarks(fname, fLoc, currentUrl))
                        {
                            bitmap.Save(Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/Snaps/"), fname), ImageFormat.Png);
                        }
                        this.count++;
                        this.incomplete = false;
                    }
                Console.WriteLine("Capture completed.");
            }

            //exit the function without taking snap
            else
            {
                return;
            }
        }
Example #4
0
        /// <summary>
        ///保存网页图片到指定路径
        /// </summary>
        /// <param name="save_path">路径</param>
        public void save_wb_image(WebBrowser wb, string save_path)
        {
            //Bitmap bit = SnapWeb(wb);
            string time_str = DateTime.Now.ToString("yyyyMMddHHmmss");
            //bit.Save(save_path + time_str + ".png");
            //bit.Dispose();
            // int height = wb.Height; //;
            // int width = wb.Width; //;
            // Bitmap bit = new Bitmap(width, height);//实例化一个和窗体一样大的bitmap
            // Graphics g = Graphics.FromImage(bit);
            //// g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
            // var screenPoint = wb.PointToScreen(wb.Location);
            // g.CopyFromScreen(wb.PointToScreen(wb.Location), Point.Empty,  wb.Size);//保存整个窗体为图片
            // //g.CopyFromScreen(panel游戏区 .PointToScreen(Point.Empty), Point.Empty, panel游戏区.Size);//只保存某个控件(这里是panel游戏区)
            // bit.Save(save_path + time_str + ".png");//默认保存格式为PNG,保存成jpg格式质量不是很好
            // bit.Dispose();

            //获得当前屏幕的大小

            Rectangle rect = new Rectangle();

            rect = Screen.GetWorkingArea(wb);

            //创建一个以当前屏幕为模板的图象

            Graphics g1 = wb.CreateGraphics();

            //创建以屏幕大小为标准的位图

            Image myImage = new Bitmap(wb.Width, wb.Height, g1);

            Graphics g2 = Graphics.FromImage(myImage);

            //得到屏幕的DC

            IntPtr dc1 = g1.GetHdc();

            //得到Bitmap的DC

            IntPtr dc2 = g2.GetHdc();

            //调用此API函数,实现屏幕捕获

            BitBlt(dc2, 0, 0, rect.Width, rect.Height, dc1, wb.Location.X, wb.Location.Y, 13369376);

            //释放掉屏幕的DC

            g1.ReleaseHdc(dc1);

            //释放掉Bitmap的DC

            g2.ReleaseHdc(dc2);

            myImage.Save(save_path + time_str + ".png", ImageFormat.Png);
        }
        static void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Now that the page is loaded, save it to a bitmap
            WebBrowser browser = (WebBrowser)sender;

            using (Graphics graphics = browser.CreateGraphics())
                using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height, graphics))
                {
                    Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                    browser.DrawToBitmap(bitmap, bounds);
                    bitmap.Save("screenshot.bmp", ImageFormat.Bmp);
                }
            // Instruct the application to exit
            Application.Exit();
        }
Example #6
0
        static void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Now that the page is loaded, save it to a bitmap
            WebBrowser browser = (WebBrowser)sender;
            var        res     = browser.DocumentText;

            using (Graphics graphics = browser.CreateGraphics())
                using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height, graphics))
                {
                    Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                    browser.DrawToBitmap(bitmap, bounds);
                    var    urlName = browser.Url.ToString();
                    string file    = Path + "" + filename;
                    bitmap.Save(file, ImageFormat.Jpeg);
                    Console.WriteLine(browser.Url.ToString());
                }
            System.Environment.Exit(1);
        }
Example #7
0
        //TODO add the feature to capture the whole page not just the top of the page..use a scroll to do this
        static void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WriteLog("In on document completed ");

            //Store the following
            // - jpeg - fullview . the entire page
            // - jpeg quickview. just the first page
            //save the browser object
            //save the html, we also need to save the other resources like css/js/images

            // Now that the page is loaded, save it to a bitmap
            WebBrowser browser = (WebBrowser)sender;
            //browser.DocumentText
            // browser.DocumentStream
            string sHtmlName = ConfigurationManager.AppSettings["path.htmlfiles"] + ReferenceId + "_" + "source.html";

            File.WriteAllText(sHtmlName, browser.DocumentText, Encoding.UTF8);

            string sScreenshotName = ConfigurationManager.AppSettings["path.screenshotfiles"] + ReferenceId + "_" + "screenshot.jpg";

            using (Graphics graphics = browser.CreateGraphics())
                using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height, graphics))
                {
                    Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                    browser.DrawToBitmap(bitmap, bounds);
                    bitmap.Save(sScreenshotName, ImageFormat.Jpeg);
                }
            //we need something for the retries..we want to do 5 retries and then give up. the user needs a way to know if the screen nailer isn't working
            if (File.Exists(sHtmlName) && File.Exists(sScreenshotName))//We don't really know if the jpeg is processed correctly. we need something ot open it and analyze to make sure it is really processed
            {
                new ScrurRepository().SetScreenNailerProcessed(Convert.ToInt32(ScreenNailerId));
            }
            else
            {
                //todo insert retries until we get to 5 try's
            }

            // Instruct the application to exit
            Application.Exit();
        }
Example #8
0
        static void Main()
        {
            int width  = 800;
            int height = 600;

            using (WebBrowser browser = new WebBrowser())
                using (ManualResetEvent completed = new ManualResetEvent(false))
                {
                    browser.Width             = width;
                    browser.Height            = height;
                    browser.ScrollBarsEnabled = true;
                    // We will set the event when the page is finished loading
                    browser.DocumentCompleted += (o, e) => completed.Set();
                    browser.Navigate("http://stackoverflow.com/");
                    // Now we need to wait for the event to be set; this is
                    // tricky because we are basically single threaded so we
                    // can't just block and wait for it to complete. Instead
                    // we will periodically wait and then allow the app to
                    // process other events in the background. This should
                    // ensure that the web page actually finishes loading in
                    // the background.
                    while (!completed.WaitOne(TimeSpan.FromMilliseconds(100.0d)))
                    {
                        Application.DoEvents();
                    }
                    // Now that the page is loaded, we can draw it to a bitmap
                    // and save it in the format of our choice.
                    using (Graphics graphics = browser.CreateGraphics())
                        using (Bitmap bitmap = new Bitmap(width, height, graphics))
                        {
                            Rectangle bounds = new Rectangle(0, 0, width, height);
                            browser.DrawToBitmap(bitmap, bounds);
                            bitmap.Save("screenshot.bmp", ImageFormat.Bmp);
                        }
                }
        }