![]() |
ASP.NET: Constructing a Graphic on the Fly in ASP.NET |
Search [ckdfibhg] |
|
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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.
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>:
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 |
[www.xoc.net] Copyright © 1997-2023 by Gregory Reddick ![]() |