views:

2118

answers:

5

Hi guys,

Is it possible to convert a Html Control to an image in C#?

Is there any C# method where I can pass the Html Control object and return an image of that html control?

Is this possible, any suggestions?

+4  A: 

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.

p.campbell
@pcampbell whether it takes a snap of whole page i want only the gridview..
Pandiya Chendur
Then create your page with just the gridview.
Chris Farmer
"Support Windows 7 and IE8 now" leads me to believe it renders using Internet Explorer. The question is: do you want IE's rendered version of the table, or some other browser's? Using something like Firefox or Safari is more difficult than IE because they don't provide ActiveX versions of their browser out-of-the-box. 3rd party implementations exist though, such as http://www.iol.ie/~locka/mozilla/mozilla.htm
Codesleuth
Mozilla has long provided embeddable versions of the Gecko runtime... They just don't advertise the fact well... Officially, the way to get the activex control is XULRunner. https://developer.mozilla.org/en/XULRunner (Unofficially, the dll is included in the firefox download anyway, because Firefox is "really" just an app which embeds Gecko...)
Stobor
+2  A: 

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:

  1. Websites Screenshot - http://www.websitesscreenshot.com/
  2. The aforementioned htmlsnapshot - http://www.guangmingsoft.net/htmlsnapshot/help.htm
  3. 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.

dnord
@dnord is there any open source tool for doing it...
Pandiya Chendur
+8  A: 

We have used http://iecapt.sourceforge.net/ to convert HTML to image. You can try it out. It is available for FREE.

Vijay
The snapshot of msdn was a trip back in time: http://iecapt.sourceforge.net/msdn.microsoft.com.png
ccook
@vijay will IECapt work with other browsers like firefox and chrome...
Pandiya Chendur
IECapt works fine with IE7. On the server IE7 should be there and the client can be any browser.
Vijay
This is my experience. It was not working on the server having different version of IE.
Vijay
A: 

You need to create a separate page which is to be converted into image and call it in iframe. Then Try following:
http://articles.sitepoint.com/article/generating-asp-net-images-fly
OR
http://www.guangmingsoft.net/wordpress/convert-html-to-image-without-temporary-files-in-c/

Brij
+1  A: 

I haven't tried it myself, but something I've been meaning to take a look at that may help you is HTMLRenderer.

Dan Tao
That one looks good
Rune Grimstad