Example #1
0
 /// <summary>
 /// save the screenshot of current webpage in webbrowser
 /// </summary>
 public void saveBitmap(WebBrowser wb, string keyword)
 {
     if (wb.Document == null)
     {
         return;
     }
     try
     {
         int scrollWidth  = 0;
         int scrollHeight = 0;
         scrollHeight = 768;  //wb.Document.Body.ScrollRectangle.Height;
         scrollWidth  = 1366; //wb.Document.Body.ScrollRectangle.Width;
         wb.Size      = new Size(scrollWidth, scrollHeight);
         Bitmap bm = new Bitmap(scrollWidth, scrollHeight);
         wb.DrawToBitmap(bm, new Rectangle(0, 0, bm.Width, bm.Height));
         string SaveAsName = null;
         SaveAsName = Regex.Replace(keyword, "(\\\\|\\/|\\:|\\*|\\?|\\\"|\\<|\\>|\\|)?", "");
         bm.Save(SaveAsName + ".png", System.Drawing.Imaging.ImageFormat.Png);
         bm.Dispose();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     finally
     {
         //
     }
 }
        /// <summary>
        /// Makes screenshot.
        /// </summary>
        protected void MakeScreenshot()
        {
            // Load the webpage into a WebBrowser control
            WebBrowser wb = new WebBrowser();

            wb.ScrollBarsEnabled      = _scrollingEnabled;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(_uri);
            while (wb.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            // Set the size of the WebBrowser control
            wb.Width  = _imageWidth;
            wb.Height = _imageHeight;

            // Take Screenshot of the web pages full width
            if (_imageHeight < 0)
            {
                wb.Width  = wb.Document.Body.ScrollRectangle.Width;
                wb.Height = wb.Document.Body.ScrollRectangle.Height;
            }

            // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
            Bitmap bitmap = new Bitmap(wb.Width, wb.Height);

            wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
            wb.Dispose();

            _image = bitmap;
        }
Example #3
0
        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser wb = (WebBrowser)sender;

            // 网页加载完毕才保存
            if (wb.ReadyState == WebBrowserReadyState.Complete)
            {
                // 获取网页高度和宽度,也可以自己设置
                int height = wb.Document.Body.ScrollRectangle.Height;
                int width  = wb.Document.Body.ScrollRectangle.Width;

                // 调节webBrowser的高度和宽度
                wb.Height = height;
                wb.Width  = width;

                Bitmap    bitmap    = new Bitmap(width, height);          // 创建高度和宽度与网页相同的图片
                Rectangle rectangle = new Rectangle(0, 0, width, height); // 绘图区域
                wb.DrawToBitmap(bitmap, rectangle);                       // 截图

                // 保存图片对话框
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "JPEG (*.jpg)|*.jpg|PNG (*.png)|*.png";
                saveFileDialog.ShowDialog();

                bitmap.Save(saveFileDialog.FileName);  // 保存图片
            }
        }
        //************************************************************************
        // Sets a flag when the Document is completely loaded
        private static Bitmap WebImageAsBitmap(String url)
        {
            WaitForNavigate(url);
            HtmlElementCollection images = webBrowser.Document.Images;

            if (images.Count == 1)
            {
                HtmlElement imageElement   = images[0];
                Rectangle   imageRectangle = imageElement.OffsetRectangle;
                // Set Bitmap Size (including the Offset)
                Bitmap bitmap = new Bitmap(imageElement.ClientRectangle.Width + imageElement.OffsetRectangle.X,
                                           imageElement.ClientRectangle.Height + imageElement.OffsetRectangle.Y);

                // Set the Size of the Browser such the hole image can be captured
                Size webBrowserSize = new Size();
                webBrowserSize.Width  = imageRectangle.Width + webBrowser.Size.Width;
                webBrowserSize.Height = imageRectangle.Height + +webBrowser.Size.Height;
                webBrowser.Size       = webBrowserSize;
                // Include the Offset in the Width and Height
                imageRectangle.Width  += imageRectangle.X;
                imageRectangle.Height += imageRectangle.Y;
                // Remove the offset, since the browser will always draw from his top left point
                imageRectangle.X = 0;
                imageRectangle.Y = 0;
                // Draws the Browser into the Bitmap
                webBrowser.DrawToBitmap(bitmap, imageRectangle);
                // Get the original Image
                bitmap = CropImage(bitmap, imageElement.OffsetRectangle);

                return(bitmap);
            }
            throw new Exception("You can only load Image Pages");
        }
Example #5
0
        public void webtoPic(WebBrowser webBrowser)
        {
            // 网页加载完毕才保存
            if (webBrowser.ReadyState == WebBrowserReadyState.Complete)
            {
                // 获取网页高度和宽度,也可以自己设置
                int height = webBrowser.Document.Body.ScrollRectangle.Height;

                int width = webBrowser.Document.Body.ScrollRectangle.Width;
                // 调节webBrowser的高度和宽度
                webBrowser.Height = height;
                webBrowser.Width  = width;
                Bitmap    bitmap    = new Bitmap(width, height);             // 创建高度和宽度与网页相同的图片
                Rectangle rectangle = new Rectangle(0, 0, width, height);    // 绘图区域
                webBrowser.DrawToBitmap(bitmap, rectangle);                  // 截图
                // 保存图片对话框
                // SaveFileDialog saveFileDialog = new SaveFileDialog();
                // saveFileDialog.Filter = "JPEG (*.jpg)|*.jpg|PNG (*.png)|*.png";
                // saveFileDialog.ShowDialog();

                // bitmap.Save(saveFileDialog.FileName);  // 保存图片
                // panel2.BackgroundImage = bitmap;
                pictureBox_radar.BackgroundImage = bitmap;
            }
        }
Example #6
0
    void wb_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
        if (e.CurrentProgress == e.MaximumProgress)
        {
            wb.ProgressChanged -= wb_ProgressChanged;
            try
            {
                int scrollWidth  = 0;
                int scrollHeight = 0;
                scrollHeight = wb.Document.Body.ScrollRectangle.Height;
                scrollWidth  = wb.Document.Body.ScrollRectangle.Width;
                wb.Size      = new Size(scrollWidth, scrollHeight);

                Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
                for (int Xcount = 0; Xcount < bitmap.Width; Xcount++)
                {
                    for (int Ycount = 0; Ycount < bitmap.Height; Ycount++)
                    {
                        bitmap.SetPixel(Xcount, Ycount, Color.Black);
                    }
                }
                wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
                bitmap.Save(outFile, ImageFormat.Png);
            }
            catch { }
        }
    }
