DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Culture and Methodologies
  3. Agile
  4. Capturing Webpage Screenshot with Html2Canvas.js

Capturing Webpage Screenshot with Html2Canvas.js

Ayobami Adewole user avatar by
Ayobami Adewole
·
Jan. 31, 15 · Tutorial
Like (0)
Save
Tweet
Share
42.80K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction


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


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 />
<p>
Some text that would be captured as screenshot
</p>
</div>
<div class="space">
</div>
@Html.Hidden("capturedShot")
<div>
<input type="button" id="btnCapture" value="Capture" />
<input type="button" id="btnDisplay" value="View Image" />
</div>

}

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

<script>
$(document).ready(function () {
$('#btnCapture').on("click", function () {
captureAndUpload();
});

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

function captureAndUpload() {
$('body').html2canvas({
onrendered: function (canvas) {
//Set hidden field's value to image data (base-64 string)
$('#capturedShot').val(canvas.toDataURL("image/png"));
document.getElementById("form").submit();
}
});
}

function captureAndDisplay() {
$('body').html2canvas({
onrendered: function (canvas) {
var myImage = canvas.toDataURL("image/png");
window.open(myImage);
}
});
}
});
</script>

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

 [HttpPost]
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);
System.IO.File.WriteAllBytes(path,uploadedImage);
return View();
}
JavaScript library Web application ASP.NET MVC Form (document) Upload Requirement Java (programming language)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Best Ways to Level Up as a Developer
  • Create Spider Chart With ReactJS
  • Is DevOps Dead?
  • What Is API-First?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: