使用WebBrower实现ASP.NET抓图 但不知为何出现空白

jsyhello 2011-04-01 04:57:41
代码如下:GetImage.cs
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;

/// <summary>
/// GetImage 的摘要说明
/// </summary>
namespace Wap.Map
{
public class GetImage
{
int F_Width;
string MyURL;
/// <summary>
/// 宽度
/// </summary>
public int ImageWidth
{
get
{
return F_Width;
}
set
{
F_Width = value;
}
}
/// <summary>
/// URL地址
/// </summary>
public string WebSite
{
get
{
return MyURL;
}
set
{
MyURL = value;
}
}
/// <summary>
/// 构造
/// </summary>
/// <param name="WebSite"></param>
/// <param name="ImageWidth"></param>
public GetImage(string WebSite, int ImageWidth)
{
this.WebSite = WebSite;
this.ImageWidth = ImageWidth;
}
/// <summary>
/// 获得图片
/// </summary>
/// <returns></returns>
public byte[] GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite);
Shot.GetIt();
Bitmap Pic = Shot.DrawBitmap(this.ImageWidth);
return imageToByteArray(Pic);
}
/// <summary>
/// 获得输出
/// </summary>
/// <param name="bitMap"></param>
/// <returns></returns>
private byte[] imageToByteArray(System.Drawing.Bitmap bitMap)
{
MemoryStream ms = new MemoryStream();
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
return ms.ToArray();
}
/// <summary>
/// 获得图片类型
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private System.Drawing.Imaging.ImageFormat getImgFormat(string fileName)
{
fileName = Path.GetExtension(fileName).TrimStart('.').ToLower();
if (fileName.Equals("bmp"))
{
return System.Drawing.Imaging.ImageFormat.Bmp;
}
if (fileName.Equals("gif"))
{
return System.Drawing.Imaging.ImageFormat.Gif;
}
if (fileName.Equals("jpg") || fileName.Equals("jpeg"))
{
return System.Drawing.Imaging.ImageFormat.Jpeg;
}
if (fileName.Equals("png"))
{
return System.Drawing.Imaging.ImageFormat.Png;
}
if (fileName.Equals("tiff"))
{
return System.Drawing.Imaging.ImageFormat.Tiff;
}
return System.Drawing.Imaging.ImageFormat.Jpeg;
}

}
/// <summary>
/// 抓网页
/// </summary>
public class WebPageBitmap
{
System.Windows.Forms.WebBrowser MyBrowser;
string URL;
public WebPageBitmap(string url)
{
this.URL = url;
MyBrowser = new System.Windows.Forms.WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
MyBrowser.Visible = false;
MyBrowser.Size = new Size(1, 1);
}

public void GetIt()
{
MyBrowser.Navigate(this.URL, false);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
System.Threading.Thread.Sleep(500);
}
}

public Bitmap DrawBitmap(int imgWidth)
{
int DocumentHeight = MyBrowser.Document.Window.Size.Height;
int DocumentWidth = MyBrowser.Document.Window.Size.Width;
MyBrowser.Size = new Size(DocumentWidth, DocumentHeight);
if (DocumentWidth < imgWidth)
{
imgWidth = DocumentWidth;
}
int imgHeight = System.Convert.ToInt32(Math.Ceiling(DocumentHeight * imgWidth / System.Convert.ToDouble(DocumentWidth)));


Bitmap MyBitmap = new Bitmap(DocumentWidth, DocumentHeight);
Rectangle DrawRect = new Rectangle(0, 0, DocumentWidth, DocumentHeight);
/*
* System.Windows.Forms.WebBrowser继承了System.Windows.Forms.WebBrowserBase
* 而
* System.Windows.Forms.WebBrowserBase继承了System.Windows.Forms.Control
*
* System.Windows.Forms.Control.DrawToBitmap(bitmap,targetBounds)
* 支持呈现到指定的位图
* bitmap 要绘制到的位图。
* targetBounds 呈现控件时的边界。
*
* DrawToBitmap 方法具有下列局限性:
* 可能会针对大位图引发 ArgumentException。允许使用的最大大小因计算机而异。
* DrawToBitmap 不支持 Windows XP Tablet PC Edition 2005 操作系统的 Ink 控件。
* 如果 TextBox 的 Visible 属性设置为 false,则 DrawToBitmap 不绘制子 TextBox。
* 容器内部的控件按相反的顺序呈现。
* 对于 RichTextBox,DrawToBitmap 不能完全发挥作用;只绘制位图的边框
*/
try
{
MyBrowser.DrawToBitmap(MyBitmap, DrawRect);
System.Drawing.Bitmap oThumbNail = new Bitmap(imgWidth, imgHeight, MyBitmap.PixelFormat);
Graphics g = Graphics.FromImage(oThumbNail);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
Rectangle oRectangle = new Rectangle(0, 0, imgWidth, imgHeight);
g.DrawImage(MyBitmap, oRectangle);
g.Dispose();
return oThumbNail;
}
catch
{
return null;
}
finally
{
MyBitmap.Dispose();
MyBrowser.Dispose();
MyBrowser = null;
}
}
}
}
...全文
123 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsyhello76 2011-04-08
  • 打赏
  • 举报
回复
看看这个有帮助吗?
http://topic.csdn.net/u/20070512/23/B45AC41D-06EF-4D15-856C-3EEAE5A86D88.html
jsyhello 2011-04-02
  • 打赏
  • 举报
回复
没人用过?
xrongzhen 2011-04-01
  • 打赏
  • 举报
回复
太长了 先回再看
jsyhello 2011-04-01
  • 打赏
  • 举报
回复
在调试状态下有的页面可以抓到,但在运行状态时空白。有些页面IE访问没有问题,但是抓的图是空白。希望高手指点迷津
jsyhello 2011-04-01
  • 打赏
  • 举报
回复
测试页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Wap.Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="Map/GetMap.aspx" />
</div>
</form>
</body>
</html>
jsyhello 2011-04-01
  • 打赏
  • 举报
回复
形成图片页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetMap.aspx.cs" Inherits="Wap.Map.GetMap" %>
后台代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;


namespace Wap.Map
{
public partial class GetMap : System.Web.UI.Page
{
private AutoResetEvent autoEvent = new AutoResetEvent(false);

private byte[] content = null;

protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;

ThreadStart start = new ThreadStart(getContent);
Thread t = new Thread(start);
t.SetApartmentState(ApartmentState.STA);
t.Priority = ThreadPriority.Highest;
t.Start();

autoEvent.WaitOne(1000, false);
while (content == null)
{
//等待线程结果;
autoEvent.WaitOne(500, false);
}
Response.BinaryWrite(content);
Response.End();
}

private void getContent()
{
string url = "http://www.baidu.com";
GetImage image = new GetImage(url, 230);
content = image.GetBitmap();
autoEvent.Set();
}
}
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