ASP.NET根据URL生成网页缩略图示例程序(C#语言)
2012-02-16 11:28
806 查看
工作中可能马上要用到根据URL生成网页缩略图功能,提前做好准备。
在网上找了份源码,但是有错误:当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a9”,解决后运行良好,记录在此备用!
起始页:Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CaptureToImage._Default" %>
<!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 id="Head1" runat="server">
<title>Snap</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="window.open('Snap.aspx?url=www.njude.com.cn')" value="生成网页缩略图"/>
</div>
</form>
</body>
</html>
调用页:Snap.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Snap.aspx.cs" Inherits="CaptureToImage.Snap" AspCompat="true" %>
<!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>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
PS:红色字体部分是为解决错误增加的代码,强制程序在单线程环境下运行!
调用页:Snap.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
namespace CaptureToImage
{
public partial class Snap : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string url = string.Empty;
url = Request.QueryString[0];
try
{
GetImage thumb = new GetImage(url, 1024, 768, 800, 600);
System.Drawing.Bitmap x = thumb.GetBitmap();
x.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.ContentType = "image/jpeg";
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
类文件:GetImage.cs
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Web.UI;
namespace CaptureToImage
{
public class GetImage
{
int S_Height;
int S_Width;
int F_Height;
int F_Width;
string MyURL;
public int ScreenHeight
{
get
{
return S_Height;
}
set
{
S_Height = value;
}
}
public int ScreenWidth
{
get
{
return S_Width;
}
set
{
S_Width = value;
}
}
public int ImageHeight
{
get
{
return F_Height;
}
set
{
F_Height = value;
}
}
public int ImageWidth
{
get
{
return F_Width;
}
set
{
F_Width = value;
}
}
public string WebSite
{
get
{
return MyURL;
}
set
{
MyURL = value;
}
}
public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)
{
this.WebSite = WebSite;
this.ScreenHeight = ScreenHeight;
this.ScreenWidth = ScreenWidth;
this.ImageHeight = ImageHeight;
this.ImageWidth = ImageWidth;
}
[STAThread]
public Bitmap GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
Shot.GetIt();
Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
return Pic;
}
}
public class WebPageBitmap
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width;
public WebPageBitmap(string url, int width, int height)
{
this.URL = url;
this.Width = width;
this.Height = height;
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
MyBrowser.Size = new Size(this.Width, this.Height);
}
public void GetIt()
{
NavigateUrl(this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
public delegate void DelUserHandler(string url);
public void NavigateUrl(string url)
{
try
{
if (this.MyBrowser.InvokeRequired)
{
DelUserHandler handler = new DelUserHandler(NavigateUrl);
MyBrowser.Invoke(handler, url);
}
else
{
this.MyBrowser.Navigate(url);
}
}
catch (Exception ex)
{
throw new Exception("NavigateUrl()" + ex.Message);
}
}
public Bitmap DrawBitmap(int theight, int twidth)
{
Bitmap myBitmap = new Bitmap(this.Width, this.Height);
Rectangle DrawRect = new Rectangle(0, 0, this.Width, this.Height);
MyBrowser.DrawToBitmap(myBitmap, DrawRect);
System.Drawing.Image imgOutput = myBitmap;
System.Drawing.Bitmap oThumbNail = new Bitmap(twidth, theight, imgOutput.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, twidth, theight);
g.DrawImage(imgOutput, oRectangle);
try
{
return oThumbNail;
}
catch
{
return null;
}
finally
{
imgOutput.Dispose();
imgOutput = null;
MyBrowser.Dispose();
MyBrowser = null;
}
}
}
}
PS:项目中需要添加引用System.Windows.Forms
在网上找了份源码,但是有错误:当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a9”,解决后运行良好,记录在此备用!
起始页:Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CaptureToImage._Default" %>
<!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 id="Head1" runat="server">
<title>Snap</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="window.open('Snap.aspx?url=www.njude.com.cn')" value="生成网页缩略图"/>
</div>
</form>
</body>
</html>
调用页:Snap.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Snap.aspx.cs" Inherits="CaptureToImage.Snap" AspCompat="true" %>
<!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>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
PS:红色字体部分是为解决错误增加的代码,强制程序在单线程环境下运行!
调用页:Snap.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
namespace CaptureToImage
{
public partial class Snap : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string url = string.Empty;
url = Request.QueryString[0];
try
{
GetImage thumb = new GetImage(url, 1024, 768, 800, 600);
System.Drawing.Bitmap x = thumb.GetBitmap();
x.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.ContentType = "image/jpeg";
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
类文件:GetImage.cs
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Web.UI;
namespace CaptureToImage
{
public class GetImage
{
int S_Height;
int S_Width;
int F_Height;
int F_Width;
string MyURL;
public int ScreenHeight
{
get
{
return S_Height;
}
set
{
S_Height = value;
}
}
public int ScreenWidth
{
get
{
return S_Width;
}
set
{
S_Width = value;
}
}
public int ImageHeight
{
get
{
return F_Height;
}
set
{
F_Height = value;
}
}
public int ImageWidth
{
get
{
return F_Width;
}
set
{
F_Width = value;
}
}
public string WebSite
{
get
{
return MyURL;
}
set
{
MyURL = value;
}
}
public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)
{
this.WebSite = WebSite;
this.ScreenHeight = ScreenHeight;
this.ScreenWidth = ScreenWidth;
this.ImageHeight = ImageHeight;
this.ImageWidth = ImageWidth;
}
[STAThread]
public Bitmap GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
Shot.GetIt();
Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
return Pic;
}
}
public class WebPageBitmap
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width;
public WebPageBitmap(string url, int width, int height)
{
this.URL = url;
this.Width = width;
this.Height = height;
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
MyBrowser.Size = new Size(this.Width, this.Height);
}
public void GetIt()
{
NavigateUrl(this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
public delegate void DelUserHandler(string url);
public void NavigateUrl(string url)
{
try
{
if (this.MyBrowser.InvokeRequired)
{
DelUserHandler handler = new DelUserHandler(NavigateUrl);
MyBrowser.Invoke(handler, url);
}
else
{
this.MyBrowser.Navigate(url);
}
}
catch (Exception ex)
{
throw new Exception("NavigateUrl()" + ex.Message);
}
}
public Bitmap DrawBitmap(int theight, int twidth)
{
Bitmap myBitmap = new Bitmap(this.Width, this.Height);
Rectangle DrawRect = new Rectangle(0, 0, this.Width, this.Height);
MyBrowser.DrawToBitmap(myBitmap, DrawRect);
System.Drawing.Image imgOutput = myBitmap;
System.Drawing.Bitmap oThumbNail = new Bitmap(twidth, theight, imgOutput.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, twidth, theight);
g.DrawImage(imgOutput, oRectangle);
try
{
return oThumbNail;
}
catch
{
return null;
}
finally
{
imgOutput.Dispose();
imgOutput = null;
MyBrowser.Dispose();
MyBrowser = null;
}
}
}
}
PS:项目中需要添加引用System.Windows.Forms
相关文章推荐
- ASP.NET根据URL生成网页缩略图示例程序(C#语言)
- ASP.NET根据URL生成网页缩略图示例程序(C#语言)
- ASP.NET实现根据URL生成网页缩略图的方法
- ASP.NET实现根据URL生成网页缩略图的方法
- ASP.NET实现根据URL生成网页缩略图的方法
- ASP.NET C#上传图片时,根据选中的内容生成缩略图
- Asp.NET生成各种网页快捷方式(桌面url快捷方式,收藏夹/开始菜单快捷方式)(转帖)
- ASP.NET生成高质量缩略图通用函数(c#代码)
- asp.net c# 程序中生成控件
- ASP.NET生成高质量缩略图通用函数(c#代码)
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- ASP.NET的路由系统:根据路由规则生成URL
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式
- 在ASP.NET中上传图片并生成缩略图的C#源码
- C#(ASP.NET) 下载数据 C#实现通过程序自动抓取远程Web网页信息
- [收藏]ASP.NET生成高质量缩略图通用函数(VB.NET、c#代码)
- asp.net生成高质量缩略图通用函数(c#代码),
- asp.net验证码生成类(C#语言)(参考)