How to convert response stream to an image?
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(bitImag开发者_运维问答e);
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();
}
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
精彩评论