日期:2014-05-18 浏览次数:20611 次
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using mshtml;
using SHDocVw;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                webBrowser1.Navigate("http://www.google.com/");
                while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();
                }
                mshtml.IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
                IHTMLElementRender render = doc.body as IHTMLElementRender;
                using (System.Drawing.Bitmap img = new Bitmap(webBrowser1.Width, webBrowser1.Height))
                using (Graphics g = Graphics.FromImage(img))
                {
                    IntPtr hdc = g.GetHdc();
                    render.DrawToDC(hdc);
                    g.ReleaseHdc(hdc); 
                    img.Save("c:\\temp\\a.jpg");
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
        }
    }
    /// <remarks>
    ///  prototype from Tlbimp  
    /// </remarks>
    [
    Guid("3050F669-98B5-11CF-BB82-00AA00BDCE0B"),
    InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown),
    ComImport
    ]
    interface IHTMLElementRender
    {
        void DrawToDC([In] IntPtr hDC);
        void SetDocumentPrinter([In, MarshalAs(UnmanagedType.BStr)] string bstrPrinterName, [In] IntPtr hDC);
    };
}