Web Browser Screen Shot

Copying my answer from StackOverflow (edit, now updated) ...

If you are on Windows (with .NET), then you could use Mathematica's NETLink functionality in conjunction with the WebBrowser class to capture a screenshot of a web page:

Needs["NETLink`"]
LoadNETType["System.Drawing.Imaging.ImageFormat", AllowShortContext -> False]
LoadNETType["System.Windows.Forms.WebBrowserReadyState", AllowShortContext -> False]

Options[dotNetBrowserScreenshot] = {Width -> 1024, Height -> Automatic};
dotNetBrowserScreenshot[uri_, OptionsPattern[]] :=
  NETBlock @ Module[{browser, bitmap, tempFile, image, bounds}
  , browser = NETNew["System.Windows.Forms.WebBrowser"]
  ; browser@Width = OptionValue[Width]
  ; browser@ScrollBarsEnabled = False
  ; browser@ScriptErrorsSuppressed = True
  ; browser@Navigate[uri]
  ; tempFile = Close@OpenWrite[]
  ; While[browser@ReadyState =!= System`Windows`Forms`WebBrowserReadyState`Complete
    , Pause[0.05]
    ]
  ; bounds = browser@Document@Body@ClientRectangle
  ; browser@Height = OptionValue[Height] /. Automatic -> bounds@Height
  ; bitmap = NETNew["System.Drawing.Bitmap", browser@Width, browser@Height]
  ; browser@DrawToBitmap[bitmap, bounds]
  ; browser@Dispose[]
  ; bitmap@Save[tempFile, System`Drawing`Imaging`ImageFormat`Png]
  ; bitmap@Dispose[]
  ; image = Import[tempFile, "PNG"]
  ; DeleteFile[tempFile]
  ; image
  ]

Sample use:

sample screenshot

The complete web page can be captured by using Height -> Automatic (which is the default). Note that the screenshot is being displayed at reduced magnification.


There is now a built-in way to do this:

WebImage["http://stackoverflow.com"]

enter image description here

session = StartWebSession["Chrome"];
WebExecute[session, "OpenPage" -> "http://stackoverflow.com"];
WebExecute[session, "CapturePage"]

enter image description here

WebExecute[session, "SetWindowSize" -> {600, 400}]
WebExecute[session, "CapturePage"]

enter image description here