Take print screen and save image automatically in C #

3

Is it possible to take a print screen of the active page of my web system and save that image somewhere using C #?

    
asked by anonymous 28.05.2014 / 22:04

1 answer

4

It is possible, with html2canvas , with some settings.

Example:

1) WebForms :

On your WebForms page, place the html2canvas references and jQuery with following Javascript code:

<script src="/Scripts/jquery-1.8.2.js"></script>
<script src="/Scripts/html2canvas.js"></script>
<script type="text/javascript">
CopyBitmap = function () {
    html2canvas(document.body, {
        onrendered: function (canvas) {
            var dataUrl = canvas.toDataURL();
            $.post('Handler1.ashx', { 'data': dataUrl }, function (data) {
                if (data == "1") {
                    alert('Imagem enviada com sucesso');
                }
            });
        }
    });
};
</script>

Call CopyBitmap on a button like this:

<button type="button" onclick="CopyBitmap();">Copiar</button>

This function will take canvas from a base64 of the image that we are going to transfer in Ajax process to a file Handler1.ashx being responsible for the conversions from that base64 to image and writing to the Fotos directory of the application:

public class Handler1 : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        String data = context.Request["data"].ToString();            
        String baseImg = Regex.Match(data, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
        Byte[] baseBytes = Convert.FromBase64String(baseImg);
        System.IO.File.WriteAllBytes(context.Server.MapPath("~/Fotos/1-1.png") , baseBytes);            
        context.Response.ContentType = "text/plain";
        context.Response.Write("1");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

2) MVC Web :

In MVC C # Web, it is much more practical in the Imagem method created in the Controler Home to receive and convert the required images by writing the image to a folder on the server (in this case in the Fotos folder). >

View

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index - MVC</h2>
<h4>WEB MVC C#</h4>
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/html2canvas.js"></script>
<script type="text/javascript">
        CopyBitmap = function () {
            html2canvas(document.body, {
                onrendered: function (canvas) {
                    var dataUrl = canvas.toDataURL();
                    $.post('/Home/Imagem', { 'data': dataUrl }, function (data) {
                        if (data == "1") {
                            alert('Imagem enviada com sucesso');
                        }
                    });
                }
            });
        };
  </script>
<button type="button" onclick="CopyBitmap();">Copiar</button>

Action:

public ActionResult Index()
{
    return View();
}
[HttpPost]
public JsonResult Imagem(String data)
{           
    String baseImg = Regex.Match(data, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
    Byte[] baseBytes = Convert.FromBase64String(baseImg);
    System.IO.File.WriteAllBytes(Server.MapPath("~/Fotos/1-2-3.png"), baseBytes);
    return Json("1");
}
    
29.05.2014 / 02:59