0

我面临一个关于将整个网页保存为图像的问题。我有一个 Web 应用程序,使用的所有控件都是“用户控件”。我的应用程序中有一个页面,并使用它来抓取网页图像并保存到解决方案中的一个文件夹中。我得到了图像,但问题是图像的高度不正确。即:我收到的页面高度不正确。当我调试时,它显示 0px 作为页面的高度。

我的代码如下:

    protected void GetThumbnailWorker()
    {
        using (WebBrowser browser = new WebBrowser())
        {
            //browser.ClientSize = new Size(_width, _height);
            //browser.ClientSize = new Size(960,2000);
            browser.ScrollBarsEnabled = false;
            browser.ScriptErrorsSuppressed = true;
            browser.Navigate(_url); 


        // Wait for control to load page
        while (browser.ReadyState != WebBrowserReadyState.Complete)
            Application.DoEvents();

        // Render browser content to bitmap
        int imgaewidth = 0;
        int imageheight=0;
        imgaewidth=browser.Document.Body.ScrollRectangle.Width;
        imageheight=browser.Document.Body.ScrollRectangle.Height;


        if (imageheight <100)
        {
            imageheight = 1500;
        }
        if (imgaewidth == 0)
        {
            imgaewidth = 964;
        }
         browser.ClientSize = new Size(imgaewidth, imageheight);
        _bmp = new Bitmap(imgaewidth, imageheight);
         browser.DrawToBitmap(_bmp, new Rectangle(0, 0,
         imgaewidth, imageheight));

    }
}

由于这个问题,我将图像高度设置为 1500。

谁能帮我?

更新代码:

public static Bitmap GetThumbnail(string url, int width, int height,
  int thumbWidth, int thumbHeight)
{
    WebsiteThumbnail thumbnail = new WebsiteThumbnail(url, width, height,
      thumbWidth, thumbHeight);
    return thumbnail.GetThumbnail();
}

/// <summary>
/// Protected constructor
/// </summary>
protected WebsiteThumbnail(string url, int width, int height,
  int thumbWidth, int thumbHeight)
{
    _url = url;
    _width = width;
    _height = height;
    _thumbWidth = thumbWidth;
    _thumbHeight = thumbHeight;
}

/// <summary>
/// Returns a thumbnail for the current member values
/// </summary>
/// <returns>Thumbnail bitmap</returns>
protected Bitmap GetThumbnail()
{
    // WebBrowser is an ActiveX control that must be run in a
    // single-threaded apartment so create a thread to create the
    // control and generate the thumbnail
    Thread thread = new Thread(new ThreadStart(GetThumbnailWorker));
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
    return _bmp.GetThumbnailImage(_thumbWidth, _thumbHeight,
      null, IntPtr.Zero) as Bitmap;
}
4

0 回答 0