Converting WebBrowser.Document To A Bitmap?

前端 未结 4 2085
梦毁少年i
梦毁少年i 2020-12-13 15:57

Is it possible to draw a WebBrowser.Document to a Bitmap? Basically taking a screenshot of a WebBrowser control (note, this is with a WebBrowser that doesn\'t live on a for

4条回答
  •  隐瞒了意图╮
    2020-12-13 16:09

    //
    //   If you want to take a snap from existing webBrowser Control
    //
    
        private void button1_Click(object sender, EventArgs e)
        {
           using (FileDialog fd = new SaveFileDialog())
            {
                fd.Filter = "Image (*.png)|*.png";
                if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    new WebPageSnap(webBrowser1.Url.ToString(), fd.FileName);
                    //might take 3 or 4 seconds to save cauz it has to load again.
                }
            }
        }
    
    
    //
    //   Or if you want to take a snap without showing up
    //
    
    
        private void button2_Click(object sender, EventArgs e)
        {
           using (FileDialog fd = new SaveFileDialog())
            {
                fd.Filter = "Image (*.png)|*.png";
                if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string url = "http://www.google.com";
                    // or 
                    url = textBox1.Text;
                    new WebPageSnap(url, fd.FileName);                }
            }
        }
    
        class WebPageSnap
        {
            WebBrowser wb;
            string outFile;
    
            public WebPageSnap(string url, string outputFile)
            {
                wb = new WebBrowser();
                wb.ProgressChanged += wb_ProgressChanged;
                outFile = outputFile;
                wb.ScriptErrorsSuppressed = true;
                wb.ScrollBarsEnabled = false;
                wb.Navigate(url);
            }
    
            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 { }
                }
            }
    
        }
    

提交回复
热议问题