日期:2014-05-17  浏览次数:20490 次

【二百分求助】页面高度问题
百分结贴,再加上这个帖子:共200分。
抓取网页快照的时候,下面的语句只能抓取指定尺寸的图片,有时候抓不全,有时候留下好多空白:
Bitmap bmp = HtmlToImg.GetHtmlToImg("http://www.baidu.com", 600, 500, 600, 500);
怎样直接获取被抓取页面的宽和高?WebBrowser的Document对象的属性WebBrowser.Document.Body.ScrollRectangle.Height该怎么用?
我不是程序员,对net不够熟练,谁能直接修改下面的代码帮我实现抓取完整页面(直接抓取和网页宽和高相同的图片):
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);
    }
}



------解决方案--------------------
用我的代码!
你自己转C#吧

VB.NET code

Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Web
Imports System.Web.Securit