Example #7
0
    public Bitmap GenerateScreenshot(string url, int width, int height)
    {
        // Load the webpage into a WebBrowser control
        WebBrowser wb = new WebBrowser();

        wb.ScrollBarsEnabled      = false;
        wb.ScriptErrorsSuppressed = true;
        wb.Navigate(url);
        // while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }


        // Set the size of the WebBrowser control
        wb.Width  = width;
        wb.Height = height;

        if (width == -1)
        {
            // Take Screenshot of the web pages full width
            wb.Width = wb.Document.Body.ScrollRectangle.Width;
        }

        if (height == -1)
        {
            // Take Screenshot of the web pages full height
            wb.Height = wb.Document.Body.ScrollRectangle.Height;
        }

        // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
        Bitmap bitmap = new Bitmap(wb.Width, wb.Height);

        wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
        wb.Dispose();

        return(bitmap);
    }
Example #8
0
        public Bitmap DrawBitmap()
        {
            this.Height = MyBrowser.Document.Body.OffsetRectangle.Bottom;
            Bitmap    myBitmap = new Bitmap(Width, Height);
            Rectangle DrawRect = new Rectangle(0, 0, Width, Height);

            MyBrowser.DrawToBitmap(myBitmap, DrawRect);
            System.Drawing.Image imgOutput  = myBitmap;
            System.Drawing.Image oThumbNail = new Bitmap(Width, this.Height, imgOutput.PixelFormat);
            Graphics             g          = Graphics.FromImage(oThumbNail);

            g.CompositingQuality = CompositingQuality.HighSpeed;
            g.SmoothingMode      = SmoothingMode.HighSpeed;
            g.InterpolationMode  = InterpolationMode.HighQualityBilinear;
            Rectangle oRectangle = new Rectangle(0, 0, Width, this.Height);

            g.DrawImage(imgOutput, oRectangle);
            try
            {
                return((Bitmap)oThumbNail);
            }
            catch (Exception)
            {
                return(null);
            }
            finally
            {
                imgOutput.Dispose();
                imgOutput = null;
                MyBrowser.Dispose();
                MyBrowser = null;
            }
        }
