Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#

Partial Snapshot of a Web Page Using HTML2canvas

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
6 Jun 2016CPOL4 min read 20.4K   6   1
Partial snapshot of a web page using HTML2canvas

Introduction

Any web page would consist of multiple UI components like header, footer, left menu, multiple contents, etc. When I say partial snapshot, I meant snapshot of one or more specific page content like a specific graph or chart appearing on the main page, a table, etc.

Background

Sometimes, we may have a business requirement of allowing print of a specific page content, copy the page content in a mail and send to recipient, etc. it is usually easier to find code to take the screen shot of the complete page which often includes unwanted content like left menu, headers, etc. But it is difficult to figure out the code which can allow partial snapshot (at least I found it difficult when trying to implement this requirement in my application). This blog would help in writing a generic piece of code which can be used in multiple scenarios related to partial snapshot.

Using the Code

The key is the JavaScript library html2canvas. We need to add the reference to this file wherever we need the functionality of partial snapshot. If the requirement is for all the pages, then simply add the reference in some master file.

The basic principle is to partially convert the html into a canvas html control and then use it for requested functionality.

JavaScript
<script type="text/javascript" src="Scripts/html2canvas.js"></script>

Next, I would show the sample code of capturing snapshot. This sample code is supposed to be executed on page load, but we can easily bind it to a button click event as well.

JavaScript
 <script language="javascript">
        $(document).ready(function () {
            html2canvas($("#testdiv"), {
                onrendered: function (canvas) {
                    var extra_canvas = document.createElement("canvas");
                    extra_canvas.setAttribute('width', 300);
                    extra_canvas.setAttribute('height', 300);
                    var ctx = extra_canvas.getContext('2d');
                    ctx.drawImage(canvas, 0, 0, 300, 300, 0, 0, 300, 300);
                    var myImage = extra_canvas.toDataURL("image/png");
                    document.getElementById("img").src = myImage;
                    $("[id*=hfImageData]").val(myImage);
                    window.open(myImage);
                }
            });
        });
</script>

#

Code

Explanation

  1.  

<script language="javascript">

 

  1.  

$(document).ready(function () {

This line ensures that code would be executed on page load.

  1.  

html2canvas($("#testdiv"), {

The div which contains the content which snapshot needs to be taken. It is hardcoded here but can be easily passed as the parameter. All the operation which would happen within this function is performed on the content in this div.

  1.  

onrendered: function (canvas) {

The main function which would do all the work.

  1.  

var extra_canvas = document.createElement("canvas");

We would have directly used the default canvas control but I face the problem that in that case, the size of the image was not coming correct. Hence introducing the extra logic of custom canvas control

  1.  

extra_canvas.setAttribute('width', 300);

Setting the width of the custom canvas.

  1.  

extra_canvas.setAttribute('height', 300);

Setting the height of the custom canvas.

  1.  

var ctx = extra_canvas.getContext('2d');

Setting the context as 2 dimensional and getting it in a variable

  1.  

ctx.drawImage(canvas, 0, 0, 300, 300, 0, 0, 300, 300);

Now drawing the image and setting the various offsets, width and height. Make sure the width and height is the same as set in the step 6 and 7.

  1.  

var myImage = extra_canvas.toDataURL("image/png");

Finally, getting the image from the custom canvas and now we are done. Below lines show multiple usage of this image

  1.  

document.getElementById("img").src = myImage;

We can set the image to some other div

  1.  

$("[id*=hfImageData]").val(myImage);

We can get image as bytes array in a hidden field. This is a very important use. I will show later how.

  1.  

window.open(myImage);

We can open image in a new window and use for printing.

  1.  

}

 

  1.  

});

 

  1.  

});

 

  1.  

</script>

 

This piece of code can be made generic and be written in some common JavaScript file to be used across the application or you may copy this piece of code in a specific file and use it. Logic would be the same in every case. Only the div id and dimension of image would change.

So just with these few lines of code and a reference to htmltocanvas js file, we can implement this typical functionality.

  • Line 2: Ensures that code would be executed on page load. This can be easily replaced with a button click event as follows:
JavaScript
$(document).ready(function () { 

$('#button').on("click", function () {            
captureSnapshot(); 
}); 

function captureSnapshot () {…….}

Usage of Images Bytes in Hidden Field

We are not over yet. There is more. I have shown above that we can get the image in a bytes array in a hidden field. We can store this hidden field value in database and use it later as well for some audit, snapshot, etc.

Please see the sample code below for accessing the hidden field and converting it to bytes array

1

string base64 = Request.Form[hfImageData.UniqueID].Trim().Split(',')[1];

Simply splitting the hidden field content by comma (,) and then getting the second value.

2

byte[] bytes = Convert.FromBase64String(base64);

Using the .NET API to convert into bytes array

3

MemoryStream stream = new MemoryStream(bytes);

We can use bytes array in multiple ways like we can store it in database. In my example, I am converting it to a stream and then I can use it form image. I could create a physical image file as well from this stream and store it in an appropriate place.

This is the additional functionality which can be achieved in association to the html to canvas code. I personally feel this is a very good addition to the core logic if used properly.

This is all you need to know to implement this partial snapshot functionality. I hope this would be useful for you in your work.

References

History

  • 6th June, 2016: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionblank window Pin
Member 1504203821-Mar-21 1:47
Member 1504203821-Mar-21 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.