What I am trying to do is to capture an image and then save it on the server side. So far, the image was only being saved on the client side.
<script>
html2canvas(document.querySelector("body")).then(canvas => {
var img = canvas.toDataURL("image/png")
var img = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
///location.href = img;
$.ajax({
type: 'POST',
url: "UserHome/UploadImage",
data: '{ "imageData" : "' + img + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert('Image saved successfully !');
}
});
</script>
UserHomeController:
static string path = @"E:\XXXXX\XXXXXXX\";
[WebMethod()]
public static void UploadImage(string imageData)
{
string fileNameWitPath = path + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);//convert from base64
bw.Write(data);
bw.Close();
}
}
}
I have followed this tute: http://www.dotnetfunda.com/articles/show/1662/saving-html-5-canvas-as-image-on-the-server-using-aspnet
Why isn't it saving the image soon as the page is done loading? Although, It is displaying the image in another tab if I uncomment the following line and removing the ajax:
///location.href = img;
Furthermore, I want to know if it is possible to capture the screen even if the browser is minimized? or any way to get that done?
Updated:
So I have successfully saved the screenshot to my server side, but the question remains, how do I capture the screen when the browser is minimized? I run the script and minimize the browser but it still captures the browser image and saves it to the server folder. Is there anyway I can work to capture the image of the screen outside the browser now?