Example #9
0
        private static void wb_callback(Object sender, WebBrowserDocumentCompletedEventArgs args)
        {
            WebBrowser wb = (WebBrowser)sender;

            if (wb.ReadyState == WebBrowserReadyState.Complete)
            {
                //将浏览器的长宽设置成参数里的长宽
                wb.Width  = para.m_WinWidth;
                wb.Height = para.m_WinHeight;

                int left   = para.m_Left;
                int top    = para.m_Top;
                int width  = para.m_SnapWidth;
                int height = para.m_SnapHeight;

                Bitmap    tmp  = new Bitmap(para.m_WinWidth, para.m_WinHeight);
                Rectangle rect = new Rectangle(0, 0, para.m_WinWidth, para.m_WinHeight);
                wb.DrawToBitmap(tmp, rect);  //进行截图,完整的一张图

                Bitmap    bm  = new Bitmap(width, height);
                Graphics  g   = Graphics.FromImage(bm);
                Rectangle src = new Rectangle(left, top, width, height);
                Rectangle des = new Rectangle(0, 0, width, height);
                g.DrawImage(tmp, des, src, GraphicsUnit.Pixel);

                bm.Save(para.m_Path);              //将截图储存
                SnapUtil.result = new SnapResult(tmp);
            }
        }
Example #10
0
        private static void _browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // http://support.microsoft.com/kb/180366
            // document completed event gets fired multiple times (once for each frame)
            // the top level frame firing this event last with ReadyState == Complete

            if (_browser.ReadyState == WebBrowserReadyState.Complete)
            {
                Bitmap    bitmap = new Bitmap(PREVIEW_WIDTH, PREVIEW_HEIGHT);
                Rectangle rect   = new Rectangle(0, 0, PREVIEW_WIDTH, PREVIEW_HEIGHT);
                _browser.DrawToBitmap(bitmap, rect);

                // turn image into byte[] so it's easier to work with
                MemoryStream imageStream = new MemoryStream();
                bitmap.Save(imageStream, ImageFormat.Jpeg);
                byte[] imageBytes = imageStream.ToArray();

                // cache image
                _cache.Add(_loadingUrl, imageBytes);

                // clean up
                bitmap.Dispose();
                imageStream.Close();
                _loadingUrl = string.Empty;

                // notify subscribers
                OnDocumentCompleted(imageBytes);
            }
        }
Example #11
0
        public Bitmap GenerateScreenshot(string url)
        {
            try
            {
                // Load the webpage into a WebBrowser control
                WebBrowser wb = new WebBrowser();
                wb.ScrollBarsEnabled      = false;
                wb.ScriptErrorsSuppressed = true;
                wb.Navigate(url);

                // waits for the page to be completely loaded
                while (wb.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();
                }

                // Take Screenshot of the web pages full width + some padding
                wb.Width = wb.Document.Body.ScrollRectangle.Height;
                // Take Screenshot of the web pages full height
                wb.Height = wb.Document.Body.ScrollRectangle.Height;

                // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
                Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
                wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, wb.Width, wb.Height));
                wb.Dispose();

                return(bitmap);
            }
            catch (Exception ex)
            {
                EpointMessage.MsgOk(ex.ToString());
                return(null);
            }
        }
Example #12
0
        //
        // Process the contents of a fully loaded WebBrowser control
        //
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser webBrowser = (WebBrowser)sender;

            try
            {
                webBrowser.ClientSize        = new Size(this.width, this.height);
                webBrowser.ScrollBarsEnabled = false;
                if (this.renderImage)
                {
                    this.capture = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
                    webBrowser.BringToFront();
                    webBrowser.DrawToBitmap(capture, webBrowser.Bounds);
                }
                else
                {
                    this.imageSrc = new List <string>();
                    HtmlElementCollection images = webBrowser.Document.Images;
                    for (int i = 0; i < images.Count; i++)
                    {
                        string src = ((mshtml.HTMLImgClass)webBrowser.Document.Images[i].DomElement).src;
                        if (!this.imageSrc.Contains(src))
                        {
                            this.imageSrc.Add(src);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            webBrowser.Dispose();
            docLoaded = true;
        }
Example #13
0
        public Bitmap CaptureWebPage(string URL)
        {
            using (var webBrowser = new WebBrowser { ScrollBarsEnabled = false, ScriptErrorsSuppressed = true })
            {
                webBrowser.Navigate(URL);

                while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();
                }

                // Thread.Sleep(2500);

                const int width = 1500;

                const int height = 1700; // webBrowser.Document.Body.ScrollRectangle.Height + 50;

                webBrowser.Width = width;
                webBrowser.Height = height;

                Bitmap bitmap = new Bitmap(width, height);

                webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, width, height));

                return bitmap;
            }
        }
