我试图通过 HTML DIV 内容生成图像。
我通过使用下面的代码成功了System.Windows.Forms.WebBrowser
。
protected void Page_Load(object sender, EventArgs e)
{
string _GenerateString = @"VisualStudio<sup>ide test</sup>";
StringBuilder sb = new StringBuilder();
sb.Append(" <style type=\"text/css\"> ");
sb.Append(" #_GenerateHTMLToImage_{ ");
sb.Append(" width:...px; ");
sb.Append(" height:...px; ");
sb.Append(" } ");
sb.Append(" </style> ");
sb.Append(" <div id='_GenerateHTMLToImage_'> ");
sb.Append(_GenerateString);
sb.Append(" </div> ");
//var bmp = MakeScreenshot_original(sb.ToString());
var bmp = MakeScreenshot(sb.ToString());
bmp.Save(@"C:\test_web.jpg");
}
public Bitmap MakeScreenshot(string html)
{
// Load the webpage into a WebBrowser control
WebBrowser wb = new WebBrowser();
wb.Navigate("about:blank");
if (wb.Document != null){
wb.Document.Write(html);
}
wb.DocumentText = html;
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
while (wb.ReadyState != WebBrowserReadyState.Complete) {
System.Windows.Forms.Application.DoEvents();
}
wb.Width = wb.Document.Body.ScrollRectangle.Width;
wb.Height = wb.Document.Body.ScrollRectangle.Height;
// Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
wb.Dispose();
return bitmap;
}
它在下面生成我
但我的问题是它生成图像(我需要的字符串 +不需要的空格)。我不知道如何生成我想要的仅图像字符串并删除任何空白。
我已经从下面的行更改了参数值...
Bitmap bitmap = new Bitmap(xxx, xxx);
wb.DrawToBitmap(bitmap, new Rectangle(xxx, xxx, xxxx, xxxx));
但是,即使我改变了参数整数值,我仍然不能满足结果。
请有任何建议。