Created
November 15, 2011 05:38
-
-
Save JoshClose/1366260 to your computer and use it in GitHub Desktop.
Programmatically Taking a Full Web Page Screenshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Runtime.InteropServices; | |
using System.Windows.Forms; | |
public class ScreenShot | |
{ | |
[ComImport] | |
[InterfaceType( ComInterfaceType.InterfaceIsIUnknown )] | |
[Guid( "0000010d-0000-0000-C000-000000000046" )] | |
private interface IViewObject | |
{ | |
[PreserveSig] | |
int Draw( [In] [MarshalAs( UnmanagedType.U4 )] int dwDrawAspect, int lindex, IntPtr pvAspect, | |
[In] /*tagDVTARGETDEVICE*/ IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, | |
[In] /*COMRECT*/ Rectangle lprcBounds, [In] /*COMRECT*/ IntPtr lprcWBounds, IntPtr pfnContinue, | |
[In] int dwContinue ); | |
[PreserveSig] | |
int GetColorSet( [In] [MarshalAs( UnmanagedType.U4 )] int dwDrawAspect, int lindex, IntPtr pvAspect, | |
[In] /*tagDVTARGETDEVICE*/ IntPtr ptd, IntPtr hicTargetDev, [Out] /*tagLOGPALETTE*/ 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*/ IntPtr pAdvSink ); | |
void GetAdvise( [In] [Out] [MarshalAs( UnmanagedType.LPArray )] int[] paspects, | |
[In] [Out] [MarshalAs( UnmanagedType.LPArray )] int[] advf, | |
[In] [Out] [MarshalAs( UnmanagedType.LPArray )] /*IAdviseSink[]*/ IntPtr[] pAdvSink ); | |
} | |
public static Bitmap Create( string url ) | |
{ | |
using( var webBrowser = new WebBrowser() ) | |
{ | |
webBrowser.ScrollBarsEnabled = false; | |
webBrowser.ScriptErrorsSuppressed = true; | |
webBrowser.Navigate( url ); | |
while( webBrowser.ReadyState != WebBrowserReadyState.Complete ) | |
{ | |
Application.DoEvents(); | |
} | |
webBrowser.Width = webBrowser.Document.Body.ScrollRectangle.Width; | |
webBrowser.Height = webBrowser.Document.Body.ScrollRectangle.Height; | |
var bitmap = new Bitmap( webBrowser.Width, webBrowser.Height ); | |
var graphics = Graphics.FromImage( bitmap ); | |
var hdc = graphics.GetHdc(); | |
var rect = new Rectangle( 0, 0, webBrowser.Width, webBrowser.Height ); | |
var viewObject = (IViewObject)webBrowser.Document.DomDocument; | |
viewObject.Draw( 1, -1, (IntPtr)0, (IntPtr)0, (IntPtr)0, hdc, rect, (IntPtr)0, (IntPtr)0, 0 ); | |
graphics.ReleaseHdc( hdc ); | |
return bitmap; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Joshclose,
its is really useful in our requirement, but i am getting only part of the web page (say, 35-40% from left top).
can you suggest me, how can i get the complete page. thanks in advance. [email protected]