ASP.NET实现根据URL生成网页缩略图的方法
本文实例讲述了ASP.NET实现根据URL生成网页缩略图的方法。分享给大家供大家参考,具体如下:
工作中需要用到根据URL生成网页缩略图功能,提前做好准备。
在网上找了份源码,但是有错误:当前线程不在单线程单元中,因此无法实例化ActiveX控件“8856f961-340a-11d0-a9”,解决后运行良好,记录在此备用!
起始页:Default.aspx
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="CaptureToImage._Default"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>Snap</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<inputtype="button"onclick="window.open('Snap.aspx?url=www.njude.com.cn')"value="生成网页缩略图"/>
</div>
</form>
</body>
</html>
调用页:Snap.aspx
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Snap.aspx.cs"Inherits="CaptureToImage.Snap"AspCompat="true"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server"> <title>无标题页</title> </head> <body> <formid="form1"runat="server"> <div> </div> </form> </body> </html>
PS:红色字体部分是为解决错误增加的代码,强制程序在单线程环境下运行!
调用页:Snap.aspx.cs
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Drawing.Imaging;
namespaceCaptureToImage
{
publicpartialclassSnap:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
stringurl=string.Empty;
url=Request.QueryString[0];
try
{
GetImagethumb=newGetImage(url,1024,768,800,600);
System.Drawing.Bitmapx=thumb.GetBitmap();
x.Save(Response.OutputStream,ImageFormat.Jpeg);
Response.ContentType="image/jpeg";
}
catch(Exceptionex)
{
Response.Write(ex.Message);
}
}
}
}
类文件:GetImage.cs
usingSystem;
usingSystem.Drawing;
usingSystem.Drawing.Imaging;
usingSystem.Windows.Forms;
usingSystem.Web.UI;
namespaceCaptureToImage
{
publicclassGetImage
{
intS_Height;
intS_Width;
intF_Height;
intF_Width;
stringMyURL;
publicintScreenHeight
{
get
{
returnS_Height;
}
set
{
S_Height=value;
}
}
publicintScreenWidth
{
get
{
returnS_Width;
}
set
{
S_Width=value;
}
}
publicintImageHeight
{
get
{
returnF_Height;
}
set
{
F_Height=value;
}
}
publicintImageWidth
{
get
{
returnF_Width;
}
set
{
F_Width=value;
}
}
publicstringWebSite
{
get
{
returnMyURL;
}
set
{
MyURL=value;
}
}
publicGetImage(stringWebSite,intScreenWidth,intScreenHeight,intImageWidth,intImageHeight)
{
this.WebSite=WebSite;
this.ScreenHeight=ScreenHeight;
this.ScreenWidth=ScreenWidth;
this.ImageHeight=ImageHeight;
this.ImageWidth=ImageWidth;
}
[STAThread]
publicBitmapGetBitmap()
{
WebPageBitmapShot=newWebPageBitmap(this.WebSite,this.ScreenWidth,this.ScreenHeight);
Shot.GetIt();
BitmapPic=Shot.DrawBitmap(this.ImageHeight,this.ImageWidth);
returnPic;
}
}
publicclassWebPageBitmap
{
WebBrowserMyBrowser;
stringURL;
intHeight;
intWidth;
publicWebPageBitmap(stringurl,intwidth,intheight)
{
this.URL=url;
this.Width=width;
this.Height=height;
MyBrowser=newWebBrowser();
MyBrowser.ScrollBarsEnabled=false;
MyBrowser.Size=newSize(this.Width,this.Height);
}
publicvoidGetIt()
{
NavigateUrl(this.URL);
while(MyBrowser.ReadyState!=WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
publicdelegatevoidDelUserHandler(stringurl);
publicvoidNavigateUrl(stringurl)
{
try
{
if(this.MyBrowser.InvokeRequired)
{
DelUserHandlerhandler=newDelUserHandler(NavigateUrl);
MyBrowser.Invoke(handler,url);
}
else
{
this.MyBrowser.Navigate(url);
}
}
catch(Exceptionex)
{
thrownewException("NavigateUrl()"+ex.Message);
}
}
publicBitmapDrawBitmap(inttheight,inttwidth)
{
BitmapmyBitmap=newBitmap(this.Width,this.Height);
RectangleDrawRect=newRectangle(0,0,this.Width,this.Height);
MyBrowser.DrawToBitmap(myBitmap,DrawRect);
System.Drawing.ImageimgOutput=myBitmap;
System.Drawing.BitmapoThumbNail=newBitmap(twidth,theight,imgOutput.PixelFormat);
Graphicsg=Graphics.FromImage(oThumbNail);
g.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
RectangleoRectangle=newRectangle(0,0,twidth,theight);
g.DrawImage(imgOutput,oRectangle);
try
{
returnoThumbNail;
}
catch
{
returnnull;
}
finally
{
imgOutput.Dispose();
imgOutput=null;
MyBrowser.Dispose();
MyBrowser=null;
}
}
}
}
PS:项目中需要添加引用System.Windows.Forms
希望本文所述对大家asp.net程序设计有所帮助。
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志