views:

40

answers:

1

please note this link http://stackoverflow.com/questions/334532/render-html-as-an-image is not help full

in previously asked question answeres said they dont get what i want to do exactly so heres is the full code also

i simply want that instead of a TABLESe i rendered an image( of the content ) on the page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Text;
using System.Data;
using System.Drawing;


public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{

    System.Web.UI.WebControls.Panel panelmain = new System.Web.UI.WebControls.Panel();
    System.Web.UI.WebControls.Literal abc = new System.Web.UI.WebControls.Literal();
    abc.Text = "as<br/>dasdas<br/>dasdad";


    DataSet ds = new DataSet();
    DataTable dt;
    DataRow dr;
    DataColumn idCoulumn;
    DataColumn nameCoulumn;

    dt = new DataTable();
    idCoulumn = new DataColumn("ID", Type.GetType("System.Int32"));
    nameCoulumn = new DataColumn("Name", Type.GetType("System.String"));
    dt.Columns.Add(idCoulumn);
    dt.Columns.Add(nameCoulumn);
    dr = dt.NewRow();
    dr["ID"] = 1;
    dr["Name"] = "Name1";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["ID"] = 2;
    dr["Name"] = "Name2";
    dt.Rows.Add(dr);

    ds.Tables.Add(dt);

    System.Web.UI.WebControls.GridView grid1 = new 
    System.Web.UI.WebControls.GridView();
    grid1.DataSource = ds;
    grid1.DataBind();

    panelmain.Controls.Add(abc);
    panelmain.Controls.Add(grid1);

    string toexport;
    toexport = RenderControl(panelmain);

    Byte[] bitmapData = new Byte[100000];
    bitmapData = Convert.FromBase64String(FixBase64ForImage(toexport));
    System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData);
    Bitmap bitImage = new Bitmap((Bitmap)Image.FromStream(streamBitmap));

    Response.ContentType = "image/gif";
    Response.AppendHeader("Content-Disposition", "inline;filename=tm.gif");
    Response.BufferOutput = true;
    Response.Charset = "utf-8";
    Response.Write(bitImage);
    Response.End();

    }



public string FixBase64ForImage(string Image)
{
    System.Text.StringBuilder sbText = new System.Text.StringBuilder(Image, Image.Length);

    sbText.Replace("\r\n", String.Empty);

    sbText.Replace(" ", String.Empty);

    return sbText.ToString();
}



public string RenderControl(Control ctrl)
{
    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);

    ctrl.RenderControl(hw);
    Response.Write(sb);

    return sb.ToString();



}
+1  A: 

In your previous question you already received an answer (by me), referring to another SO thread where this question was answered. The second answer in that thread links to another page on the web where the whole process of starting a WebBrowser object, rending a page, capturing the image, converting that to your preferred image file format, saving it locally is explained. Not a straightforward task (expect to spend some hours), but not too hard either.

To repeat the answer in the referred to question, just have a look at this WinCustomize.com article, download the source and experiment

Abel