一.引用js脚本包,html2canvas.js为截图包,使用非常简单
<script src="~/Content/html2canvas.js"></script>,到网上下载
二.要保存的html元素
<body> <div id="header"></div> <div id="main"> <div id="sharecontent"> <div id="sharedownload"> <img src="/@ViewBag.BackImg" /> </div> <div id="shareqrcode"> <div> <img src="/@ViewBag.RQcode" /> <p>@@<span>@(ViewBag.agentName)的大V号主页</span></p> <p>MB:@ViewBag.Phone</p> </div> </div> </div> <div id="previewImage" style="display:none;"></div> </div> <div id="footer"></div> </body>
三.js代码
var element = $("#sharecontent"); //要截图的div var getCanvas; $("#SaveDownload").click(function () //点击"保存下载控件" { //newName,保存图片的新名称,防止重复 var newName = new Date().getTime().toString(16) + Math.ceil(Math.random() * 10000).toString(16); html2canvas(element, { onrendered: function (canvas) { //$("#previewImage").append(canvas); //可以,追加到界面上预览 //getCanvas = canvas; var imgageData = canvas.toDataURL("image/png");//把canvas转为base64图片 //下面的代码是,给a标签的href链接图片地址,然后下载 var newData = imgageData.replace(/^data:image/png/, "data:application/octet-stream"); $("#btn-SaveDownload").attr("download", newName+".png").attr("href", newData); $("#btn-SaveDownload")[0].click(); } }); })