0

我试图通过 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));

但是,即使我改变了参数整数值,我仍然不能满足结果。

请有任何建议。

4

3 回答 3

1

在我看来,如果你赢得了删除你周围的写入像素,那么你可以在创建图像后使用一个简单的过滤器来创建它们,然后你会找到图像的最终起点和大小,然后将其剪切。

您可以使用GetPixel(x, y)

例如,您可以从上到下扫描图像,然后从下到上,从右到左和从左到右扫描,以找到不包含白色像素的图像的新尺寸。

例如,类似的东西:

for (int AxonX = 0; AxonX < wb.Width; AxonX++)
{
    for (int AxonY = 0; AxonY < wb.Height; AxonY++)
    {
        Color ThisPixelColor = wb.GetPixel(AxonX, AxonY);

        // Check if white or not and do the rest            
    }
}
于 2013-01-08T08:44:48.423 回答
1

默认样式表为大多数 DOM 元素提供不为零的边距和/或填充,包括body.

我建议明确设置margin: 0; padding: 0;样式div。然后进一步向下导航到div容器并获取尺寸。

于 2013-01-08T10:04:29.897 回答
0

最后我找到了如何解决我自己的问题。
让我分享一下我的解决方法,以便其他面临同样问题的人可以避免同样的困难。

首先让我感谢@devstuff 和@Aristos。

我的解决方案如下...

protected void Page_Load(object sender, EventArgs e)
    {   
        string _GenerateString = @"Number One 1<sup>st</sup> , the very first image creation.";
        StringBuilder sb_GenerateHTMLContent = new StringBuilder();
        sb_GenerateHTMLContent.Append(" <div style=\" white-space: nowrap \"> ");
        sb_GenerateHTMLContent.Append( _GenerateString );
        sb_GenerateHTMLContent.Append(" </div>");

        var bmp = MakeScreenshot(sb_GenerateHTMLContent.ToString());
        bmp.Save(@"C:\test_web.jpg");
    }

    public Bitmap MakeScreenshot(string html)
    {
        // Load the webpage into a WebBrowser control
        WebBrowser wb = new WebBrowser();
        Bitmap bitmap = null;
        try
        {
            wb.Navigate("about:blank");
            if (wb.Document != null)
            {
                wb.Document.Write(html);
            }

            wb.DocumentText = html;
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Width = 0;

            while (wb.ReadyState != WebBrowserReadyState.Complete)
            {
                System.Windows.Forms.Application.DoEvents();
            }

            StringBuilder sb_style_body = new StringBuilder();
            sb_style_body.Append(" margin-top:0; ");
            sb_style_body.Append(" margin-right:0; ");
            sb_style_body.Append(" margin-left:0; ");
            sb_style_body.Append(" margin-bottom:0; ");                
            sb_style_body.Append(" padding-top:0; ");
            sb_style_body.Append(" padding-bottom:0; ");
            sb_style_body.Append(" padding-right:0; ");
            sb_style_body.Append(" padding-left:0; ");
            sb_style_body.Append(" font-family:Arial,Helvetica,sans-serif; ");
            sb_style_body.Append(" font-size:medium; ");

            wb.Document.Body.Style = sb_style_body.ToString();

            wb.Height = wb.Document.Body.ScrollRectangle.Height;
            wb.Width = wb.Document.Body.ScrollRectangle.Width;

            bitmap = new Bitmap(wb.Width, wb.Height);
            wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally {
            wb.Dispose();
        }
        return bitmap;
    }
于 2013-01-08T10:50:31.517 回答