Get started with Asp.net -- How to Use Asp.net to dynamically output images

Source: Internet
Author: User
1. Introduction

Most of the time for Asp.net development is to dynamically output HTML files and connect static image files through . Most of the programming work is to add code to the aspx file. What we actually do is to improve the derived class of the page class. Because all the classes corresponding to aspx are inherited from the page. The page class is responsible for managing the entire page lifecycle. However, sometimes outputs do not require complex HTML pages, but simple images. For example, a verification code is a very typical example.

2. Request Execution Process and general processing procedures

Httpapplication finds the ihttphandler Class Based on the ing relationship of the processing program in Web. cofig Based on the URL. after instantiation, it calls the processrequest () method of the class to complete the rendering output. Most of the work of ASP. netweb programs is to output HTML, So Microsoft has designed a page class that implements the ihttphandler interface to process this task. For other outputs, slices, compressed files, plain text, and XLS files, Microsoft does not provide a separate processing program, instead, it is done by providing a type of ashx called a general processing program. What Microsoft has done is to handle the ashx file ing, we only need to complete the ihttphandler interface function.

 

3. process the output image

In vs2010, right-click a website project and choose add new project --> General handler. A. ashx file is added with the following content:

<% @ Webhandler Language = "C #" class = "handler" %> </P> <p> using system; <br/> using system. web; </P> <p> public class handler: ihttphandler {<br/> private httpresponse response; <br/> Public void processrequest (httpcontext context) <br/>{< br/> context. response. contenttype = "text/plain"; <br/> context. response. write ("Hello World "); <br/>}</P> <p> Public bool isreusable <br/>{< br/> Get <br/>{< br/> return false; <br/>}</P> <p>}

As you can see, the Code defines a handler class and implements the ihttphandler interface. What we need to do is to improve the processrequest interface function. The following code outputs a simple rectangular image.

<% @ Webhandler Language = "C #" class = "handler" %> </P> <p> using system; <br/> using system. web; </P> <p> public class handler: ihttphandler {<br/> private httpresponse response; <br/> Public void processrequest (httpcontext context) <br/>{< br/> context. response. contenttype = "image/JPEG"; <br/> system. drawing. bitmap bitmap = new system. drawing. bitmap (100,100); <br/> system. drawing. graphics G = system. drawing. graphics. fromimage (Bitmap); <br/> // draw <br/> G. clear (system. drawing. color. black); <br/> G. drawrectangle (system. drawing. pens. red, 0, 0, 50, 50); </P> <p> G. dispose (); <br/> bitmap. save (context. response. outputstream, system. drawing. imaging. imageformat. JPEG); <br/> bitmap. dispose (); <br/>}</P> <p> Public bool isreusable <br/>{< br/> Get <br/>{< br/> return false; <br/>}</P> <p>}

To display dynamically generated images in other HTML documents, add the following code to the HTML document:

4. Output other types

With ashx (Active Server handler extend), we can output the document types in any HTTP protocol specification. Of course, HTML is also included. If you do not want to use complicated aspx, you can use ashx to complete simple HTML output tasks on your own. However, the convenience of page class lifecycle management is lost. You can decide whether to use the page class (aspx) based on the specific requirements ).

 

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.