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 { [return: MarshalAs(UnmanagedType.I4)] [PreserveSig] int Draw( [MarshalAs(UnmanagedType.U4)] UInt32 dwDrawAspect, int lindex, IntPtr pvAspect, [In] IntPtr 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([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hicTargetDev, [Out] IntPtr ppColorSet); [PreserveSig] int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze); [PreserveSig] int Unfreeze([In, MarshalAs(UnmanagedType.U4)] int dwFreeze); void SetAdvise([In, MarshalAs(UnmanagedType.U4)] int aspects, [In, MarshalAs(UnmanagedType.U4)] int advf, [In, MarshalAs(UnmanagedType.Interface)] IAdviseSink pAdvSink); void GetAdvise([In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects, [In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf, [In, Out, MarshalAs(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 }
Wednesday, August 11, 2010
How to convert Html page to a Image using WebBrowser - Windows Forms
Subscribe to:
Post Comments (Atom)
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...
-
Why we need asynchronous tasks? Execute a time consuming operations in parallel to the CLR thread which execute the request If you have ...
-
Demo: I was thinking a way to show images before actually uploading them to server. I would say to preview images using javascript. Obv...
No comments:
Post a Comment