Search code examples
c#asp.netimagepanel

ASP.Net Panel to Image


I have a control of Panel and within panel I have sapn control on each span I have some colorfull text, all I want to save as image, is it possible?

If yes how?

I am using asp.net and 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> 

Please do let me know.

Thanking you in advance


Solution

  • Here's a great tutorial from Mudassar Ahmed Khan's website using HTML5 Canvas, div and table elements combined with Html2Canvas library.

    Check it out:

    https://www.aspsnippets.com/Articles/Convert-Export-HTML-DIV-or-Table-to-Image-using-HTML-Canvas-in-ASPNet-using-C-and-VBNet.aspx

    UPDATE

    I combined the tutorial with your requirement. I had to also update the references to the javascript files (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 (code behind)

    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();
        }
    }
    

    Results:

    WebApp

    Image result