Over a million developers have joined DZone.

Capturing Webpage Screenshot with Html2Canvas.js

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.


Recently, I was to implement a web application requirement that stipulates that the screenshot of a web page containing form filled by users be captured, as the form is being submitted. While this may seems trivial, a lot of intricacies is involved in building an image representation of the page as it is shown on the browser. 


Html2canvas.js is a javascript library that provides functionality for capturing a screenshot, the screenshot is built based on the information available on the page. The script traverses the DOM and builds a representation of the page alongside the images and the css styles present on the page, but it doesn’t render flash, java applets and iframe. The html2canvas library can be downloadedhere 

The example in this blog post uses Asp.net MVC and it contains code to capture the screenshot and display the captured image on the browser and upload the image to the controller action method for saving as a png file on the server. 

Add reference to the required javascript libraries at the header of the page 

<script src="~/Js/jquery-1.9.1.js"></script>
<script src="~/Js/html2canvas.js"></script>
<script src="~/Js/jquery.plugin.html2canvas.js"></script>

Below is the Razor and Html tags for the page 

@using(Html.BeginForm("ScreenShot", "Home", FormMethod.Post, new { @id = "form" }))
<div class="designPane">
<br />
<br />
Some text that would be captured as screenshot
<div class="space">
<input type="button" id="btnCapture" value="Capture" />
<input type="button" id="btnDisplay" value="View Image" />


The javascript code for capturing the screenshot and displaying the captured image and that for uploading to the server. 

$(document).ready(function () {
$('#btnCapture').on("click", function () {

$('#btnDisplay').on("click", function () {

function captureAndUpload() {
onrendered: function (canvas) {
//Set hidden field's value to image data (base-64 string)

function captureAndDisplay() {
onrendered: function (canvas) {
var myImage = canvas.toDataURL("image/png");

The controller action method below accepts the uploaded screenshot and writes it to a file. 

public ActionResult ScreenShot(FormCollection formCollection)
string screenShot = formCollection["capturedShot"];
//remove the image header details
string trimmedData = screenShot.Replace("data:image/png;base64,","");

//convert the base 64 string image to byte array
byte[] uploadedImage=Convert.FromBase64String(trimmedData);

//the byte array can be saved into database or on file system
//saving the image on the server
string fileName=Guid.NewGuid()+".png";
string path=Server.MapPath("~/App_Data/"+fileName);
return View();

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of Ayobami Adewole, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}