C#怎么将 HTML转换为图片或 PDF?


本文摘自PHP中文网,作者零下一度,侵删。

首先是把 HTML 转换为图片。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public partial class Form1 : Form

    {public Form1()

        {

            InitializeComponent();

        }

 

        WebBrowser webBrowser = null;public void ConvertToImg()

        {

            webBrowser = new WebBrowser();//是否显式滚动条webBrowser.ScrollBarsEnabled = false;//加载HTML页面的地址webBrowser.Navigate("");            //页面加载完成执行事件webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);

        }private void webBrowser_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作        {//获取解析后HTML的大小System.Drawing.Rectangle rectangle = webBrowser.Document.Body.ScrollRectangle;int width = rectangle.Width;int height = rectangle.Height;//设置解析后HTML的可视区域webBrowser.Width = width;

            webBrowser.Height = height;

 

            Bitmap bitmap = new System.Drawing.Bitmap(width, height);

            webBrowser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, width, height));//设置图片文件保存路径和图片格式,格式可以自定义string filePath = AppDomain.CurrentDomain.BaseDirectory + "../../SaveFIle/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";

            bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);

        }private void button1_Click(object sender, EventArgs e)

        {

            ConvertToImg();

        }

    }

View Code

上面这种方法是解析指定URL地址的HTML,然后转换为图片。当然,也可以直接写一些HTML标签。如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public partial class Form1 : Form

    {public Form1()

        {

            InitializeComponent();

        }

 

        WebBrowser webBrowser = null;public void ConvertToImg()

        {string html = "<div><table border = \"1\" cellspacing = \"0\" cellpadding = \"3\" style = \"text-align:center;\"><tr><th style = \"width:60px;\">字段1</th><th style = \"width:60px;\">字段2</th><th style = \"width:60px;\">字段3</th></tr><tr style = \"color:green;\"><td>小明</td><td>22</td><td>185</td></tr><tr style = \"color:red;\"><td>小青</td><td>21</td><td>170</td></tr></table></div>";

 

            webBrowser = new WebBrowser();//是否显式滚动条webBrowser.ScrollBarsEnabled = false;//加载 htmlwebBrowser.DocumentText = html;            //页面加载完成执行事件webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);

        }private void webBrowser_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作        {//获取解析后HTML的大小System.Drawing.Rectangle rectangle = webBrowser.Document.Body.ScrollRectangle;int width = rectangle.Width;int height = rectangle.Height;//设置解析后HTML的可视区域webBrowser.Width = width;

            webBrowser.Height = height;

 

            Bitmap bitmap = new System.Drawing.Bitmap(width, height);

            webBrowser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, width, height));//设置图片文件保存路径和图片格式,格式可以自定义string filePath = AppDomain.CurrentDomain.BaseDirectory + "../../SaveFIle/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";

            bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);

        }private void button1_Click(object sender, EventArgs e)

        {

            ConvertToImg();

        }

    }

View Code

阅读剩余部分

相关阅读 >>

介绍c#中的堆和栈

使用c#实现发送自定义的html格式邮件的代码案例

.net实现简易的文件增量备份程序

.net中json数据进行序列化和反序列化操作分析

编写高性能 .net的实例教程

.net core中如何使用entity framework操作postgresql?

.net core中如何使用ref和span<t>提高程序性能的实现代码

c#如何利用filesystemwatcher控件实现的文件监控的具体示例分享

c#中五种访问修饰符作用范围实例详解

c#如何使用正则表达式来验证中文字符的案例

更多相关阅读请进入《csharp》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...