Using GDI in ASP.net

In .net framework, drawing functions are provided from System.Drawing namespace. GDI, Graphic Device Interface) is the under lying technology. It is also known as GDI+, the name of the wrapper assembly of GDI.

GDI+ is imported as default when developing desktop application, as it necessary to render windows forms. GDI+ can be used within asp.net as well to provide drawing functions on the fly. Implementation examples could be loading and displaying images from a database or creating a captcha component.

Using GDI+ is not a complicated task. You will need to obtain a graphic object of an image or drawing surface. Then you can use the method associated with the graphic object such as Drawline, DrawArc, FillEllipse, FillPath, etc., to draw what you want.

The graphic object is an in-memory object. There would be a moment you wonder how to send the image to users. The solution will not be saving the image to a file, because our purpose is to show the image to users, not to store it on our server hard disk. You can use either an asp.net handle or an asp.net traditional web form to complete the task. You will need to set its content-type to the correct content-type of the image, e.g. “image/gif”. If you use a web form, a call to the method of Response.Clear() will be required to clean unwanted characters related to a normal html web page. In the loading even of the form or handle, you can call the object’s method of Save(Response.OutputStream) and then call Response.Flush() to send the image out. On the web page that needs to show the image, the image control (or img tag) will have its URL pointed to the above web form or handle, with extension of .aspx or .ashx accordingly.

 

Tags:

RSS