ASP.NET: Constructing a Graphic on the Fly in ASP.NET


Previous Topic Previous Next Topic Next
Xoc Software
Training
RVBA Conventions
Maya Calendar Program
Company Information
Tools
ASP.NET and Other Tips
.NET: Debugging Designer Features of a Custom Control in Visual Studio
.NET: Setting the Default Font in a Windows Mobile/Compact Framework Custom Control
.NET Fixing C# XML Comments so they Work in Windows XP SP2
.NET: Getting and Setting the Application Version Number
.NET: Getting the Path of the Executing Assembly
.NET: Retrieving Assembly Attributes
.NET: Setting the RootFolder to Other Values in the FolderBrowserDialog in .NET
.NET: Sizing Columns in a ListView Control in .NET
.NET: Using Remoting in .NET
ASP.NET: Constructing a Graphic on the Fly in ASP.NET
ASP.NET: Controlling Caching in ASP.NET Web Forms
ASP.NET: How to use the FrontPage Server Extensions with ASP.NET
ASP.NET: Seeing What is in the ViewState in ASP.NET Web Forms
ASP.NET: Using Forms Authentication in ASP.NET
ASP.NET: View Trace Information on your ASP.NET Web Pages
ASP: Create XML from an ADO query
ASP: Detect Incomplete Loads
ASP: Including an ASP.NET Web Page In a Classic ASP Web Page
ASP: Process .HTM Files with Scripts and Server Side Includes
ASP: QuickSort Algorithm
ASP: Retrieve all server variables from IIS
ASP: Send Email from Active Server Page
HTML: How to Create a Non-Scrolling Region in HTML
IE: Allowing Only Certain ActiveX Controls to Run in Internet Explorer
IIS: Creating a web site for testing in IIS Server
IIS: Creating Multiple Web Sites within IIS on Windows 2000 and Windows XP Professional
IIS: IIS/Visual InterDev Problems and Fixes
IIS: Redirect a domain such as xoc.net to www.xoc.net
SQL Server: Execute SQL Server Updategram
Web Design: Design for People with Disabilities
Web Design: Keep a Web Page out of the Google Cache
Windows: Get HTTP Header of a Web Page using Telnet
Windows: Testing Domain Names without DNS
Windows: Using Hosts File to Access Web Sites with XP SP2
Windows: Windows XP Command Line Tools
Windows Mobile: Reprogramming the Push-to-Talk Button on the AT&T Tilt
Articles
Miscellaneous
Downloads
Links
Search
Email

Other Xoc managed sites:
http://grr.xoc.net
http://www.986faq.com
http://www.mayainfo.org
https://mayacalendar.xoc.net
http://www.yachtslog.com

I needed to construct a PNG file on the fly, so that its content was constructed at the moment it was downloaded. This technique will also work with GIF and JPEG files. This proved easier than I expected in .NET. The graphic is constructed entirely virtually, and is never saved to disk. In fact, there is no file with the graphic name at all.

Step 1, You need to tell IIS that the expected file type needs to be processed by the .NET framework. This has to occur in the IIS Manager dialogs. You may need to get your ISP to perform this task for you.

  1. Right click on the web site in IIS manager and select Properties.
  2. Click on the Home Directory tab.
  3. Click on Configuration button. In the Application Configuration dialog, you are going to set the mapping for .PNG files (or GIF or JPEG) to run through the .NET processing. The easiest way is to...
  4. Select the line in the dialog for an existing extension that is mapped to aspnet_isapi.dll, such as .config and click Edit.
  5. Select and Copy (using Ctrl+C) the path in the Executable textbox from this entry and select Cancel.
  6. Press the Add button.
  7. Paste (using Ctrl+V) the path that you copied.
  8. In the extension textbox add your extension, such as .png.
  9. In the Verbs section, select the Limit To option and put GET, HEAD into the textbox.
  10. Press OK and close all the dialogs

Step 2, Next you have to tell IIS that it should call a piece of code when a file is requested. In the web.config file for the web site, add the following section as a child of <system.web>:

<httpHandlers> <add verb="GET" path="mygraphic.png" type="MyNameSpace.MyImage, MyAssemblyName" /> </httpHandlers>

This tells .NET that when mygraphic.png is requested that it should instantiate the MyNameSpace.MyImage class.

Step 3, Create the MyImage class. The MyImage class is responsible for drawing the graphic. Add the following class to the web site:

using System; 
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Web; 

namespace MyNameSpace
{ 
    public class MyImage: IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            Bitmap bmp;
            Graphics gph; 
            Brush brushFill;
            System.IO.MemoryStream ms; 

            context.Response.BufferOutput = true; 
            context.Response.Clear();
            context.Response.ClearHeaders(); 
            context.Response.ClearContent();
            context.Response.ContentType = "image/png";
            context.Response.Expires = 0; 
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
            context.Response.Cache.SetNoServerCaching(); 
            context.Response.Cache.SetNoStore(); 
            context.Response.Cache.SetMaxAge(System.TimeSpan.Zero); 

            bmp = new Bitmap(100, 100, PixelFormat.Format24bppRgb);
            gph = Graphics.FromImage(bmp);
            brushFill = new SolidBrush(Color.Blue);
            gph.FillRectangle(brushFill, 1, 1, 50, 50); 
            ms = new System.IO.MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            Byte[] abyte = ms.ToArray(); 
            context.Response.OutputStream.Write(abyte, 0, abyte.Length); 
            context.Response.End();
            bmp.Dispose();
            gph.Dispose();
        } 

        public Boolean IsReusable
        { 
            //To enable pooling, return true here.
            //This keeps the handler in memory.
            get
            {
                return false;
            }
        }
    }
}

Step 4, Create a web page that uses the graphic. Put the following line in the web page:

<img src="mygraphic.png" />

When the page is rendered, you will have a 100 x 100 pixel black square with a 50 x 50 pixel blue square in the upper left hand corner. You will need to study the GDI+ features of the .NET framework to modify the drawing. But that's beyond the scope of what I wanted to cover here.


Top