ASP.NET實現(xiàn)根據(jù)URL生成網(wǎng)頁縮略圖的方法
本文實例講述了ASP.NET實現(xiàn)根據(jù)URL生成網(wǎng)頁縮略圖的方法。分享給大家供大家參考,具體如下:
工作中需要用到根據(jù)URL生成網(wǎng)頁縮略圖功能,提前做好準(zhǔn)備。
在網(wǎng)上找了份源碼,但是有錯誤:當(dāng)前線程不在單線程單元中,因此無法實例化 ActiveX 控件“8856f961-340a-11d0-a9”,解決后運(yùn)行良好,記錄在此備用!
起始頁: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="生成網(wǎng)頁縮略圖"/>
</div>
</form>
</body>
</html>
調(diào)用頁: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>無標(biāo)題頁</title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
PS:紅色字體部分是為解決錯誤增加的代碼,強(qiáng)制程序在單線程環(huán)境下運(yùn)行!
調(diào)用頁: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程序設(shè)計有所幫助。
- ASP.NET創(chuàng)建動態(tài)縮略圖的方法
- asp.net生成縮略圖示例方法分享
- asp.net中生成縮略圖并添加版權(quán)實例代碼
- asp.net生成縮略圖實現(xiàn)代碼
- asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
- asp.net 生成縮略圖代碼
- asp.net 上傳圖片并同時生成縮略圖的代碼
- asp.net 點縮略圖彈出隨圖片大小自動調(diào)整的頁面
- ASP.Net 上傳圖片并生成高清晰縮略圖
- asp.net生成高質(zhì)量縮略圖通用函數(shù)(c#代碼),支持多種生成方式
- ASP.NET中高質(zhì)量縮略圖的生成代碼
- asp.net圖片上傳生成縮略圖的注意事項
相關(guān)文章
未處理的事件"PageIndexChanging" 之解決方案
今天我寫一個小程序遇到這個問題,上網(wǎng)搜了一下,已經(jīng)有很好的解決方法了,以前都是拉控件自己生成,現(xiàn)在用代碼自己寫就出現(xiàn)了這個問題2008-07-07
CHECKBOX 的全選、取消及跨頁保存的實現(xiàn)方法
CHECKBOX的操作在頁面中很常見,比如全選、取消、跨頁保存等等,下面有個不錯的示例,大家可以嘗試操作下2013-10-10
在IIS上部署ASP.NET Core Web API的方法步驟
這篇文章主要介紹了在IIS上部署ASP.NET Core Web API的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
asp.net上傳execl文件后,在頁面上加載顯示(示例代碼)
本篇文章主要是對asp.net上傳execl文件后,在頁面上加載顯示(示例代碼)進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
.NET使用Collections.Pooled提升性能優(yōu)化的方法
這篇文章主要介紹了.NET使用Collections.Pooled性能優(yōu)化的方法,今天要給大家分享類庫Collections.Pooled,它是通過池化內(nèi)存來達(dá)到降低內(nèi)存占用和GC的目的,另外也會帶大家看看源碼,為什么它會帶來這些性能提升,一起通過本文學(xué)習(xí)下吧2022-05-05
.NET+JS對用戶輸入內(nèi)容進(jìn)行字?jǐn)?shù)提示功能的實例代碼
.NET+JS對用戶輸入內(nèi)容進(jìn)行字?jǐn)?shù)提示功能的實例代碼,需要的朋友可以參考一下2013-06-06
C# web api返回類型設(shè)置為json的兩種方法
web api寫api接口時默認(rèn)返回的是把你的對象序列化后以XML形式返回,那么怎樣才能讓其返回為json呢,下面為大家介紹幾種不錯的方法2014-02-02

