Converting WebBrowser.Document To A Bitmap?

前端 未结 4 2082
梦毁少年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:03

    http://www.bryancook.net/2006/03/screen-capture-for-invisible-windows.html

    and here:

    http://www.codeproject.com/KB/graphics/screen_capturing.aspx

    I believe you should get the handle of your WebBrowser control and save it's content as image like suggested in those links.

    0 讨论(0)
  • 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 { }
                }
            }
    
        }
    
    0 讨论(0)
  • 2020-12-13 16:18
        public void HTMLScreenShot()
        {
            WebBrowser wb = new WebBrowser();
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
            wb.Size = new Size(800, 600);
    
            // Add html as string
            wb.Navigate("about:blank");
            wb.Document.Write("<b>Hellow World!</b>");
            // Add html from website
            // wb.Navigate("http://myurl.com");
    
        }
    
        void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser wb = sender as WebBrowser;
    
            using (Bitmap bitmap = new Bitmap(wb.Width, wb.Height))
            {
                Rectangle bounds = new Rectangle(new Point(0, 0), wb.Size);
                wb.DrawToBitmap(bitmap, bounds);
                bitmap.Save("C:\WebsiteScreenshot.png");
            }
        }
    
    0 讨论(0)
  • 2020-12-13 16:21

    I use the following code to capture a screenshot of a web page loaded in a WebBrowser control:

    class NativeMethods
    {
        [ComImport]
        [Guid("0000010D-0000-0000-C000-000000000046")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        interface IViewObject
        {
            void Draw([MarshalAs(UnmanagedType.U4)] uint dwAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [MarshalAs(UnmanagedType.Struct)] ref RECT lprcBounds, [In] IntPtr lprcWBounds, IntPtr pfnContinue, [MarshalAs(UnmanagedType.U4)] uint dwContinue);
        }
    
        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
    
        public static void GetImage(object obj, Image destination, Color backgroundColor)
        {
            using(Graphics graphics = Graphics.FromImage(destination))
            {
                IntPtr deviceContextHandle = IntPtr.Zero;
                RECT rectangle = new RECT();
    
                rectangle.Right = destination.Width;
                rectangle.Bottom = destination.Height;
    
                graphics.Clear(backgroundColor);
    
                try
                {
                    deviceContextHandle = graphics.GetHdc();
    
                    IViewObject viewObject = obj as IViewObject;
                    viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rectangle, IntPtr.Zero, IntPtr.Zero, 0);
                }
                finally
                {
                    if(deviceContextHandle != IntPtr.Zero)
                    {
                        graphics.ReleaseHdc(deviceContextHandle);
                    }
                }
            }
        }
    }
    

    Example:

    Bitmap screenshot = new Bitmap(1024, 768);
    NativeMethods.GetImage(webBrowser.ActiveXInstance, screenshot, Color.White);
    
    0 讨论(0)
提交回复
热议问题