Convert a HTML Control (Div or Table) to an image using C#
We have used http://iecapt.sourceforge.net/ to convert HTML to image. You can try it out. It is available for FREE.
Consider this (untested!) library over at guangmingsoft called htmlsnapshot.
add a reference to the htmlsnap2.dll
There's a sample project there for download.
Here's their sample code, lifted straight from that link:
snap = new CHtmlSnapClass();
snap.Url("www.google.com", "*")
byte[] data = (byte[])snap.GetImageBytes(".jpg");
//byte[] data = (byte[])snap.GetThumbImageBytes(".jpg", 100, 100, 1);
FileStream fs = File.OpenWrite(@"c:\1.jpg");
BinaryWriter br = new BinaryWriter(fs);
br.Write(data);
br.Close();
fs.Close();
Update If you wanted only a particular control, you could write yourself a page whose job is to re-render your target control as the only bits of HTML on the page.
The control you're describing has, as its output, HTML. That's all it does.
Your problem is that you want to turn a snippet of HTML into an image. Rendering HTML is done by a browser - ASP.NET has basically nothing to do with how HTML is rendered by a client.
Most .NET libraries that do this job (turning HTML into images) use IE to power the conversion. Some of those utilities include:
- Websites Screenshot - http://www.websitesscreenshot.com/
- The aforementioned htmlsnapshot - http://www.guangmingsoft.net/htmlsnapshot/help.htm
- Basically any HTML -> PDF library has this functionality, including ABCPdf - http://www.websupergoo.com/abcpdf-1.htm
But the more basic answer to the question is that ASP.NET controls don't render to an image format. You'll have to do an IE screenshot of a page that has only that control (or HTML) on it.