Understand HttpHandler and generate a text clip for all *. jpg images on the image.

Source: Internet
Author: User

The IHttpHandler interface is defined as follows:
Copy codeThe Code is as follows:
Interface IHttpHandler
{
Void ProcessRequest (HttpContext ctx );
Bool IsReuseable {get ;}

1. Create a website named MyHttpHandlerTest
2. Right-click Add and select the class library named MyHttpHandler.
3-right-click the class library created in the previous step and add System. Web reference

Main Code:
Copy codeThe Code is as follows:
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 Member
Public bool IsReusable
{
Get {return true ;}
}

Public void ProcessRequest (HttpContext context)
{
Context. Response. Write ("handler processing ");
}
# Endregion
}
}

4-right-click the MyHttpHandler class library and choose "MyHttpHandler" from the shortcut menu.

5-in the web. config system. web node, the following nodes are listed below:
<HttpHandlers>
<Add verb = "*" path = "Handler1.aspx" type = "MyHttpHandler. Class1, MyHttpHandler"/>
<! --
Options in the configuration file:

· Verb can be "GET" or "POST", indicating to process GET or POST requests. "*" Indicates that all requests are processed.

· Path indicates processing the corresponding file. "*. aspx" indicates processing the requests sent to all ASPX pages. You can specify a path, such as "/test/*. aspx", indicating that the ASPX file under the test directory is processed only.

· In the Type attribute, the string before the comma specifies the Class Name of the HttpHandler implementation class, and the subsequent string specifies the name of the Dll file.

The format is as follows: type = "full name of the custom HttpHandler implementation class, namespace of the custom HttpHandler implementation class (that is, Dll name )"

Or type = "full name of the custom HttpHandler implementation class"
-->
</HttpHandlers>
6-Right-click MyHttpHandlerTest to add a reference and select the project to find the. dll file after compilation.

7-run Handler1.aspx. The page displays:

Now we use HttpHandler to generate a text segment in the image.
Add a Class. The default value is Class. cs.
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Web;
Using System. Web. SessionState;
Using System. Drawing;
/// <Summary>
/// Summary of Class1
/// </Summary>
Public class Class1: IHttpHandler
{
Public Class1 ()
{
//
// TODO: add the constructor logic here
//
}
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", 24366f, FontStyle. Regular );
String writetext = HttpUtility. UrlEncode (context. Request. QueryString ["writetext"]);
Gh. DrawString (HttpUtility. UrlDecode (writetext), font, new SolidBrush (Color. LightBlue), 20366f, newimage. Height-font. Height-30 );
Newimage. Save (context. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
Gh. Dispose ();
Newimage. Dispose ();
}
}

Create a new. aspx page, add a HyperLink control, and then add a piece of code to the. cs file to pass the value.
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
HyperLink1.NavigateUrl = "img.jpg? Writetext = "+ HttpUtility. UrlEncode (" snail bait ");
}

In addition, you need to change the httpHandlers node to the following in the web. config file:
<Add verb = "*" path = "*. jpg" type = "Class1"/>
Only files in .jpg format can be processed.
Refer to in-depth analysis of asp.net 2.0 control development by not far

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.