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
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private AxSHDocVw.AxWebBrowser axWebBrowser1;
/// <summary>
/// Required designer variable.
/// </summary>
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();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if (components != null )
{
components.Dispose();
}
}
base .Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
/// <summary>
/// The main entry point for the application.
/// </summary>
[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);
}
}
© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.