Example #14
0
        /// <summary>
        /// 网页加载完成事件处理函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser webBrowser = (WebBrowser)sender;

            // 网页加载完毕才保存
            if (webBrowser.ReadyState == WebBrowserReadyState.Complete)
            {
                // 获取网页高度和宽度,也可以自己设置
                //int height = webBrowser.Document.Body.ScrollRectangle.Height;
                //int width = webBrowser.Document.Body.ScrollRectangle.Width;
                int         height = 700;
                int         width  = 1050;
                HtmlElement em     = webBrowser.Document.GetElementById("J_1730_110100");
                Debug.WriteLine(em);
                // 调节webBrowser的高度和宽度
                webBrowser.Height = height;
                webBrowser.Width  = width;

                Bitmap    bitmap    = new Bitmap(width, height);          // 创建高度和宽度与网页相同的图片
                Rectangle rectangle = new Rectangle(0, 0, width, height); // 绘图区域
                webBrowser.DrawToBitmap(bitmap, rectangle);               // 截图

                // 保存图片对话框
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "JPEG (*.jpg)|*.jpg|PNG (*.png)|*.png";
                saveFileDialog.ShowDialog();

                bitmap.Save(saveFileDialog.FileName);  // 保存图片
            }
        }
Example #15
0
        private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser wb = sender as WebBrowser;

            if (!wb.Document.Title.Equals("HTTP 404 Not Found"))
            {
                int scrollWidth  = 0;
                int scrollHeight = 0;

                scrollHeight = wb.Document.Body.ScrollRectangle.Height;
                scrollWidth  = wb.Document.Body.ScrollRectangle.Width;
                wb.Size      = new Size(scrollWidth, scrollHeight);


                Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
                for (int xcount = 0; xcount < bitmap.Width; xcount++)
                {
                    for (int ycount = 0; ycount < bitmap.Height; ycount++)
                    {
                        bitmap.SetPixel(xcount, ycount, Color.Black);
                    }
                }
                wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
                bitmap.Save(_filePath, ImageFormat.Png);
            }
        }
 private Size RenderHtmlValue(
     Graphics graphics,
     Rectangle cellBounds,
     string formattedValue,
     bool drawCell)
 {
     using (var webBrowser = new WebBrowser())
     {
         webBrowser.ScrollBarsEnabled = false;
         webBrowser.Navigate("about:blank");
         webBrowser.Document.Write(formattedValue);
         webBrowser.Size = Size;
         var rect = new Rectangle(
             webBrowser.Document.Body.ScrollRectangle.Location,
             webBrowser.Document.Body.ScrollRectangle.Size);
         rect.Width      = Size.Width - cellBounds.X;
         webBrowser.Size = rect.Size;
         cellBounds      = new Rectangle(cellBounds.X, cellBounds.Y, rect.Width, rect.Height);
         var htmlBodyElement = webBrowser.Document.Body.DomElement as mshtml.IHTMLBodyElement;
         htmlBodyElement.WhenNotNull(
             bodyElement =>
         {
             cellBounds.Height += Convert.ToInt32(htmlBodyElement.bottomMargin);
         });
         if (drawCell)
         {
             using (var bitmap = new Bitmap(webBrowser.Width, webBrowser.Height))
             {
                 webBrowser.DrawToBitmap(bitmap, targetBounds);
                 graphics.DrawImage(bitmap, location);
             }
         }
     }
     return(cellBounds.Size);
 }
Example #17
0
        private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Now that the page is loaded, save it to a bitmap
            WebBrowser browser = sender as WebBrowser;

            browser.Visible = false;
            try
            {
                // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
                Rectangle bounds = Screen.PrimaryScreen.Bounds;
                using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
                {
                    browser.DrawToBitmap(bitmap, bounds);
                    bitmap.Save(imgPathInternal);
                }
            }
            catch (ThreadAbortException)
            {
                Console.WriteLine("Error aborting thread, returning");
                browser.Dispose();
                return;
            }
            finally
            {
                browser.Dispose();
            }
        }
