HTML为JPG用C# [英] html to jpg with c#

查看:207
本文介绍了HTML为JPG用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些搜索,从一个html页面尝试并生成JPG文件,并找到了一个解决方案,称为IECapt或类似的东西,需要在服务器上的IE浏览器工作...不是我想要的。

下面就是我希望做:生成HTML页面一幅JPG格式(HTML页面将只是文本),然后把水印在JPG

从本质上讲,我创建一个样本,我的用户可以看到,这将仅仅是从HTML(只是再次连胜文)创建的映像。该样品应当具有水印上它,如上所述。是否有任何库可与C#这样做吗?我想是通过在我的网页,我想转换为一个方法的URL,也许是保存JPG的路径,那么它的工作它的魔力,以及URL转换为jpg图片,折腾了水印它,然后说万岁!

编辑:

添加一些code的答案below..can't让我的头围绕这样的:

  InitialContainer C =新InitialContainer(&​​LT; HTML><身体GT;< D​​IV ALIGN = \中央\>这是我的HTML,它在这里工作和LT? ; / DIV>< /身体GT;< / HTML>中);
            位图m_Bitmap =新位图(400,700);
            c.Paint(Graphics.FromImage(m_Bitmap));
            m_Bitmap.Save(@C:\测试\ Test.bmp);
 

编辑2:

这确实可行。

 位图m_Bitmap =新位图(400,600);
            的PointF点=新的PointF(0,0);
            HtmlRenderer.Render(Graphics.FromImage(m_Bitmap),< HTML><身体GT;< D​​IV ALIGN = \中央\>这是我的HTML,它在这里工作< / DIV>< /身体GT;< / HTML>中,点,500);
            m_Bitmap.Save(@C:\测试\ Test.bmp);
 

解决方案

您可以使用这个 HtmlRenderer

I did some searching to try and generate jpg files from an html page and found one solution called IECapt or something similar that requires IE on the server to work...not what I want.

Here's what I'm looking to do: Generate a jpg image from an html page (the html page will just be text) and then put a watermark over the jpg.

Essentially, I'm creating a "sample" that my users can see which will just be an image created from html (again just straight text). That sample should have a watermark on it as mentioned above. Are there any libraries available to do this with c#? What I'd like is to pass in the url of my page that I want converted to a method and maybe the save path of the jpg, then have it work its magic, and convert that url to a jpg image, toss a watermark on it, then say hooray!

edit:

adding some code from the answer below..can't get my head around this:

InitialContainer c = new InitialContainer("<html><body><div align=\"center\">This is my html, does it work here?</div></body></html>");
            Bitmap m_Bitmap = new Bitmap(400, 700);
            c.Paint(Graphics.FromImage(m_Bitmap));
            m_Bitmap.Save(@"C:\test\Test.bmp");

edit 2:

this DOES work.

Bitmap m_Bitmap = new Bitmap(400, 600);
            PointF point = new PointF(0,0);
            HtmlRenderer.Render(Graphics.FromImage(m_Bitmap), "<html><body><div align=\"center\">This is my html, does it work here?</div></body></html>",point, 500);
            m_Bitmap.Save(@"C:\test\Test.bmp");

解决方案

You can use this HtmlRenderer class.

这篇关于HTML为JPG用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