C# 将 HTML 渲染为图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/334532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Render HTML as an Image
提问by Tablet
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 issue because of how backgrounds disappear when printing and other problems, so I think the best solution would be to be able to generate an image based on the html, or set up some kind of template that takes in strings and an image, and generates an image using the image fed in as a background and puts the coupon information on top.
我正在根据动态输入和裁剪图像生成优惠券,我现在正在使用 ntml 和 css 显示优惠券,问题是,由于打印时背景如何消失和其他问题,打印这已成为一个问题,所以我认为最好的解决方案是能够基于 html 生成图像,或者设置某种接受字符串和图像的模板,并使用输入的图像作为背景生成图像并将优惠券信息在上面。
Is there anything that does this already?
有什么东西可以做到这一点吗?
This is for an ASP.NET 3.5 C# website!
这是针对 ASP.NET 3.5 C# 网站的!
Thanks in advance.
提前致谢。
edit: It'd be great if the output could be based on the HTML input, as the coupon is designed by manipulating the DOM using jQuery and dragging stuff around, it all works fine, it's just when it comes to the printing (to paper) it has z-indexing issues.
编辑:如果输出可以基于 HTML 输入,那就太好了,因为优惠券是通过使用 jQuery 操作 DOM 并拖动东西来设计的,一切正常,只是在打印时(到纸) 它有 z 索引问题。
采纳答案by Dillie-O
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:
您可以做的是创建一个 aspx 页面,将响应类型更改为您想要的格式,然后将图像放入流中。我创建了一个执行类似操作的条形码生成器。排除生成图像的所有手续,您将 Page_Load 看起来像这样:
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.
就是这样!然后您在图像中要做的就是将 URL 设置为类似于 RenderImage.aspx?MagicParm=WooHoo 或您需要的任何内容。这样你就可以让它渲染你想要指定的任何东西。
回答by adam
回答by nickf
Unless the "other problems" are pretty severe, couldn't you just instruct your users to turn on Background Images when printing?
除非“其他问题”非常严重,否则您不能在打印时指示您的用户打开背景图像吗?
In any case, I'd default to serving a PDF rather than an image, doubly so since it is intended for print.
在任何情况下,我都会默认提供 PDF 而不是图像,因为它是用于打印的,所以加倍如此。
回答by Webjedi
回答by Dillie-O
You can render html to a bitmap using the WebBrowser control in either a winforms or console application.
您可以在 winforms 或控制台应用程序中使用 WebBrowser 控件将 html 呈现为位图。
An example of this can be found here: http://www.wincustomize.com/articles.aspx?aid=136426&c=1
一个例子可以在这里找到:http: //www.wincustomize.com/articles.aspx?aid=136426& c=1
The above example can be modified to run in ASP.Net by creating a new STAThread and performing an Application.Run on it to start a new message loop.
通过创建新的 STAThread 并在其上执行 Application.Run 以启动新的消息循环,可以将上面的示例修改为在 ASP.Net 中运行。
回答by Jon Winstanley
PHP/Ruby Alternative
PHP/Ruby 替代方案
If you have accessed this question and are actually looking for soething that will work without Windows, you can try the KHTML library: http://wiki.goatpr0n.de/projects/khtmld
如果您已经访问了这个问题并且实际上正在寻找可以在没有 Windows 的情况下工作的解决方案,您可以尝试 KHTML 库:http: //wiki.goatpr0n.de/projects/khtmld
The website has a ridiculous name I admit, but I can assure you it is genuine. Other related pages are: the sourceforge page http://khtml2png.sourceforge.net/
我承认该网站的名称很荒谬,但我可以向您保证它是真实的。其他相关页面有:sourceforge 页面http://khtml2png.sourceforge.net/
回答by UpTheCreek
Just set up your css properly, so that you have a css file targeted at the print medium. It is pretty easy to guarantee that the coupon will always be legible, without worrying about whether they have bg images on or not. Needlesly moving to an image doesn't make any sense, unless there is some reason you don't want it to be machine readable.
只需正确设置您的 css,这样您就有一个针对打印介质的 css 文件。很容易保证优惠券始终清晰可辨,而不必担心它们是否有 bg 图像。不必要地移动到图像没有任何意义,除非有某种原因您不希望它是机器可读的。
回答by Lucas
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.
我自己没有尝试过,但是您应该能够通过使用 WebBrowser 控件和从 Control 基类继承的 DrawToBitmap() 方法将 HTML 呈现为图像。
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.
更新:我自己尝试过,但有一些注意事项。WebBrowser 控件在显示控件之前似乎不会呈现网页,因此 WebBrowser 需要在表单中,并且必须显示表单才能呈现 HTML 并引发 DocumentCompleted 事件。
回答by bakoyaro
MARKUP ONLY ALTERNATE SOLUTION
仅标记替代解决方案
Use SVG and XSLT to transform the html data into an image that can be rendered/saved/etc.
使用 SVG 和 XSLT 将 html 数据转换为可以呈现/保存/等的图像。
I'll admit that at first it was tedious getting this to work because of all of the coordinates, but well worth the effort once it is running.
我承认,起初由于所有坐标,让它工作很乏味,但是一旦运行就非常值得付出努力。