Render HTML as an Image

前端 未结 9 2075
遥遥无期
遥遥无期 2020-11-27 18:19

I\'m generating a coupon based on dynamic input and a cropped image, and I\'m displaying the coupon using ntml and css right now, the problem is, printing this has become an

相关标签:
9条回答
  • 2020-11-27 18:48

    There is a very powerful image creation library called GD which I often use with PHP.

    I am led to believe there is a wrapper for this library that ASP programmers can use. Try this

    0 讨论(0)
  • 2020-11-27 18:51

    I haven't tried to myself, but you should be able to render HTML into an image by using the WebBrowser control and the DrawToBitmap() method inherited from the base Control class.

    UPDATE: I tried this myself and there are some caveats. The WebBrowser control doesn't seem to render the web page until the control is show, so the WebBrowser needs to be in a Form and the Form must be shown for the HTML to be rendered and the DocumentCompleted event to be raised.

    0 讨论(0)
  • 2020-11-27 18:57

    What you can do is create an aspx page that changes the response type to be in the format you want and then put the image into the stream. I created a barcode generator that does a similar thing. Excluding all the formalities of generating the image, you'll Page_Load will look something like this:

    Bitmap FinalBitmap = new Bitmap();
    MemoryStream msStream = new MemoryStream();
    
    strInputParameter == Request.Params("MagicParm").ToString()
    
    // Magic code goes here to generate your bitmap image.
    FinalBitmap.Save(msStream, ImageFormat.Png);
    
    Response.Clear();
    Response.ContentType = "image/png";
    
    msStream.WriteTo(Response.OutputStream);
    
    if ((FinalBitmap != null)) FinalBitmap.Dispose();
    

    and that's it! Then all you have to do in your image is set the URL to be something like RenderImage.aspx?MagicParm=WooHoo or whatever you need. That way you can have it render whatever you want to specify.

    0 讨论(0)
提交回复
热议问题