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));
しかし、パラメーターの整数値を変更しても、結果は満足できません。
任意の提案をお願いします。