locked
How to convert HTML to JPEG Image RRS feed

  • Question

  • Can any one tell me how to convert basic html to image using c#?

    Regards,
    Vicky 
    Friday, September 26, 2008 1:54 PM

Answers

All replies

  • Read html in string variables and draw it on Bitmap. Save the bitmap.
    MCTS, CodeProject MVP 2008
    Friday, September 26, 2008 2:03 PM
  • 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

    Friday, September 26, 2008 5:05 PM
  • There's a great example of how to do this here:

    http://www.dreamincode.net/code/snippet2539.htm
    David Morton - http://blog.davemorton.net/
    • Marked as answer by nobugz Saturday, September 27, 2008 6:37 PM
    Friday, September 26, 2008 5:17 PM
  • this works for windows. but i need this for web application.

    Regards
    Vicky
    Friday, September 26, 2008 6:14 PM
  • I would imagine (although I could be completely wrong about this), that you could still do the same thing in ASP.NET. Spawn a Thread.  Call Thread.SetApartmentState(ApartmentState.STA), reference System.Drawing and System.Windows.Forms.  Create a method to run on the separate thread that will perform these functions in the link, and then just start the thread.  I don't believe you need to actually have the WebBrowser shown on a form to accomplish this.
    David Morton - http://blog.davemorton.net/
    Friday, September 26, 2008 6:17 PM
  • but why does this chunk of code give me error

    Bitmap img = (Bitmap)Image.FromStream(ms);//invalid parameter.

    If this is resolved it will be very helpful.

    Regards
    Vicky

    Friday, September 26, 2008 6:28 PM
  • Which error is it exactly?  Could you post the exact error message?  Have you instantiated a MemoryStream and named it "ms"?

    Wait, I just read your code.  You're getting an error because the value you've put into the stream is a text value, and you're trying to convert a text stream to a picture.  That won't work.  Just because you have the HTML doesn't mean the HTML can be converted to a picture without using some kind of client. 

    I do wish Giorgi would give a better explanation here. Perhaps he's thinking you're trying to draw the text of the HTML onto the bitmap directly, perhaps by DrawString or some other method, but I don't think that's what you're trying to do.

    Have you looked further into creating a thread to create the bitmap with a WebBrowser control?
    David Morton - http://blog.davemorton.net/
    Friday, September 26, 2008 6:29 PM
  • i need to get the contents of a html page and convert it to image format. I have not implemented threading till now. its a great idea. i would surely try that way but currently i think it would take time if i start looking into threading hence i want to stay away from threading and implement it in my c# cod behind.

    Regards,
    Vicky

    Friday, September 26, 2008 6:39 PM
  • Okay.  Good luck then.  You may be better off asking this question in the ASP.NET forums at http://forums.asp.net/ since you're working with a web application.


    David Morton - http://blog.davemorton.net/
    • Marked as answer by nobugz Saturday, September 27, 2008 6:36 PM
    • Unmarked as answer by nobugz Saturday, September 27, 2008 6:37 PM
    Friday, September 26, 2008 6:41 PM
  • Thanks,

    Regards,
    Vicky
    Friday, September 26, 2008 6:47 PM
  • Did you try to use third-party library? PDF Vision .Net it's very good library which supports html to image conversion. Add dll in your application and use this code:

    			SautinSoft.PdfVision v = new SautinSoft.PdfVision();
                System.Drawing.Bitmap imgHtml = v.ConvertHtmlFileToBitmap(@"http://www.cnn.com");
    			//If you need split bitmap use Graphics :)

    Also you may look this good example how to do this http://blog.sautinsoft.com/?p=249

    Friday, December 16, 2011 10:42 AM
  • Hi, you can also try this:

    DocumentModel.Load("index.html", LoadOptions.HtmlDefault)
                 .Save(this.Response, "index.jpg", new ImageSaveOptions(ImageSaveFormat.Jpeg));

    The code uses this C# Word Automation alternative and it will stream the image to an ASP.NET client's browser.

    Thursday, May 8, 2014 8:33 AM
  • i have the save requirement can you give the code
    Friday, August 29, 2014 10:23 AM
  • Well, you need to use the proper C# libraries to covert the HTML to JPEF. Check out this link http://www.print-driver.com/download for more info.
    Sunday, March 15, 2015 7:32 AM
  • Well, you need to use the proper C# libraries to covert the HTML to JPEF. Check out this link http://www.print-driver.com/download for more info.
    • Edited by josewatt Sunday, March 15, 2015 7:34 AM spellings
    Sunday, March 15, 2015 7:33 AM