创建缩略图失败并创建空白图像

时间:2012-05-30 21:54:00

标签: c# thumbnails image-capture

我有一个奇怪的问题,我拿了一个单词列表,并通过谷歌搜索单词创建截图.. 在它创建了50个截图后,它开始创建空白图像!!

有人可以帮我解决问题!

PS:如果我需要更清楚,请告诉我!

这是我用来创建屏幕截图的代码...在我的主页上我只是循环我的话!

有人可以帮助我解决问题

public class GeneateScreenshot
 {
 public void GenerateScreenshot()
  {

  }
public Bitmap GenerateScreenshot(string url)
{
    // This method gets a screenshot of the webpage
    // rendered at its full size (height and width)

    return GenerateScreenshot(url, -1, -1);
}

public Bitmap GenerateScreenshot(string url, int width, int height)
{
    // Load the webpage into a WebBrowser control

    WebBrowser wb = new WebBrowser();
    wb.ScrollBarsEnabled = false;
    wb.ScriptErrorsSuppressed = true;
    wb.Navigate(url);
    while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }


    // Set the size of the WebBrowser control
    wb.Width = width;
    wb.Height = height;

    if (width == -1)
    {
        // Take Screenshot of the web pages full width
        wb.Width = wb.Document.Body.ScrollRectangle.Width;
    }

    if (height == -1)
    {
        // Take Screenshot of the web pages full height
        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;
}

}

1 个答案:

答案 0 :(得分:1)

我怀疑DrawToBitmap函数不是将WebBrowser控件转换为图像的正确方法。查看以下链接,看起来您需要使用一些本机方法来可靠地执行此操作:

WebBrowser.DrawToBitmap() or other methods?

Converting WebBrowser.Document To A Bitmap?

http://www.codeproject.com/Articles/58605/HTML-to-Image-in-C

P.S。使用此代码时,我很快被Google的机器人探测器接听,所以你也可能遇到麻烦。