I want to take a screenshot of an HTML div tag when the web application is running on the client-side and store the image on the server. I read many articles on html2canvas and tried to implement the code however the image that is getting saved on the server is turning up all black. I tried by setting the background color to white but then it is turning up all white. In short, the image is blank. So any help will be appreciated. Thank you.
Html
<div id="viewer-container" class="sticky-top" style="height:70%;top:25%;width:41%;left:38%;">
<canvas id="viewer-canvas"></canvas>
</div>
Js
html2canvas(document.getElementById("viewer-container"), {
onrendered: function (canvas) {
var image = canvas.toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
$.ajax({
type: "POST",
url: '@Url.Action("TakeImage", "Home")',
data: { base64data: image },
success: function (result) { alert(result); }
});
}
})
Controller
[HttpPost]
public ActionResult TakeImage()
{
if (Request.Form["base64data"] != null)
{
string image = Request.Form["base64data"].ToString();
byte[] data = Convert.FromBase64String(image);
var path = Path.Combine(Server.MapPath("~/BIM"), "testimage.png");
System.IO.File.WriteAllBytes(path, data);
}
return View();
}