Change HTML table contents to image format
Source: Darin Dimitrov's Answer
You will need first a rendering engine capable of handling HTML and
optionally javascript and css (in case you want to support them).
Using a WebBrowser control could be done but there might be
better ways.
There are few other options also, Refer the following links:
Html table (text) to image using C#
How to convert block of html to an image (e.g. jpg) in asp.net
Convert a HTML Control (Div or Table) to an image using C#
render HTML (convert to bitmap)
code snippet:
public Bitmap GenerateScreenshot(string url)
{
// This method gets a screenshot of the webpage
// rendered at its full size (height and width)
return GenerateScreenshot(url, -1, -1);
}
public Bitmap GenerateScreenshot(string url, int width, int height)
{
// Load the webpage into a WebBrowser control
WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
// Set the size of the WebBrowser control
wb.Width = width;
wb.Height = height;
if (width == -1)
{
// Take Screenshot of the web pages full width
wb.Width = wb.Document.Body.ScrollRectangle.Width;
}
if (height == -1)
{
// Take Screenshot of the web pages full height
wb.Height = wb.Document.Body.ScrollRectangle.Height;
}
// Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
wb.Dispose();
return bitmap;
}
Convert html table/chart element to image
You could try HTML2PDF converter. See https://www.html2pdf.fr
Or HTML2PS converter. See http://user.it.uu.se/%7Ejan/html2ps.html
ImageMagick can use the latter, if installed to do the conversion. See HTML section at http://www.imagemagick.org/script/formats.php
Add image into html table dynamically
Try this:
HtmlTable tbProductImage = new HtmlTable();
HtmlTableRow trImageRow = new HtmlTableRow();
for (int j = 0; j < columnCount; j++)
{
if (filteredFileList.Count != 0)
{
HtmlTableCell tdImageCell = new HtmlTableCell();
Image imageProduct = new Image();
imageProduct.ID = "id";
imageProduct.ImageUrl = "url";
tdImageCell.Controls.Add(imageProduct);
trImageRow.Cells.Add(tdImageCell);
}
}
tbProductImage.Rows.Add(trImageRow);
Convert HTML string to image
Thanks all for your responses. I used HtmlRenderer external dll (library) to achieve the same and found below code for the same.
Here is the code for this
public void ConvertHtmlToImage()
{
Bitmap m_Bitmap = new Bitmap(400, 600);
PointF point = new PointF(0, 0);
SizeF maxSize = new System.Drawing.SizeF(500, 500);
HtmlRenderer.HtmlRender.Render(Graphics.FromImage(m_Bitmap),
"<html><body><p>This is some html code</p>"
+ "<p>This is another html line</p></body>",
point, maxSize);
m_Bitmap.Save(@"C:\Test.png", ImageFormat.Png);
}
C# How Do I Load an image into a code-behind generated table?
You can add an image to a Cell just as you would add a Cell to a Row.
HtmlImage image = new HtmlImage();
image.Src ="/studentPics/" + sep.si.studentPicID;
tdPic.Controls.Add(image);
However as I look at your code, a GridView would be a better choice I think.
You can bind a List directly to the GridView.
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
if (!Page.IsPostBack)
{
GridView1.DataSource = lsep;
GridView1.DataBind();
}
Show Binary Image in Html Table from Code Behind
Because you create a new asp.net image control, and try to use the object as string.
These lines must be remove...
System.Web.UI.WebControls.Image imgNew = new System.Web.UI.WebControls.Image();
imgNew.ImageUrl = "data:image/png;base64," + base64String;
htmlTable.Append("<td>" + imgNew + "</td>");
if you like to render it as string, must be change as:
htmlTable.AppendFormat("<td><img src=\"data:image/png;base64,{0}\"></td>", base64String);
Please note, I do not know if the rest of your code is bug free...
Related Topics
How to Get First Record in Each Group Using Linq
How Does Wcf Deserialization Instantiate Objects Without Calling a Constructor
Copy Files from Resources/Streamingassets to Application.Persistentdatapath Upon Installation
Is There a Standard Way to Encode a .Net String into JavaScript String for Use in Ms Ajax
MVC Which Submit Button Has Been Pressed
Linq - Left Join on Multiple (Or) Conditions
How to Cast String to Int. Error Msg: Input String Was Not in a Correct Format
Debugging Automatic Properties
How to Reference Embedded Images from CSS
Xamarin iOS Memory Leaks Everywhere
Calling a C# Library from Python
Order of Serialized Fields Using JSON.Net