<script src="~/Js/jquery-1.9.1.js"></script> <script src="~/Js/html2canvas.js"></script> <script src="~/Js/jquery.plugin.html2canvas.js"></script>
@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> }
<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>
[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(); }
Share this page on
1431
People Like(s) This Page
Permalink
comments powered by Disqus