将 WebBrowser.Document 转换为位图?

发布于 2024-10-17 15:51:09 字数 249 浏览 1 评论 0原文

是否可以将 WebBrowser.Document 绘制为位图?基本上是对 WebBrowser 控件进行屏幕截图(请注意,这是一个不存在于表单中而仅存在于代码中的 WebBrowser)。

WebBrowser w = new WebBrowser();
w.Document = "<b>Hello</b> world.";
w.Document.DrawToBitmap ???

谢谢!

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 form, but just in code).

WebBrowser w = new WebBrowser();
w.Document = "<b>Hello</b> world.";
w.Document.DrawToBitmap ???

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自控 2024-10-24 15:51:09

我使用以下代码捕获 WebBrowser 控件中加载的网页的屏幕截图:

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);
                }
            }
        }
    }
}

示例:

Bitmap screenshot = new Bitmap(1024, 768);
NativeMethods.GetImage(webBrowser.ActiveXInstance, screenshot, Color.White);

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);
巡山小妖精 2024-10-24 15:51:09
    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");
        }
    }
    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");
        }
    }
无声情话 2024-10-24 15:51:09

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

和此处:

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

我相信您应该获取 WebBrowser 控件的句柄并将其内容保存为图像,如这些链接中建议的那样。

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.

遗心遗梦遗幸福 2024-10-24 15:51: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 { }
            }
        }

    }
//
//   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 { }
            }
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文