0

Please note this link Render HTML as an Image is not helpful.

In previously asked question answered said they don't get what I want to do exactly so here's is the full code also.

I simply want that instead of a TABLES 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();



}
halfer
  • 19,824
  • 17
  • 99
  • 186
user287745
  • 3,071
  • 10
  • 56
  • 99
  • Well, for one, your code is entirely missing the part where you load an HTML rendering engine and use that to render the content. – Matti Virkkunen Aug 29 '10 at 11:42
  • public string RenderControl(Control ctrl) this does the work, its able to produce a doc file using thos returned string,. please please help.. what am i missing give links – user287745 Aug 29 '10 at 12:09
  • It doesn't produce "a doc file", it just produces HTML. Rendering HTML to an image is a very daunting task. Just look at how complicated web browsers are. – Matti Virkkunen Aug 29 '10 at 12:17
  • @user287745: user @Matti is correct, `public string RenderControl(Control ctrl)` does not create a *picture* of HTML, it creates an in-memory string of *HTML code*. Creating an image of that means rendering the HTML code using one of the many browsers, capturing it as a screenshot and then use that image. – Abel Aug 29 '10 at 14:29
  • 1
    Possible duplicate of [Render HTML as an Image](http://stackoverflow.com/questions/334532/render-html-as-an-image) – jessehouwing Dec 29 '16 at 19:14

1 Answers1

1

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

Community
  • 1
  • 1
Abel
  • 56,041
  • 24
  • 146
  • 247