理解HttpHandler,并為所有*.jpg圖片生成一段文字于圖片上
更新時間:2012年03月12日 16:51:04 作者:
HttpHandler就是最終相應(yīng)HTTP請求,生成HTTP響應(yīng)的處理器,他們的實(shí)例由asp.net運(yùn)行時創(chuàng)建,,并生存在asp.net的運(yùn)行時環(huán)境中,如果asp.net運(yùn)行時是處理請求的工廠,HttpHandler是處理請求的工人
接口IHttpHandler的定義如下:
interface IHttpHandler
{
void ProcessRequest(HttpContext ctx);
bool IsReuseable { get; }
1新建一網(wǎng)站,名為MyHttpHandlerTest
2右擊添加,選擇類庫,取名為MyHttpHandler
3-在上一步新建的類庫上右鍵添加System.Web引用
主要代碼:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace MyHttpHandler
{
public class Class1:IHttpHandler,IRequiresSessionState
{
#region IHttpHandler成員
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Write("handler處理");
}
#endregion
}
}
4-在MyHttpHandler類庫上右鍵,生成,取名為MyHttpHandler
5-在web.config中的system.web節(jié)點(diǎn)中天下如下節(jié)點(diǎn)
<httpHandlers>
<add verb="*" path="Handler1.aspx" type="MyHttpHandler.Class1,MyHttpHandler"/>
<!--
配置文件中的選項說明:
· verb可以是"GET"或"POST",表示對GET或POST的請求進(jìn)行處理。"*"表示對所有請求進(jìn)行處理。
· Path指明對相應(yīng)的文件進(jìn)行處理,"*.aspx"表示對發(fā)給所有ASPX頁面的請求進(jìn)行處理??梢灾该髀窂?,如"/test/*.aspx",表明只對test目錄下的ASPX文件進(jìn)行處理。
· Type屬性中,逗號前的字符串指明HttpHandler的實(shí)現(xiàn)類的類名,后面的字符串指明Dll文件的名稱。
格式如:type="自定義HttpHandler的實(shí)現(xiàn)類的全名,自定義HttpHandler的實(shí)現(xiàn)類的命名空間(即Dll名)"
或 type="自定義HttpHandler的實(shí)現(xiàn)類的全名"
-->
</httpHandlers>
6-在MyHttpHandlerTest右鍵添加引用,選擇項目找到剛才編譯后的.dll文件
7-運(yùn)行Handler1.aspx,頁面顯示:

下面我們利用HttpHandler將一段文字生成于圖片中
添加一個類,默認(rèn)為Class.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
/// <summary>
/// Class1 的摘要說明
/// </summary>
public class Class1:IHttpHandler
{
public Class1()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
public bool IsReusable
{
get { return true; }
}
private static Image OldImage = null;
private static Image GetOldImage(HttpContext context)
{
if (OldImage == null)
{
OldImage = Image.FromFile(context.Server.MapPath("~/Images/Old.jpg"));
}
return OldImage.Clone() as Image;
}
public void ProcessRequest(HttpContext context)
{
Image newimage = GetOldImage(context);
Graphics gh = Graphics.FromImage(newimage);
Font font = new Font("Monaco", 24.0f, FontStyle.Regular);
string writetext = HttpUtility.UrlEncode(context.Request.QueryString["writetext"]);
gh.DrawString(HttpUtility.UrlDecode(writetext), font, new SolidBrush(Color.LightBlue), 20.0f, newimage.Height - font.Height - 30);
newimage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
gh.Dispose();
newimage.Dispose();
}
}
新建一個.aspx頁面,添加一個HyperLink控件,再在其.cs文件中添加一段代碼傳值
protected void Page_Load(object sender, EventArgs e)
{
HyperLink1.NavigateUrl = "img.jpg?writetext=" + HttpUtility.UrlEncode("大蝸牛");
}
另外還需在web.config文件中將httpHandlers節(jié)點(diǎn)中改為如下
<add verb="*" path="*.jpg" type="Class1"/>
表明對所有的.jpg格式的文件才會處理
參考《道不遠(yuǎn)人 深入解析asp.net 2.0控件開發(fā)》
復(fù)制代碼 代碼如下:
interface IHttpHandler
{
void ProcessRequest(HttpContext ctx);
bool IsReuseable { get; }
1新建一網(wǎng)站,名為MyHttpHandlerTest
2右擊添加,選擇類庫,取名為MyHttpHandler
3-在上一步新建的類庫上右鍵添加System.Web引用
主要代碼:
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace MyHttpHandler
{
public class Class1:IHttpHandler,IRequiresSessionState
{
#region IHttpHandler成員
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Write("handler處理");
}
#endregion
}
}
4-在MyHttpHandler類庫上右鍵,生成,取名為MyHttpHandler
5-在web.config中的system.web節(jié)點(diǎn)中天下如下節(jié)點(diǎn)
<httpHandlers>
<add verb="*" path="Handler1.aspx" type="MyHttpHandler.Class1,MyHttpHandler"/>
<!--
配置文件中的選項說明:
· verb可以是"GET"或"POST",表示對GET或POST的請求進(jìn)行處理。"*"表示對所有請求進(jìn)行處理。
· Path指明對相應(yīng)的文件進(jìn)行處理,"*.aspx"表示對發(fā)給所有ASPX頁面的請求進(jìn)行處理??梢灾该髀窂?,如"/test/*.aspx",表明只對test目錄下的ASPX文件進(jìn)行處理。
· Type屬性中,逗號前的字符串指明HttpHandler的實(shí)現(xiàn)類的類名,后面的字符串指明Dll文件的名稱。
格式如:type="自定義HttpHandler的實(shí)現(xiàn)類的全名,自定義HttpHandler的實(shí)現(xiàn)類的命名空間(即Dll名)"
或 type="自定義HttpHandler的實(shí)現(xiàn)類的全名"
-->
</httpHandlers>
6-在MyHttpHandlerTest右鍵添加引用,選擇項目找到剛才編譯后的.dll文件
7-運(yùn)行Handler1.aspx,頁面顯示:

