locked
How to convert HTML to Image RRS feed

  • Question

  •  
    I tried with the below code but doesnt seems to work.

    String myImage = @"<html><body><table><tr>

    <td padding-top: 3px;"">

    <font style='font-size: 18px; weight: bold; font-family: Arial; color: #2a4c71;'>

    TEST IMAGE</font></td></tr></table></body></html>";

    byte[] myBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(myImage);

    MemoryStream ms = new MemoryStream(myBytes);

    try

    {

    Bitmap img = (Bitmap)Image.FromStream(ms);//gettting an error here saying parameter invalid

    Response.Clear();

    Response.AddHeader("CONTENT-DISPOSITION", "ATTACHMENT;FILENAME=CAAR_APPLICATION_IMAGE.JPEG");

    Response.ContentType = "IMAGE/JPEG";

    Response.Charset = "";

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    Response.Write(img.ToString());

    Response.Flush();

    Response.End();

    }

    catch (Exception ex)

    {

    }

    Thanks,
    Vicky

    • Edited by Vicky84 Friday, September 26, 2008 7:43 PM
    • Moved by CoolDadTx Saturday, September 27, 2008 6:19 PM Unrelated to IDE, moving (Moved from Visual C# IDE to .NET Base Class Library)
    Friday, September 26, 2008 7:42 PM

Answers

  • Hi Vicky,

    As far as I know, it is not as easy as defininig the HTML you need, and trying to throw that inside an image. Who would be responsible for rendering the HTML and transforming it into an image? What would it have to done when there are controls in there, or JavaScript.

    You will have to create an Image, use its Graphics property to paint on, and generate your image by hand this way. In this example you can see how this might be done using an existing image and drawing on that. Of course you can also start with an empty image.

    Hope this helps.
    Mark the best replies as answers - my blog: http://bloggingabout.net/blogs/rick
    • Marked as answer by Zhi-Xin Ye Wednesday, October 1, 2008 10:37 AM
    Saturday, September 27, 2008 7:16 AM