# Turning HTML Documents into images...the code

<datetime class="hidden">2004-06-30T00:00</datetime>
<!-- category -- mostlylucidcouk, Imported -->

Well, after a bit of playing I've got something working now, I'm no Win32 coder but this seems to work:

using System; 

using System.Drawing; 

using System.Drawing.Imaging; 

using System.Collections; 

using System.ComponentModel; 

using System.Windows.Forms; 

using System.Data; 

using mshtml; 

using System.Runtime.InteropServices; 

using Microsoft.Win32; 

namespace ThumbMaker 

{    

/// &lt;summary&gt; 

/// Summary description for Form1. 

/// &lt;/summary&gt; 

public class Form1 : System.Windows.Forms.Form 

{ 

private AxSHDocVw.AxWebBrowser axWebBrowser1; 

/// &lt;summary&gt; 

/// Required designer variable. 

/// &lt;/summary&gt; 

private System.ComponentModel.Container components = null ; 

private const string urlStr = "http://news.bbc.co.uk/" ; 

public Form1() 

{ 

// 

// Required for Windows Form Designer support 

// 

InitializeComponent(); 

object o = null ; 

this .Visible= false ; 

axWebBrowser1.Navigate(urlStr, ref o, ref o, ref o, ref o); 

} 

const int WM\_PAINT = 0x000F; 

const int WM\_PRINTCLIENT = 0x0318; 

const int WM\_ERASEBKGND = 0x0014; 

const int WM\_PRINT = 0x0317; 

const int PRF\_CHILDREN   = 0x00000010; 

const int PRF\_CLIENT   = 0x00000004; 

public void GrabWindow() 

{ 

IntPtr hWnd = this .Handle; 

IntPtr hBmp; 

Bitmap MyBitmap= null ; 

IntPtr hDCMem = GDI32.CreateCompatibleDC((IntPtr) null ); 

IntPtr hDC = User32.GetWindowDC(hWnd); 

hBmp = GDI32.CreateCompatibleBitmap(hDC,(IntPtr)axWebBrowser1.Width ,(IntPtr)axWebBrowser1.Height); 

User32.ReleaseDC(hWnd, hDC); 

IntPtr hOld = GDI32.SelectObject(hDCMem, hBmp); 

User32.SendMessage(hWnd, WM\_PRINT, hDCMem, new IntPtr(PRF\_CLIENT | PRF\_CHILDREN)); 

GDI32.SelectObject(hDCMem, hOld); 

GDI32.DeleteObject(hDCMem); 

MyBitmap = Bitmap.FromHbitmap(hBmp); 

MyBitmap.Save( "test.png" ,ImageFormat.Png); 

this .Close(); 

} 

/// &lt;summary&gt; 

/// Clean up any resources being used. 

/// &lt;/summary&gt; 

protected override void Dispose( bool disposing ) 

{ 

if ( disposing ) 

{ 

if (components != null ) 

{ 

components.Dispose(); 

} 

} 

base .Dispose( disposing ); 

} 

#region Windows Form Designer generated code 

/// &lt;summary&gt; 

/// Required method for Designer support - do not modify 

/// the contents of this method with the code editor. 

/// &lt;/summary&gt; 

private void InitializeComponent() 

{ 

System.Resources.ResourceManager resources = new System.Resources.ResourceManager( typeof (Form1)); 

this .axWebBrowser1 = new AxSHDocVw.AxWebBrowser(); 

((System.ComponentModel.ISupportInitialize)( this .axWebBrowser1)).BeginInit(); 

this .SuspendLayout(); 

// 

// axWebBrowser1 

// 

this .axWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill; 

this .axWebBrowser1.Enabled = true ; 

this .axWebBrowser1.Location = new System.Drawing.Point(0, 0); 

this .axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject( "axWebBrowser1.OcxState" ))); 

this .axWebBrowser1.Size = new System.Drawing.Size(1024, 768); 

this .axWebBrowser1.TabIndex = 0; 

this .axWebBrowser1.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2\_DocumentCompleteEventHandler( this .axWebBrowser1\_DocumentComplete); 

// 

// Form1 

// 

this .AutoScale = false ; 

this .AutoScaleBaseSize = new System.Drawing.Size(5, 13); 

this .ClientSize = new System.Drawing.Size(1024, 768); 

this .Controls.Add( this .axWebBrowser1); 

this .FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 

this .Name = "Form1" ; 

this .ShowInTaskbar = false ; 

this .Text = "Form1" ; 

((System.ComponentModel.ISupportInitialize)( this .axWebBrowser1)).EndInit(); 

this .ResumeLayout( false ); 

} 

#endregion 

/// &lt;summary&gt; 

/// The main entry point for the application. 

/// &lt;/summary&gt; 

[STAThread] 

static void Main() 

{ 

Application.Run( new Form1()); 

} 

private void axWebBrowser1\_DocumentComplete( object sender, AxSHDocVw.DWebBrowserEvents2\_DocumentCompleteEvent e) 

{      

GrabWindow(); 

} 

} 

class GDI32 

{ 

[DllImport( "GDI32.dll" )] 

public static extern bool BitBlt(IntPtr hdcDest,IntPtr nXDest,IntPtr nYDest, 

IntPtr nWidth,IntPtr nHeight,IntPtr hdcSrc, 

IntPtr nXSrc,IntPtr nYSrc,IntPtr dwRop); 

[DllImport( "GDI32.dll" )] 

public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,IntPtr nWidth, 

IntPtr nHeight); 

[DllImport( "GDI32.dll" )] 

public static extern IntPtr CreateCompatibleDC(IntPtr hdc); 

[DllImport( "GDI32.dll" )] 

public static extern bool DeleteDC(IntPtr hdc); 

[DllImport( "GDI32.dll" )] 

public static extern bool DeleteObject(IntPtr hObject); 

[DllImport( "GDI32.dll" )] 

public static extern IntPtr GetDeviceCaps(IntPtr hdc,IntPtr nIndex); 

[DllImport( "GDI32.dll" )] 

public static extern IntPtr SelectObject(IntPtr hdc,IntPtr hgdiobj); 

} 

class User32 

{ 

[DllImport( "User32.dll" )] 

public static extern IntPtr GetDesktopWindow(); 

[DllImport( "User32.dll" )] 

public static extern IntPtr GetWindowDC(IntPtr hWnd); 

[DllImport( "User32.dll" )] 

public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC); 

[DllImport( "User32.dll" )] 

public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); 

} 

}