views:

61

answers:

4

Hi

I like to convert html for example a table to and image and save as jpg. (what I mean is that a table displayed on web page, I only want to get that specific table and save as image) Is it possible using asp.net?

thanks

A: 

See below code.You need to pass a table in parameter. Please let us know the output.

 private void saveURLToImage(string url)
    {
        if (!string.IsNullOrEmpty(url))
        {
            string content = "";

            System.Net.WebRequest webRequest = WebRequest.Create(url);
            System.Net.WebResponse webResponse = webRequest.GetResponse();
            System.IO.StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
            content = sr.ReadToEnd();
            //save to file
            byte[] b = Convert.FromBase64String(content);           
            System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            img.Save(@"c:\pic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            img.Dispose();
            ms.Close();
        }
    }
Amit
This Content is a line string - including all the html code... not what he ask for. I think Amit, you need to check at least what the GetRespose return.
Aristos
My Mistake.....
Amit
A: 

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

or

ref

http://stackoverflow.com/questions/1972739/convert-a-html-control-div-or-table-to-an-image-using-c
Amit
A: 

I think this is a rendering issue and completely depends upon what browser rendering your HTML.

But this the closest thing I could get you using GDI+ and WebBrowser control from CodeProject.

Let me know whether it helped you or not!

Regards.

Munim Abdul
A: 

If I may suggest giving php a shot? I found it extremely useful for our website, being able to easily generate .png images from the code (png is an overall better quality than jpeg).

The code necessary is already part of the standard, so you don't need to go through the trouble of finding a free/trial package in order to get this done.

Take a look here: http://www.thesitewizard.com/php/create-image.shtml

Even if you aren't familiar with php, this should make enough sense. The methods are more than readable- very straightforward.

<?php
$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, "thesitewizard.com",
  $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );

header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>

Since this becomes an image, you might be able to call it from html in some shape, way, or form. Again, I'd prefer php in general because you can inter-mix html into it as well.

If php isn't your cup of tea, I'd be happy to search for some HTML solutions for you.

Justian Meyer