Wednesday, August 11, 2010

How to convert Html page to a Image using WebBrowser - Windows Forms

public partial class HtmlToImage : Form
{
    public HtmlToImage()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        HtmlSnap hc = new HtmlSnap();            
        hc.Create("http://www.charith.gunasekara.web-sphere.co.uk/");
        // Or you can set the DocumentText : HTML
    }
}
 
[ComVisible(true), ComImport()]
[GuidAttribute("0000010d-0000-0000-C000-000000000046")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IViewObject
{
    [returnMarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int Draw(
        [MarshalAs(UnmanagedType.U4)] UInt32 dwDrawAspect,
        int lindex,
        IntPtr pvAspect,
        [InIntPtr ptd,
        IntPtr hdcTargetDev,
        IntPtr hdcDraw,
        [MarshalAs(UnmanagedType.Struct)] ref Rectangle lprcBounds,
        [MarshalAs(UnmanagedType.Struct)] ref Rectangle lprcWBounds,
        IntPtr pfnContinue,
        [MarshalAs(UnmanagedType.U4)] UInt32 dwContinue);
    [PreserveSig]
    int GetColorSet([InMarshalAs(UnmanagedType.U4)] int dwDrawAspect,
        int lindex, IntPtr pvAspect, [InIntPtr ptd,
        IntPtr hicTargetDev, [OutIntPtr ppColorSet);
    [PreserveSig]
    int Freeze([InMarshalAs(UnmanagedType.U4)] int dwDrawAspect,
                    int lindex, IntPtr pvAspect, [OutIntPtr pdwFreeze);
    [PreserveSig]
    int Unfreeze([InMarshalAs(UnmanagedType.U4)] int dwFreeze);
    void SetAdvise([InMarshalAs(UnmanagedType.U4)] int aspects,
        [InMarshalAs(UnmanagedType.U4)] int advf,
        [InMarshalAs(UnmanagedType.Interface)] IAdviseSink pAdvSink);
    void GetAdvise([InOutMarshalAs(UnmanagedType.LPArray)] int[] paspects,
        [InOutMarshalAs(UnmanagedType.LPArray)] int[] advf,
        [InOutMarshalAs(UnmanagedType.LPArray)] IAdviseSink[] pAdvSink);
}
 
public class HtmlSnap
{
    #region Attributes
 
    private WebBrowser browser;
    private Rectangle screen;
    private Size? imgsize = null;
 
    #endregion
 
    #region Events
 
    public delegate void HtmlSnapEvent(object sender, Uri url, Bitmap image);
    public event HtmlSnapEvent HtmlImageCapture;
 
    #endregion
 
    #region Constructors
 
    public HtmlSnap()
    {
        browser = new WebBrowser();
        screen = Screen.PrimaryScreen.Bounds;
        browser.Width = screen.Width;
        browser.Height = screen.Height;
        browser.ScriptErrorsSuppressed = true;
        browser.ScrollBarsEnabled = false;
        browser.Navigating += new WebBrowserNavigatingEventHandler(Navigating);
        browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
    }
 
    #endregion        
 
    #region Public methods
 
    public void Create(string url)
    {
        imgsize = null;
        browser.Navigate(url);
    }
    public void Create(string url, Size imgsz)
    {
        this.imgsize = imgsz;
        browser.Navigate(url);
    }
    void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        Rectangle body = browser.Document.Body.ScrollRectangle;
        Rectangle docRectangle = new Rectangle()
        {
            Location = new Point(0, 0),
            Size = new Size(body.Width > screen.Width ? body.Width : screen.Width,
                body.Height > screen.Height ? body.Height : screen.Height)
        };
        browser.Width = docRectangle.Width;
        browser.Height = docRectangle.Height;
        Rectangle imgRectangle;
        if (imgsize == null)
            imgRectangle = docRectangle;
        else
            imgRectangle = new Rectangle()
            {
                Location = new Point(0, 0),
                Size = imgsize.Value
            };
        Bitmap bitmap = new Bitmap(imgRectangle.Width, imgRectangle.Height);
        IViewObject ivo = browser.Document.DomDocument as IViewObject;
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            IntPtr hdc = g.GetHdc();
            ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref imgRectangle, 
                ref docRectangle, IntPtr.Zero, 0);
            g.ReleaseHdc(hdc);
        }
        bitmap.Save("C:/Temp.bmp");
        if (HtmlImageCapture != null) HtmlImageCapture(this, browser.Url, bitmap);
    }
    void Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
            
    }
 
    #endregion
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...