Example #18
0
        private Bitmap _GenerateScreenshot(string url, int width, int height)
        {
            // Load the webpage into a WebBrowser control
            WebBrowser webBrowser = new WebBrowser();

            webBrowser.ScrollBarsEnabled      = false;
            webBrowser.ScriptErrorsSuppressed = true;
            webBrowser.Navigate(url);

            // wait for iiiiiiiiiiiit...
            while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }


            // Set the size of the WebBrowser control
            webBrowser.Width  = width;
            webBrowser.Height = height;

            // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
            Bitmap bitmap = new Bitmap(webBrowser.Width, webBrowser.Height);

            webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, webBrowser.Width, webBrowser.Height));
            webBrowser.Dispose();

            return(bitmap);
        }
Example #19
0
        /// <summary>
        /// 网页加载完成事件处理函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser webBrowser = (WebBrowser)sender;

            webBrowser.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
            // 网页加载完毕才保存
            if (webBrowser.ReadyState == WebBrowserReadyState.Complete)
            {
                // 获取网页高度和宽度,也可以自己设置
                int height = webBrowser.Document.Body.ScrollRectangle.Height;
                int width  = webBrowser.Document.Body.ScrollRectangle.Width + this.RightWidthPadding;

                // 调节webBrowser的高度和宽度
                webBrowser.Height = height;
                webBrowser.Width  = width;

                using (Bitmap bitmap = new Bitmap(width, height))             // 创建高度和宽度与网页相同的图片
                {
                    Rectangle rectangle = new Rectangle(0, 0, width, height); // 绘图区域
                    webBrowser.DrawToBitmap(bitmap, rectangle);               // 截图
                    if (ImageStream == null)
                    {
                        ImageStream = new MemoryStream();
                    }
                    bitmap.Save(ImageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                Reset.Set();
            }
        }
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser m_WebBrowser = (WebBrowser)sender;

            m_WebBrowser.ClientSize        = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
            m_WebBrowser.ScrollBarsEnabled = false;
            m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
            m_WebBrowser.BringToFront();
            m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
            //m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
            //WebBrowser webBrowser = (WebBrowser) sender;
            //{
            //    // 网页加载完毕才保存
            //    if (webBrowser.ReadyState == WebBrowserReadyState.Complete)
            //    {
            //        // 获取网页高度和宽度,也可以自己设置
            //        int height = webBrowser.Document.Body.ScrollRectangle.Height;
            //        int width = webBrowser.Document.Body.ScrollRectangle.Width;

            //        // 调节webBrowser的高度和宽度
            //        webBrowser.Height = height;
            //        webBrowser.Width = width;

            //        Bitmap bitmap = new Bitmap(width, height); // 创建高度和宽度与网页相同的图片
            //        Rectangle rectangle = new Rectangle(0, 0, width, height); // 绘图区域
            //        webBrowser.DrawToBitmap(bitmap, rectangle); // 截图

            //        // 保存图片
            //        m_Bitmap =
            //            (Bitmap)bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);


            //    }
            //}
        }
Example #21
0
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser webBrowser = (WebBrowser)sender;

            webBrowser.ClientSize        = new Size(this.width, this.height);
            webBrowser.ScrollBarsEnabled = false;
            bmp = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
            webBrowser.BringToFront();
            webBrowser.DrawToBitmap(bmp, webBrowser.Bounds);
            Image img = bmp.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
            //string fileName = url.Replace("http://", "") + ".jpg";
            //fileName = System.Web.HttpUtility.UrlEncode(fileName);
            string fileName = GetImageFileName(url);

            if (absolutePath != null && !File.Exists(absolutePath + fileName))
            {
                img.Save(absolutePath + fileName);
            }
            bmp = (Bitmap)img;
            webBrowser.Dispose();
            if (mre != null)
            {
                mre.Set();
            }
        }
