首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

【二百分】页面高度有关问题

2012-08-13 
【二百分求助】页面高度问题百分结贴,再加上这个帖子:共200分。抓取网页快照的时候,下面的语句只能抓取指定尺

【二百分求助】页面高度问题
百分结贴,再加上这个帖子:共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.CollectionsImports System.ConfigurationImports System.DataImports System.WebImports System.Web.SecurityImports System.Web.UIImports System.Web.UI.HtmlControlsImports System.Web.UI.WebControlsImports System.Web.UI.WebControls.WebPartsImports System.DrawingImports System.Drawing.ImagingImports System.ThreadingPublic Class Form1    Private _webBrowser As System.Windows.Forms.WebBrowser    Public pic As String    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Dim tb As New Data.DataTable        tb = code.Data_DataTable("select a_url,ID from a_domain order by id").Tables(0)        ProgressBar1.Maximum = tb.Rows.Count        ProgressBar1.Minimum = 0        Dim i As Integer        For i = 0 To tb.Rows.Count - 1            Dim url As String = tb.Rows(i).Item("a_url")            Dim id As Integer = tb.Rows(i).Item("id")            pic = "b_" & id & "_" & DateTime.Now.ToString("yyyyMMdd") & ".jpg"            _webBrowser = New System.Windows.Forms.WebBrowser()            _webBrowser.ScrollBarsEnabled = False    '不显示滚动条            _webBrowser.Navigate(url)            AddHandler _webBrowser.DocumentCompleted, New System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(AddressOf Completed)            While _webBrowser.ReadyState <> System.Windows.Forms.WebBrowserReadyState.Complete                '避免假死,若去掉则可能无法触发 DocumentCompleted 事件。                System.Windows.Forms.Application.DoEvents()            End While            code.Data_update("insert into a_domain_pic(a_id,a_img)values('" & id & "','" & pic & "')")            ProgressBar1.Value += 1        Next    End Sub    Public Sub Completed(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)        '设置浏览器宽度、高度为文档宽度、高度,以便截取整个网页。        Dim fileName As String = "C:\img\" & pic        '_webBrowser.Width = _webBrowser.Document.Body.ScrollRectangle.Width        '_webBrowser.Height = _webBrowser.Document.Body.ScrollRectangle.Height        _webBrowser.Width = 1000        _webBrowser.Height = 1000        Using bmp As New Bitmap(_webBrowser.Width, _webBrowser.Height)            _webBrowser.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))            bmp.Save(fileName, ImageFormat.Jpeg)            Dim originalFilename As String = fileName            Dim strGoodFile As String = fileName.Replace("b_", "s_")            '从文件取得图片对象             Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(originalFilename)            '取得图片大小             'Dim size As System.Drawing.Size = New Drawing.Size(image.Width \ iScale, image.Height \ iScale)            Dim w As Integer = 150            Dim h As Integer = 150            '新建一个bmp图片             'Dim bitmap As System.Drawing.Image = New System.Drawing.Bitmap(size.Width, size.Height)            Dim bitmap As System.Drawing.Image = New System.Drawing.Bitmap(w, h)            '新建一个画板             Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap)            '设置高质量插值法             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High            '设置高质量,低速度呈现平滑程度             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality            '清空一下画布             g.Clear(Drawing.Color.Transparent)            '在指定位置画图             g.DrawImage(image, New System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), New System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.GraphicsUnit.Pixel)            '保存高清晰度的缩略图             bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg)            g.Dispose()        End Using    End Sub    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click        End    End SubEnd Class 


[解决办法]
因为你这个代码我运行了有图片这个文件,但没截下来,所以我在网上找了个和你这个相似的。如果你那个代码在你那截图截的下来的话,你只要改下这个类代码即可。如下所示:
 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 m_WebBrowser = (WebBrowser)sender;
m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
m_WebBrowser.ScrollBarsEnabled = false ;
m_WebBrowser.Height = m_WebBrowser.Document.Body.ScrollRectangle.Height + 20;
m_WebBrowser.Width =1024;
m_Bitmap = new Bitmap(m_WebBrowser.Width, m_WebBrowser.Height);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_WebBrowser.Width, m_WebBrowser.Height, null, IntPtr.Zero);

}





如果有什么问题,你把我这所有代码在另外复制下运行看看!
webform1里的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;


namespace WebApplication8
{
public partial class WebForm1 : 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();
Bitmap m_Bitmap = HtmlToImg.GetWebSiteThumbnail("http://z.firefoxchina.cn", 600, 600, 600, 600);
MemoryStream ms = new MemoryStream();
m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//JPG、GIF、PNG等均可
byte[] buff = ms.ToArray();
//Response.BinaryWrite(buff);
FileStream fs = new FileStream(Server.MapPath(DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"), FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();

}
}
}
类里面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace WebApplication8
{
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 GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
{
HtmlToImg thumbnailGenerator = new HtmlToImg(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);
return thumbnailGenerator.GenerateWebSiteThumbnailImage();
}
public Bitmap GenerateWebSiteThumbnailImage()
{
Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
private void _GenerateWebSiteThumbnailImage()
{
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 m_WebBrowser = (WebBrowser)sender;
m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
m_WebBrowser.ScrollBarsEnabled = false ;
m_WebBrowser.Height = m_WebBrowser.Document.Body.ScrollRectangle.Height + 20;
m_WebBrowser.Width =1024;
m_Bitmap = new Bitmap(m_WebBrowser.Width, m_WebBrowser.Height);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_WebBrowser.Width, m_WebBrowser.Height, null, IntPtr.Zero);

}

}
}
[解决办法]
你加下我QQ,我回复你的私信里有我号码

热点排行