ASP.NET
  Home arrow ASP.NET arrow Page 3 - Playing with Images in ASP.NET 3.5 AJAX Ap...
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

Playing with 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-08-26

    Table of Contents:
  • Playing with Images in ASP.NET 3.5 AJAX Applications
  • Hacking the HTML
  • A Few words about the HTTP Handler
  • Loading Images from an Assembly
  • Accessing Images from a SQL Server Database

  • 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


    Playing with Images in ASP.NET 3.5 AJAX Applications - A Few words about the HTTP Handler


    (Page 3 of 5 )

    Since the HTTP Handler is almost necessary in all the following sample applications, let's say a few words about it.

    HTTP handlers and modules are basic components of Microsoft's ASP.NET architecture. Any request for ASP.NET-managed resources will be resolved using an HTTP handler, and then transmitted through an HTTP module. For example, ASP.NET will map, per incoming HTTP request, to a special HTTP handler.

    ASP.NET has built into it several ready-made HTTP handlers. There is one special HTTP handler to serve the ASP.NET pages, one to serve the .NET Web services and one to resolve the .NET remote requests for the IIS managed remote objects. There are also many other auxiliary handlers, such as trace.axd to trace the page life cycle, webresource.axd to inject assembly resources and scripts into the images, etc.

    In other conditions, you may ask ASP.NET to process some request in the non-standard means when you can write your custom HTTP handlers. In fact, you can rely on the custom HTTP handlers to do any jobs. For example, via an elaborate custom HTTP handler, you can let the users make nearly any invocation through the web, such as to write a click counter, to deal with any image related tasks, etc.

    Now, we've talked enough about theory. Let us roll up our sleeves to construct some interesting sample applications.

    Using a .aspx Page to Render Images

    From the point of view of a browser, a web page is merely a series of characters waiting in line to be processed. When the browser finds an <img> mark it comes to realize that it needs to open another related download channel and sends out a request for the specified URL. To the browser, it does not matter much what kinds of URL and protocol they are; what really concerns it is that the expected output will match a given format- the MIME type.

    Let's examine a trivial example to demonstrate how to specify the proper MIME type using the Content-Type header to render an image onto the page indirectly.

    First, let us look at the main page ( AspxMode.aspx ) to undertake the task of rendering the image. The following gives the key piece of code:

    <div>

    <asp:Image ID="Image1" runat="server" ImageUrl="pic.aspx" />

    </div>

    Here, when the browser finds the <img> mark (which is translated from the ASP.NET server control <Image>), it will open another download channel as well as send out the related request. Then, it expects the MIME type to be matched. If so, an image will be output; otherwise, some exception will possibly be thrown.

    Now, let's see what hides within the other page pic.aspx. The HTML elements' definitions are listed as below:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="pic.aspx.cs" Inherits="pic" %>


    <!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>Untitled Page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    </div>

    </form>

    </body>

    </html>

    Anything peculiar? Nothing, but Visual Studio 2008 automatically generated stuff! In fact, what really plays the crucial role lies in the code-behind file-pic.aspx.cs:

    public partial class pic : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    string s = Server.MapPath(@"~/imgs/lord.bmp");

    Response.ContentType = "image/bmp";

    Response.WriteFile(s);

    }

    }

    Here, we first get the path to the image file lord.bmp, and after we set the ContentType attribute to "image/bmp," we simply write the image data bits to the response (output) stream. Note, in later samples, we will do something with the image bytes before writing them to the stream. Figure 1 shows the running-time snapshot of the AspxMode.aspx page.


    Figure 1-the running-time snapshot for AspxMode.aspx

    In fact, if you try to start up the pic.aspx page independently, you will still catch sight of the same picture. In the latter case, the web server produces the correct image bytes, sets the proper content type, and finally writes all data into the output stream. While in the former case, the output stream in relation to the  pic.aspx page is further redirected to the src attribute of the <img> element in the first AspxMode.aspx page, and as a result, the AspxMode.aspx page takes the responsibilities of rendering the image onto the screen.

    Author's Note: an aspx page is in fact a complex HTTP handler. Although this sample has achieved the aim of rendering an image via another aspx page acting as an agency, this is not the recommended way to output an image dynamically. The mostly recommended way to accomplish such tasks is to define a custom HTTP handler. In later samples, we will delve into it.

    Next, let us see another way to load images.

    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 4 hosted by Hostway
    Stay green...Green IT