获取 ASP.NET C# 2.0 网站缩略图截图






4.81/5 (32投票s)
如何在 VS 2005 中使用 C#.NET 2.0 获取网站/URL 的缩略图/截图。
示例应用程序截图
引言
这段代码/网站获取特定 URL 的缩略图/截图,可以指定浏览器尺寸和缩略图尺寸。
背景
好吧,厌倦了寻找获取 URL 截图的方法,却只找到 Windows.Forms 的示例,使用简单的 DrawToBitmap
方法。我意识到在 ASP.NET 中,由于单线程和多线程的限制,这行不通。所以我使用了 .SetApartmentState(ApartmentState.STA)
来使其成为单线程。
Using the Code
只需调用您想要与 ASP.NET Image
控件的 ImageUrl
关联的方法即可。
服务器端
使用 IHttpHandler 方法调用
ImageBox.ImageUrl = "~/HandlerWSThumb.ashx?url=" +
Server.UrlEncode(txtURL.Text).ToLower().ToString() + "&bw=" +
txtBW.Text + "&bh=" + txtBH.Text + "&tw=" +
txtTW.Text + "&th=" + txtTH.Text;
使用类方法调用
Bitmap bmp = ClassWSThumb.GetWebSiteThumbnail(txtURL.Text, Int32.Parse(txtBW.Text),
Int32.Parse(txtBH.Text), Int32.Parse(txtTW.Text),
Int32.Parse(txtTH.Text));
bmp.Save(Server.MapPath("~") + "/thumbnail.bmp");
ImageBox.ImageUrl = "~/thumbnail.bmp";
//**if Jpeg is the image format you need just replace the 2 lines above for:
//
//bmp.Save(Server.MapPath("~") + "/thumbnail.jpg", ImageFormat.Jpeg);
//ImageBox.ImageUrl = "~/thumbnail.jpg";
//** but don't forget to add the reference
// to the class in the page 'Default.aspx'(code behind):
//using System.Drawing.Imaging;
这是类方法的代码
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace GetWebSiteThumb
{
public class ClassWSThumb
{
public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth,
int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
{
return new WSThumb(Url, BrowserWidth, BrowserHeight,
ThumbnailWidth, ThumbnailHeight).GetWSThumb();
}
private class WSThumb
{
public WSThumb(string Url, int BW, int BH, int TW, int TH) {
__Url = Url;
__BrowserWidth = BW;
__BrowserHeight = BH;
__ThumbnailWidth = TW;
__ThumbnailHeight = TH;
}
private Bitmap __Bitmap = null;
private string __Url = null;
private int __ThumbnailWidth;
private int __ThumbnailHeight;
private int __BrowserWidth;
private int __BrowserHeight;
public string Url {
get{return __Url;}
set{__Url = value;}
}
public Bitmap ThumbnailImage {
get{return __Bitmap;}
}
public int ThumbnailWidth {
get{return __ThumbnailWidth;}
set{__ThumbnailWidth = value;}
}
public int ThumbnailHeight {
get {return __ThumbnailHeight;}
set {__ThumbnailHeight = value;}
}
public int BrowserWidth {
get{return __BrowserWidth;}
set{__BrowserWidth = value;}
}
public int BrowserHeight {
get{return __BrowserHeight;}
set{__BrowserHeight = value;}
}
public Bitmap GetWSThumb() {
ThreadStart __threadStart = new ThreadStart(_GenerateWSThumb);
Thread __thread = new Thread(__threadStart);
__thread.SetApartmentState(ApartmentState.STA);
__thread.Start();
__thread.Join();
return __Bitmap;
}
private void _GenerateWSThumb() {
WebBrowser __WebBrowser = new WebBrowser();
__WebBrowser.ScrollBarsEnabled = false;
__WebBrowser.Navigate(__Url);
__WebBrowser.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (__WebBrowser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
__WebBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e) {
WebBrowser __WebBrowser = (WebBrowser)sender;
__WebBrowser.ClientSize = new Size(this.__BrowserWidth, this.__BrowserHeight);
__WebBrowser.ScrollBarsEnabled = false;
__Bitmap = new Bitmap(__WebBrowser.Bounds.Width, __WebBrowser.Bounds.Height);
__WebBrowser.BringToFront();
__WebBrowser.DrawToBitmap(__Bitmap, __WebBrowser.Bounds);
if (__ThumbnailHeight != 0 && __ThumbnailWidth !=0)
__Bitmap = (Bitmap)__Bitmap.GetThumbnailImage(
__ThumbnailWidth, __ThumbnailHeight, null, IntPtr.Zero);
}
}
}
}
这是 IHTTPHandler
方法的代码
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Reflection;
namespace GetWebSiteThumb
{
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class HandlerWSThumb : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Bitmap thumb = null;
string url = null;
int bw = 800; //valor por defeito
int bh = 600; //valor por defeito
int tw = 0; // sem thumbnail
int th = 0;
context.Response.ContentType = "image/jpeg";
if (context.Request["url"] != null)
{
if (context.Request["url"].ToString().ToLower().Contains("http://") ||
context.Request["url"].ToString().ToLower().Contains("https://"))
url = context.Request["url"].ToString();
else
url = "http://" + context.Request["url"].ToString();
}
if (context.Request["bw"] != null)
bw = Int32.Parse(context.Request["bw"].ToString());
if (context.Request["bh"] != null)
bh = Int32.Parse(context.Request["bh"].ToString());
if (context.Request["tw"] != null)
tw = Int32.Parse(context.Request["tw"].ToString());
if (context.Request["th"] != null)
th = Int32.Parse(context.Request["th"].ToString());
// return context bitmap
thumb = GetWebSiteThumbnail(url, bw, bh);
if (tw != 0 && th != 0)
thumb.GetThumbnailImage(tw, th, null, IntPtr.Zero).Save(
context.Response.OutputStream, ImageFormat.Jpeg);
else
thumb.Save(context.Response.OutputStream, ImageFormat.Jpeg);
thumb.Dispose();
}
public static Bitmap GetWebSiteThumbnail(string Url,
int BrowserWidth, int BrowserHeight)
{
WebsiteThumbnailImage thumbnailGenerator =
new WebsiteThumbnailImage(Url, BrowserWidth, BrowserHeight);
return thumbnailGenerator.GenerateWebSiteThumbnailImage();
}
private class WebsiteThumbnailImage
{
public WebsiteThumbnailImage(string Url,
int BrowserWidth, int BrowserHeight)
{
this.m_Url = Url;
this.m_BrowserWidth = BrowserWidth;
this.m_BrowserHeight = BrowserHeight;
}
private string m_Url = null;
public string Url
{
get { return m_Url; }
set { m_Url = value; }
}
private Bitmap m_Bitmap = null;
public Bitmap ThumbnailImage
{
get { return m_Bitmap; }
}
private int m_BrowserWidth;
public int BrowserWidth
{
get { return m_BrowserWidth; }
set { m_BrowserWidth = value; }
}
private int m_BrowserHeight;
public int BrowserHeight
{
get { return m_BrowserHeight; }
set { m_BrowserHeight = value; }
}
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);
}
}
// Resusable flag
public bool IsReusable
{
get { return false; }
}
}
}
关注点
我了解到做事总是三种方式:正确的方式、错误的方式和最好的方式。我尝试涵盖这三种方式;作为补充,您可以构建一个 WebService 方法来返回缩略图。也许将来我会编辑这篇文章,并在示例中添加一些代码来实现它。
历史
- 版本 1.0:欢迎提出升级和建议。
- 版本 1.1:添加了一条关于将 类方法 中生成的 BMP 转换为 JPEG 的注释,应一位用户的要求。