试用http://121.18.78.216/ 希望各位提宝贵意见(自己的讨论群152524724),谢谢
实现思路:
1、通过WebBrower抓取网页的图片
2、通过新线程实现ASP.NET使用WinForm组件
3、通过页面输出图片
源码:
1、通过WebBrower抓取网页的图片
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;
/// <summary>
/// GetImage 的摘要说明
/// </summary>
namespace WWW.SnapMap
{
public class GetImage
{
/// <summary>
/// 宽度
/// </summary>
private int ImageWidth;
/// <summary>
/// 高度
/// </summary>
private int ImageHeight;
/// <summary>
/// URL地址
/// </summary>
private string WebSite;
/// <summary>
/// 构造
/// </summary>
/// <param name="webSite"></param>
/// <param name="imageWidth"></param>
/// <param name="imageHeight"></param>
public GetImage(string webSite, int imageWidth, int imageHeight)
{
this.WebSite = webSite;
this.ImageWidth = imageWidth;
this.ImageHeight = imageHeight;
}
/// <summary>
/// 获得图片
/// </summary>
/// <returns></returns>
public byte[] GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite);
Shot.GetIt();
Bitmap Pic = Shot.DrawBitmap(this.ImageWidth, this.ImageHeight);
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(1000);
}
}
public Bitmap DrawBitmap(int imgWidth, int imgHeight)
{
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;
}
if (DocumentHeight < imgHeight)
{
imgHeight = DocumentHeight;
}
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;
}
finally
{
MyBitmap.Dispose();
MyBrowser.Dispose();
MyBrowser = null;
}
}
}
}
2、通过新线程实现ASP.NET使用WinForm组件
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;
using MyQuery.Utils;
namespace WWW.SnapMap
{
public partial class GetMap : System.Web.UI.Page
{
private AutoResetEvent autoEvent = new AutoResetEvent(false);
private byte[] content = null;
private string url=http://www.baidu.com;
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
mapIP = WebHelper.GetAppConfig("GetMapIP");
ThreadStart start = new ThreadStart(getContent);
Thread t = new Thread(start);
t.SetApartmentState(ApartmentState.STA);
t.Priority = ThreadPriority.Highest;
t.Start();
autoEvent.WaitOne(2000, false);
while (content == null)
{
//等待线程结果;
autoEvent.WaitOne(2000, false);
}
autoEvent.WaitOne(2000, false);
Response.BinaryWrite(content);
Response.End();
}
private void getContent()
{
GetImage image = new GetImage(url, 800,600);
content = image.GetBitmap();
autoEvent.Set();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetMap.aspx.cs" Inherits="WWW.SnapMap.GetMap" %>
3、通过页面输出图片
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowMap.aspx.cs" Inherits="WWW.SnapMap.ShowMap" %>
<!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></title>
</head>
<body οncοntextmenu="event.returnValue=false">
<form id="form1" runat="server">
<div align="center">
<img alt="图片" src=“GetMap.aspx" border="0"/>
</div>
</form>
</body>
</html>