Example #22
0
        private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser browser = (WebBrowser)sender;

            browser.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
            //HtmlElementCollection imgs = browser.Document.GetElementsByTagName("img");
            //foreach (HtmlElement item in imgs)
            //{
            //    item.SetAttribute("src", "");

            //    item.Style = "display:none";
            //}

            Rectangle rec = browser.Document.Body.ScrollRectangle;

            if (rec.Width < START_WIDTH)
            {
                rec.Width = START_WIDTH;
            }
            if (rec.Height < START_HEIGHT)
            {
                rec.Height = START_HEIGHT / 10;
            }
            bmp = new Bitmap(rec.Width, rec.Height);
            browser.DrawToBitmap(bmp, browser.Bounds);
            browser.Dispose();
            mre.Set();
        }
Example #23
0
        public Bitmap DrawBitmap(int theight, int twidth)
        {
            Bitmap    myBitmap = new Bitmap(Width, Height);
            Rectangle DrawRect = new Rectangle(0, 0, Width, Height);

            MyBrowser.DrawToBitmap(myBitmap, DrawRect);
            System.Drawing.Image imgOutput  = myBitmap;
            System.Drawing.Image oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
            Graphics             g          = Graphics.FromImage(oThumbNail);

            g.CompositingQuality = CompositingQuality.HighSpeed;
            g.SmoothingMode      = SmoothingMode.HighSpeed;
            g.InterpolationMode  = InterpolationMode.HighQualityBilinear;
            Rectangle orectangle = new Rectangle(0, 0, twidth, theight);

            g.DrawImage(imgOutput, orectangle);
            try
            {
                return((Bitmap)oThumbNail);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
            finally
            {
                imgOutput.Dispose();
                imgOutput = null;
                MyBrowser.Dispose();
                MyBrowser = null;
            }
        }
        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 #25
0
        private Bitmap ExportUrlToImage(string url, int width, int height)
        {
            // Load the webpage into a WebBrowser control
            WebBrowser wb = new WebBrowser();

            wb.ScrollBarsEnabled      = false;
            wb.ScriptErrorsSuppressed = true;

            // wb.Navigate(url);
            wb.Navigate(url, null, null,
                        "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36");
            while (wb.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            // Set the size of the WebBrowser control
            wb.Width  = width;
            wb.Height = height;

            Bitmap bitmap = new Bitmap(wb.Width, wb.Height);

            wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, wb.Width, wb.Height));

            wb.Dispose();

            return(bitmap);
        }
Example #26
0
        private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser browser = sender as WebBrowser;
            int        w       = browser.Document.Body.ScrollRectangle.Width;
            int        h       = browser.Document.Body.ScrollRectangle.Height;

            browser.ScriptErrorsSuppressed = true;
            using (Bitmap bitmap = new Bitmap(w, h))
            {
                browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
                using (MemoryStream stream = new MemoryStream())
                {
                    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] bytes = stream.ToArray();
                    using (FileStream sw = new FileStream(@"c:\temp\1.png", FileMode.OpenOrCreate))
                    {
                        using (BinaryWriter bw = new BinaryWriter(sw))
                        {
                            bw.Write(bytes);
                        }
                    }
                    //imgScreenShot.Visible = true;
                    //imgScreenShot.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
                }
            }
        }
Example #27
0
            private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                WebBrowser m_WebBrowser = (WebBrowser)sender;

                //自适应窗口大小
                //scrollLeft, scrollTop, scrollWidth, scrollHeight, clientWidth, clientHeight
                //---------------------------------------------------------------------------------
                //Util.Debug(m_WebBrowser.Document.Body.GetAttribute("scrollHeight"));
                //Util.Debug(m_WebBrowser.Document.Body.GetAttribute("scrollWidth"));
                //-------------------------------------------------------------------------------
                //OleDbHelper.AppendToFile(@"D:\wwwroot\chinaqmzxco29607\upload\debug.txt", Environment.NewLine
                //    + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + ": "+ m_WebBrowser.Document.Url.ToString()
                //    + Environment.NewLine);
                //OleDbHelper.AppendToFile(@"D:\wwwroot\chinaqmzxco29607\upload\debug.txt",
                //    "scrollHeight = " + m_WebBrowser.Document.Body.GetAttribute("scrollHeight") + Environment.NewLine
                //    + "scrollWidth = " + m_WebBrowser.Document.Body.GetAttribute("scrollWidth") + Environment.NewLine);

                if (this.m_BrowserWidth != 0)
                {
                    this.m_BrowserWidth = int.Parse(m_WebBrowser.Document.Body.GetAttribute("scrollWidth"));
                }
                if (this.m_BrowserHeight != 0)
                {
                    this.m_BrowserHeight = int.Parse(m_WebBrowser.Document.Body.GetAttribute("scrollHeight"));
                }

                m_WebBrowser.ClientSize        = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
                m_WebBrowser.ScrollBarsEnabled = false;
                m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
                m_WebBrowser.BringToFront();
                m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
                //m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
                m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, this.m_BrowserHeight, null, IntPtr.Zero);
            }
