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();
}private void webBrowser_DocumentCompleted(object sender, EventArgs e)
webBrowser.Height = height;
Bitmap bitmap = new System.Drawing.Bitmap(width, height);
webBrowser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, width, height));
bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}private void button1_Click(object sender, EventArgs e)
{
ConvertToImg();
}
}