使用 C# 将 HTML 控件(Div 或 Table)转换为图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1972739/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:19:36  来源:igfitidea点击:

Convert a HTML Control (Div or Table) to an image using C#

c#asp.nethtmlimage

提问by ACP

Is it possible to convert a Html Control to an image in C#?

是否可以将 Html 控件转换为 C# 中的图像?

Is there any C# method where I can pass the Html Control object and return an image of that html control?

是否有任何 C# 方法可以传递 Html Control 对象并返回该 html 控件的图像?

Is this possible, any suggestions?

这可能吗,有什么建议吗?

采纳答案by Vijay

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

我们已经使用http://iecapt.sourceforge.net/将 HTML 转换为图像。你可以试试看。它是免费的

回答by p.campbell

Consider this (untested!) library over at guangmingsoftcalled htmlsnapshot.

考虑一下guangmingsoft 上的这个(未经测试!)库, 名为 htmlsnapshot。

add a reference to the htmlsnap2.dll

添加对 htmlsnap2.dll 的引用

There's a sample project there for download.

那里有一个示例项目可供下载。

Here's their sample code, lifted straight from that link:

这是他们的示例代码,直接从该链接中提取:

snap = new CHtmlSnapClass();
snap.Url("www.google.com", "*")
byte[] data = (byte[])snap.GetImageBytes(".jpg");
//byte[] data = (byte[])snap.GetThumbImageBytes(".jpg", 100, 100, 1);


FileStream fs = File.OpenWrite(@"c:.jpg");
BinaryWriter br = new BinaryWriter(fs);
br.Write(data);
br.Close();
fs.Close();

UpdateIf you wanted only a particular control, you could write yourself a page whose job is to re-render your target control as the only bits of HTML on the page.

更新如果您只需要一个特定的控件,您可以自己编写一个页面,该页面的工作是将目标控件重新呈现为页面上仅有的 HTML 位。

回答by dnord

The control you're describing has, as its output, HTML. That's all it does.

您所描述的控件的输出是 HTML。这就是它所做的一切。

Your problem is that you want to turn a snippet of HTML into an image. Rendering HTML is done by a browser - ASP.NET has basically nothing to do with how HTML is rendered by a client.

您的问题是您想将 HTML 片段转换为图像。呈现 HTML 是由浏览器完成的 - ASP.NET 基本上与客户端如何呈现 HTML 无关。

Most .NET libraries that do this job (turning HTML into images) use IE to power the conversion. Some of those utilities include:

大多数执行此工作(将 HTML 转换为图像)的 .NET 库使用 IE 来支持转换。其中一些实用程序包括:

  1. Websites Screenshot - http://www.websitesscreenshot.com/
  2. The aforementioned htmlsnapshot - http://www.guangmingsoft.net/htmlsnapshot/help.htm
  3. Basically any HTML -> PDF library has this functionality, including ABCPdf - http://www.websupergoo.com/abcpdf-1.htm
  1. 网站截图 - http://www.websitesscreenshot.com/
  2. 前面提到的 htmlsnapshot - http://www.guangmingsoft.net/htmlsnapshot/help.htm
  3. 基本上任何 HTML -> PDF 库都有这个功能,包括 ABCPdf - http://www.websupergoo.com/abcpdf-1.htm

But the more basic answer to the question is that ASP.NET controls don't render to an image format. You'll have to do an IE screenshot of a page that has only that control (or HTML) on it.

但对这个问题更基本的回答是 ASP.NET 控件不会呈现为图像格式。您必须对只有该控件(或 HTML)的页面进行 IE 屏幕截图。

回答by Brij

You need to create a separate page which is to be converted into image and call it in iframe. Then Try following:
http://articles.sitepoint.com/article/generating-asp-net-images-fly
OR
http://www.guangmingsoft.net/wordpress/convert-html-to-image-without-temporary-files-in-c/

您需要创建一个单独的页面,将其转换为图像并在 iframe 中调用它。然后尝试以下操作:
http: //articles.sitepoint.com/article/generating-asp-net-images-fly

http://www.guangmingsoft.net/wordpress/convert-html-to-image-without-temporary-文件在 C/

回答by Dan Tao

I haven't tried it myself, but something I've been meaning to take a look at that may help you is HTMLRenderer.

我自己还没有尝试过,但我一直想看看可能对你有帮助的东西是HTMLRenderer

回答by Thomas Martin

Yes. It can be easily done using the html2canvas method. html2canvas is used to take screenshots of the html elements. Yo can do this by simply adding the html div into an html modal and pass the modal id to the jquery function and call that jqueryy function in the button onclick. you can checkout this link to know more about the htm2canvas method

是的。使用 html2canvas 方法可以轻松完成。html2canvas 用于截取 html 元素的屏幕截图。您可以通过简单地将 html div 添加到 html 模态并将模态 id 传递给 jquery 函数并在按钮 onclick 中调用该 jqueryy 函数来完成此操作。您可以查看此链接以了解有关 htm2canvas 方法的更多信息