ASP.NET
  Home arrow ASP.NET arrow Page 3 - Handling Dynamic Images in ASP.NET 3.5 AJA...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Handling Dynamic Images in ASP.NET 3.5 AJAX Applications
By: Xianzhong Zhu
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-09-03

    Table of Contents:
  • Handling Dynamic Images in ASP.NET 3.5 AJAX Applications
  • Constructing an ASP.NET AJAX Styled Message Board Sample
  • About the Data Storage
  • Creating the Sample .aspx Page
  • Property Related Value

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Handling Dynamic Images in ASP.NET 3.5 AJAX Applications - About the Data Storage


    (Page 3 of 5 )

    In this sample, the GuestBook.xml file under the App_Data folder is used to store the message information submitted by users. Moreover, the other two classes, GuestBook and Comment, are auxiliary classes to maintain this kind of data. The following defines the schema of this .xml file and already entered data:

    <?xml version="1.0"?>

    <GuestBook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://demo.126.com/">

    <comment name="jime" text="hi,good work." />

    <comment name="Mike" text="Thank you. This helps me a lot!" />

    <comment name="John" text="I like your ASP.NET AJAX article. Would you please provide the related source code?" />

    </GuestBook>

    Next, the SQL Server 2005 database myNorthWind.mdf with only a table namedcodetableis defined to persist the verifying codes to help generate the verifying picture. In this sample, we put a string, 'GenerateCAPTCHATest' to the field Code in the codetable table to be used to generate the verification code (five characters are selected at random here).

    Next, let us delve into the most interesting point in this sample.

    Using GDI+ to Create the Verifying Picture in the Custom HTTP Handler

    In this sample, we mainly employ two techniques to hold up the possible robot action. One is to use the above-introduced ASP.NET AJAX Toolkit control, NoBot, which is designed to achieve the result from the angle of time. In other words, only the data entered within specified time is allowed. The other solution is to take the traditional action, which is to draw some characters commingled with some pictures so that only users entering the specified correct characters can continue to take the next step.

    For convenience, let's again look at the pictures which  contain the letters users are required to enter, as shown Figure 5 below.


    Figure 5-only users that are able to enter the letters within the picture below is allowed to leave work


    In this sample, to accomplish the above second blocking solution we have recourse to a custom HTTP handler, GenerateCAPTCHA.ashx. As you have guessed, all the secrets behind the scene are finished within the important function named ProcessRequest, defined in this HTTP handler. The following lists the complete source code of this function.

    // render the verification code

    public void ProcessRequest (HttpContext context)

    {

    // Create the Bitmap

    using (Bitmap objBitmap = new Bitmap(_width, _height, PixelFormat.Format32bppArgb))

    {

    // create the canvas

    using (Graphics objGraphics = Graphics.FromImage(objBitmap))

    {

    //specify the smoothing mode

    objGraphics.SmoothingMode = SmoothingMode.AntiAlias;


    //Define a rectangle to render the verification code

    Rectangle rect = new Rectangle(0, 0, _width, _height);

     

    //define a brush with specified hatching style

    HatchBrush hBr = new HatchBrush(HatchStyle.WideDownwardDiagonal, Color.Yellow, Color.White);

    // create a rectangle for rendering picture

    objGraphics.FillRectangle(hBr, rect);

    hBr.Dispose();

     

    // define the character offset in the string

    int charOffset = 0;

    //compute the width for each character

    double charWidth = Convert.ToDouble(_width) / Convert.ToDouble(_randomTextLength);

    //look on per character as a rectangle so as to twist it easily

    Rectangle rectChar;

    // define the fond style

    Font fnt = null;

     

    //first use a black color brush to render the letter within the rectangle,

    //then twist the rectangle, and at last use the rectangle to draw

    using (Brush br = new SolidBrush(Color.Black))

    {

    foreach (Char ch in _randomText)

    {

    fnt = GetFontStyle();

    rectChar = new Rectangle(Convert.ToInt32(charOffset * charWidth), 0, Convert.ToInt32(charWidth), _height);


    GraphicsPath gp = TextPath(ch.ToString(), fnt, rectChar);

    TwistText(gp, rectChar);

    objGraphics.FillPath(br, gp);

    charOffset += 1;

    }

    }


    // add some background noise points as well as the line noise

    AddNoise(objGraphics, rect);

    AddLine(objGraphics, rect);


    // specify the ContentType of the Response object

    //, and require when the drawing is finished return the result to the browser side

    context.Response.ContentType = "Image/Jpeg";

    context.Response.Clear();

    // set the Cache-Control: no-cache

    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

    context.Response.BufferOutput = true;

    context.Response.Flush();

    objBitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);

    }

     

    HttpApplication app = context.ApplicationInstance;


    //you can use the following two sentence to create a unique GUID to access the newly-generated verifying code

    //here we simply hard code it

    //Guid guid = System.Guid.NewGuid();

    // string strGuid = guid.ToString();

    string strGuid = "CAPTCHA";


    if (strGuid != String.Empty)

    {

    HttpRuntime.Cache.Insert(strGuid, _randomText);

    }

    }

    }

    I've put enough comments in the code that additional explanation should not be necessary. As for the other helper functions used in it, you can refer to the source code, which can be downloaded at the end of this article.

    More ASP.NET Articles
    More By Xianzhong Zhu


     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT