I have a div containing gridview and table, need to convert this div to an Image which will be sent in mail and printed. What are the best possible solutions for it.
// create the HTML to Image converter
HtmlToImage htmlToImageConverter = new HtmlToImage();
// set browser width
htmlToImageConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);
// set HTML Load timeout
htmlToImageConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);
// set whether the resulted image is transparent
htmlToImageConverter.TransparentImage =
(dropDownListImageFormat.SelectedValue == "PNG") ?
checkBoxTransparentImage.Checked : false;
// set triggering mode
// for WaitTime mode set the wait time before convert
switch (dropDownListTriggeringMode.SelectedValue)
{
case "Auto":
htmlToImageConverter.TriggerMode = ConversionTriggerMode.Auto;
break;
case "WaitTime":
htmlToImageConverter.TriggerMode = ConversionTriggerMode.WaitTime;
htmlToImageConverter.WaitBeforeConvert = int.Parse(textBoxWaitTime.Text);
break;
case "Manual":
htmlToImageConverter.TriggerMode = ConversionTriggerMode.Manual;
break;
default:
htmlToImageConverter.TriggerMode = ConversionTriggerMode.Auto;
break;
}
// get the image buffer in the selected image format
byte[] imageBuffer = GetImageBuffer(imageObject);
// the image object rturned by converter can be disposed
imageObject.Dispose();
// inform the browser about the binary data format
string mimeType = imageFormatName == "jpg" ? "jpeg" : imageFormatName;
HttpContext.Current.Response.AddHeader("Content-Type", "image/" + mimeType);
// let the browser know how to open the image
HttpContext.Current.Response.AddHeader("Content-Disposition",
String.Format("attachment; filename={0};
size={1}",
imageFileName, imageBuffer.Length.ToString()));
// write the image buffer to HTTP response
HttpContext.Current.Response.BinaryWrite(imageBuffer);
// call End() method of HTTP response
// to stop ASP.NET page processing
HttpContext.Current.Response.End();
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 a shitty html code</p>"
+ "<p>This is another html line</p></body>",
point, maxSize);
m_Bitmap.Save(@"C:\Test.png", ImageFormat.Png);
}
We are trying to better understand customer views on social support experience. Click HERE to participate the survey. Thanks!
Member
12 Points
79 Posts
Convert Div to Image
Feb 11, 2014 02:11 AM|rbmbala|LINK
I have a div containing gridview and table, need to convert this div to an Image which will be sent in mail and printed. What are the best possible solutions for it.
Regards,
Bala
Member
552 Points
591 Posts
Re: Convert Div to Image
Feb 11, 2014 02:54 AM|Anil Srivastava|LINK
// create the HTML to Image converter
HtmlToImage htmlToImageConverter = new HtmlToImage();
// set browser width
htmlToImageConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);
// set HTML Load timeout
htmlToImageConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);
// set whether the resulted image is transparent
htmlToImageConverter.TransparentImage =
(dropDownListImageFormat.SelectedValue == "PNG") ?
checkBoxTransparentImage.Checked : false;
// set triggering mode
// for WaitTime mode set the wait time before convert
switch (dropDownListTriggeringMode.SelectedValue)
{
case "Auto":
htmlToImageConverter.TriggerMode = ConversionTriggerMode.Auto;
break;
case "WaitTime":
htmlToImageConverter.TriggerMode = ConversionTriggerMode.WaitTime;
htmlToImageConverter.WaitBeforeConvert = int.Parse(textBoxWaitTime.Text);
break;
case "Manual":
htmlToImageConverter.TriggerMode = ConversionTriggerMode.Manual;
break;
default:
htmlToImageConverter.TriggerMode = ConversionTriggerMode.Auto;
break;
}
// convert to image
System.Drawing.Image imageObject = null;
string imageFormatName = dropDownListImageFormat.SelectedValue.ToLower();
string imageFileName = String.Format("HtmlToImage.{0}", imageFormatName);
if (radioButtonConvertUrl.Checked)
{
// convert URL
string url = textBoxUrl.Text;
imageObject = htmlToImageConverter.ConvertUrlToImage(url)[0];
}
else
{
// convert HTML code
string htmlCode = textBoxHtmlCode.Text;
string baseUrl = textBoxBaseUrl.Text;
imageObject = htmlToImageConverter.ConvertHtmlToImage(htmlCode, baseUrl)[0];
}
// get the image buffer in the selected image format
byte[] imageBuffer = GetImageBuffer(imageObject);
// the image object rturned by converter can be disposed
imageObject.Dispose();
// inform the browser about the binary data format
string mimeType = imageFormatName == "jpg" ? "jpeg" : imageFormatName;
HttpContext.Current.Response.AddHeader("Content-Type", "image/" + mimeType);
// let the browser know how to open the image
HttpContext.Current.Response.AddHeader("Content-Disposition",
String.Format("attachment; filename={0};
size={1}",
imageFileName, imageBuffer.Length.ToString()));
// write the image buffer to HTTP response
HttpContext.Current.Response.BinaryWrite(imageBuffer);
// call End() method of HTTP response
// to stop ASP.NET page processing
HttpContext.Current.Response.End();
Member
12 Points
79 Posts
Re: Convert Div to Image
Feb 12, 2014 07:25 AM|rbmbala|LINK
How to refer class HtmlToImage.
Member
552 Points
591 Posts
Re: Convert Div to Image
Feb 13, 2014 11:50 AM|Anil Srivastava|LINK
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlimage(v=vs.110).aspx
All-Star
37441 Points
9076 Posts
Re: Convert Div to Image
Feb 13, 2014 12:07 PM|AidyF|LINK
I think it is part of the iTextSharp library.
Member
552 Points
591 Posts
Re: Convert Div to Image
Feb 13, 2014 01:10 PM|Anil Srivastava|LINK
http://forums.asp.net/t/1764959.aspx
All-Star
15648 Points
2151 Posts
Re: Convert Div to Image
Feb 17, 2014 06:52 AM|Happy Chen - MSFT|LINK
i would suggest you check out the link for details:
Convert HTML string to image [closed]
Codes as shown below:
Member
12 Points
79 Posts
Re: Convert Div to Image
Feb 19, 2014 11:51 PM|rbmbala|LINK
The best thing to do is to create html mail instead of taking image.