Example #28
0
        public Bitmap GenerateScreenshot(string html, int width, int height)
        {
            // Load the webpage into a WebBrowser control
            WebBrowser _wb = new WebBrowser();

            _wb.ScrollBarsEnabled      = false;
            _wb.ScriptErrorsSuppressed = true;
            _wb.DocumentText           = HtmlUtility.TrimScript(html);
            _wb.ScriptErrorsSuppressed = true;

            while (_wb.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            // Set the size of the WebBrowser control
            _wb.Width  = width;
            _wb.Height = height;

            if (width == -1)
            {
                // Take Screenshot of the web pages full width
                if (_wb.Document.Body.ScrollRectangle.Width != 0)
                {
                    _wb.Width = _wb.Document.Body.ScrollRectangle.Width;
                }
                else
                {
                    _wb.Width = 1024;
                }
            }

            if (height == -1)
            {
                // Take Screenshot of the web pages full height
                if (_wb.Document.Body.ScrollRectangle.Height != 0)
                {
                    _wb.Height = _wb.Document.Body.ScrollRectangle.Height;
                }
                else
                {
                    _wb.Height = 768;
                }
            }

            try
            {
                // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
                Bitmap _bitmap = new Bitmap(_wb.Width, _wb.Height);
                _wb.DrawToBitmap(_bitmap, new Rectangle(0, 0, _wb.Width, _wb.Height));

                _wb.Dispose();

                return(_bitmap);
            }
            catch
            {
                return(null);
            }
        }
Example #29
0
        public void TakeSnapshot(string location)
        {
            try
            {
                WebBrowser wb = (WebBrowser)GetCurrentWB();
                if (wb != null)
                {
                    if (wb.ReadyState == WebBrowserReadyState.Complete)
                    {
                        int width  = wb.Document.Body.ScrollRectangle.Width;
                        int height = wb.Document.Body.ScrollRectangle.Height;

                        _bitmap = new Bitmap(width, height);
                        wb.Dock = DockStyle.None;
                        wb.ScrollBarsEnabled = false;

                        wb.Size = _bitmap.Size;
                        wb.DrawToBitmap(_bitmap, wb.Bounds);

                        if (location != "")
                        {
                            _bitmap.Save(location);
                        }

                        wb.Dock = DockStyle.Fill;
                        wb.ScrollBarsEnabled = true;
                    }
                }
            }
            catch { }
        }
        private static Image RenderOperationWithBrowser(TemplateObject to, string htmlToRender, Size size)
        {
            using (WebBrowser w = new WebBrowser())
            {
                w.AllowNavigation        = true;
                w.ScrollBarsEnabled      = false;
                w.ScriptErrorsSuppressed = true;

                w.DocumentText = htmlToRender;

                while (w.ReadyState != WebBrowserReadyState.Complete)
                {
                    // Pump the message queue for the web browser.
                    Application.DoEvents();
                }

                // Set the size of the WebBrowser control
                int suggestedWidth  = w.Document.Body.ScrollRectangle.Width;
                int suggestedHeight = w.Document.Body.ScrollRectangle.Height;

                int realWidth  = (suggestedWidth < size.Width) ? suggestedWidth : size.Width;
                int realHeight = (suggestedHeight < size.Height) ? suggestedHeight : size.Height;

                // If any of the Size-properties is set to zero, use the full size part.
                w.Width  = (realWidth > 0) ? realWidth : suggestedWidth;
                w.Height = (realHeight > 0) ? realHeight : suggestedHeight;

                // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
                using (Bitmap bitmap = new Bitmap(w.Width, w.Height))
                {
                    w.DrawToBitmap(bitmap, new Rectangle(0, 0, w.Width, w.Height));
                    return((Image)bitmap.Clone());
                }
            }
        }