下面我們利用HttpHandler將一段文字生成于圖片中
添加一個類,默認(rèn)為Class.cs
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
/// <summary>
/// Class1 的摘要說明
/// </summary>
public class Class1:IHttpHandler
{
public Class1()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
public bool IsReusable
{
get { return true; }
}
private static Image OldImage = null;
private static Image GetOldImage(HttpContext context)
{
if (OldImage == null)
{
OldImage = Image.FromFile(context.Server.MapPath("~/Images/Old.jpg"));
}
return OldImage.Clone() as Image;
}
public void ProcessRequest(HttpContext context)
{
Image newimage = GetOldImage(context);
Graphics gh = Graphics.FromImage(newimage);
Font font = new Font("Monaco", 24.0f, FontStyle.Regular);
string writetext = HttpUtility.UrlEncode(context.Request.QueryString["writetext"]);
gh.DrawString(HttpUtility.UrlDecode(writetext), font, new SolidBrush(Color.LightBlue), 20.0f, newimage.Height - font.Height - 30);
newimage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
gh.Dispose();
newimage.Dispose();
}
}
新建一個.aspx頁面,添加一個HyperLink控件,再在其.cs文件中添加一段代碼傳值
復(fù)制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
HyperLink1.NavigateUrl = "img.jpg?writetext=" + HttpUtility.UrlEncode("大蝸牛");
}
另外還需在web.config文件中將httpHandlers節(jié)點(diǎn)中改為如下
<add verb="*" path="*.jpg" type="Class1"/>
表明對所有的.jpg格式的文件才會處理
參考《道不遠(yuǎn)人 深入解析asp.net 2.0控件開發(fā)》
相關(guān)文章
使用visual studio自動創(chuàng)建IIS虛擬目錄
使用visual studio自動創(chuàng)建IIS虛擬目錄,需要的朋友可以參考一下2013-02-02ASP.NET MVC結(jié)合JavaScript登錄、校驗和加密
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC結(jié)合JavaScript登錄、校驗和加密的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08asp.net下Oracle,SQL Server,Access萬能數(shù)據(jù)庫通用類
Oracle,SQL Server,Access萬能數(shù)據(jù)庫通用類!,使用asp.net開發(fā)多數(shù)據(jù)庫系統(tǒng)的朋友可以參考下。2010-10-10ASP.NET CORE學(xué)習(xí)教程之自定義異常處理詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET CORE學(xué)習(xí)教程之自定義異常處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01SqlDataReader指定轉(zhuǎn)換無效的解決方法
這篇文章主要為大家詳細(xì)介紹了SqlDataReader指定轉(zhuǎn)換無效的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06asp.net Gridview數(shù)據(jù)列中實(shí)現(xiàn)鼠標(biāo)懸浮變色
Gridview一般朋友們都比較常用,因為它可以方便快捷的實(shí)現(xiàn)我們所需的很多功能,代碼也比較簡潔。平時的項目中這個控件我也比較常用,其中有個功能用到的頻率也比較多。所以記錄下備忘。2010-06-06使用ASP.NET MVC 4 Async Action+jQuery實(shí)現(xiàn)消息通知機(jī)制的實(shí)現(xiàn)代碼
這兩天在使用Asp.net MVC 4開發(fā)COMET消息通知機(jī)制,在后端使用異步線程對消息進(jìn)行訂閱,客戶端通過AJAX長連接請求MVC中的ACTION2013-02-02