我有一个Panel控件,在面板中我对每个跨度都有sapn控件我有一些彩色文本,我想保存为图像,是否可能?
如果是,如何?
我正在使用asp.net和c#。
<asp:Panel ID="wordcloud2" runat="server">
<span data-weight="43">Nike</span>
<span data-weight="41">Reebok</span>
<span data-weight="60">Adidas</span>
<span data-weight="39">Roush</span>
<span data-weight="17">Bata</span>
<span data-weight="35">Lunar's</span>
<span data-weight="33">VKC</span>
<span data-weight="31">Lee cooper</span>
</asp:Panel>
请告诉我。
提前感谢你
答案 0 :(得分:1)
这是Mudassar Ahmed Khan网站上使用HTML5 Canvas,div
和table
元素与Html2Canvas
库结合的精彩教程。
检查出来:
<强>更新强>
我将教程与您的要求结合起来。我还必须更新对javascript文件(cdn's)的引用:
<强> PanelToImage.aspx 强>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PanelToImage.aspx.cs" Inherits="PanelToImage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js" ></script>
<script type="text/javascript" src="https://github.com/niklasvh/html2canvas/releases/download/v1.0.0-alpha.8/html2canvas.min.js"></script>
</head>
<body>
<script type="text/javascript">
function ConvertToImage(btnExport) {
html2canvas($("#dvTable")[0]).then(function (canvas) {
var base64 = canvas.toDataURL();
$("[id*=hfImageData]").val(base64);
__doPostBack(btnExport.name, "");
});
return false;
}
</script>
<form id="form1" runat="server">
<div id="dvTable" style="width: 340px; background-color: White; padding: 5px">
<asp:Panel ID="wordcloud2" runat="server">
<span data-weight="43">Nike</span>
<span data-weight="41">Reebok</span>
<span data-weight="60">Adidas</span>
<span data-weight="39">Roush</span>
<span data-weight="17">Bata</span>
<span data-weight="35">Lunar's</span>
<span data-weight="33">VKC</span>
<span data-weight="31">Lee cooper</span>
</asp:Panel>
</div>
<br />
<asp:HiddenField ID="hfImageData" runat="server" />
<asp:Button ID="btnExport" Text="Export to Image" runat="server" UseSubmitBehavior="false" OnClick="ExportToImage" OnClientClick="return ConvertToImage(this)" />
</form>
</body>
</html>
PanelToImage.cs(代码隐藏)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class PanelToImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ExportToImage(object sender, EventArgs e)
{
string base64 = Request.Form[hfImageData.UniqueID].Split(',')[1];
byte[] bytes = Convert.FromBase64String(base64);
Response.Clear();
Response.ContentType = "image/png";
Response.AddHeader("Content-Disposition", "attachment; filename=HTML.png");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
}
}
<强>结果:强>