日期:2014-05-18  浏览次数:21023 次

【修改代码】怎样直接获取页面宽和高???
抓取网页快照的时候,下面的语句只能抓取指定尺寸的图片,有时候抓不全,有时候留下好多空白:
Bitmap bmp = HtmlToImg.GetHtmlToImg("http://www.baidu.com", 600, 500, 600, 500);
怎样直接获取被抓取页面的宽和高?谁能直接修改下面的代码帮我实现(网上搜的m_webBrowser.Document.Window.Size;可以吗?该怎么用?):
aspx.cs
C# code

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Bitmap bmp = HtmlToImg.GetHtmlToImg("http://www.baidu.com", 600, 500, 600, 500);
        MemoryStream stream = new MemoryStream();
        bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] buff = stream.ToArray();
        FileStream fs = new FileStream(Server.MapPath(DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"), FileMode.Create);
        stream.WriteTo(fs);
        stream.Close();
        fs.Close();
    }
}


HtmlToImage.cs
C# code

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

/// <summary>
/// 生成网页快照
/// </summary>
public class HtmlToImg
{
    Bitmap m_Bitmap;
    string m_Url;
    int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight;

    public HtmlToImg(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
    {
        m_Url = Url;
        m_BrowserHeight = BrowserHeight;
        m_BrowserWidth = BrowserWidth;
        m_ThumbnailWidth = ThumbnailWidth;
        m_ThumbnailHeight = ThumbnailHeight;
    }

    public static Bitmap GetHtmlToImg(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
    {
        HtmlToImg thumbnailGenerator = new HtmlToImg(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);
        return thumbnailGenerator.GenerateHtmlToImgImage();
    }

    public Bitmap GenerateHtmlToImgImage()
    {
        Thread m_thread = new Thread(new ThreadStart(_GenerateHtmlToImgImage));
        m_thread.SetApartmentState(ApartmentState.STA);
        m_thread.Start();
        m_thread.Join();
        return m_Bitmap;
    }

    private void _GenerateHtmlToImgImage()
    {
        WebBrowser m_WebBrowser = new WebBrowser();
        m_WebBrowser.ScrollBarsEnabled = false;
        m_WebBrowser.Navigate(m_Url);
        m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
        while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
            Application.DoEvents();
        m_WebBrowser.Dispose();
    }

    private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser m_WebBrowser = (WebBrowser)sender;
        m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
        m_WebBrowser.ScrollBarsEnabled = false;
        m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
        m_WebBrowser.BringToFront();
        m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
        m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
    }

}




------解决方案--------------------
你可以试试WebBrowser 的Document对象的属性啊

webBrowser.Document.Body.ScrollRectangle.Height
or
webBrowser.Document.Body.OffsetRectangle.Bottom
------解决方案--------------------
盛情难却呀,我来顶一下