Programming Microsoft ASP.NET 4 - Dino Esposito [71]
type="PictureViewerHandler, AspNetGallery.Extensions" /> You place the assembly in the GAC and move the configuration script to the global web.config to extend the settings to all applications on the machine. If you’re targeting IIS 7 integrated mode, you also need the following: verb="*" path="folder.axd" type="PictureViewerHandler, AspNetGallery.Extensions" /> Serving Images More Effectively In spite of the wide use of images on the Web, there is just one way in which a Web page can reference an image—by using the HTML In many cases, the URL points to a static resource such as a GIF or JPEG file. In this case, the Web server takes the request upon itself and serves it without invoking external components. However, the fact that many Where else can you turn to get images aside from picking them up from the server file system? One way to do it is to load images from a database, or you can generate or modify images on the fly just before serving the bits to the browser. Loading Images from Databases The facts say that all database management systems (DBMS) of a certain reputation and volume have supported binary large objects (BLOB) for quite some time. Sure, a BLOB field doesn’t necessarily contain an image—it can contain a multimedia file or a long text file—but overall there must be a good reason for having this BLOB supported in Microsoft SQL Server, Oracle, and similar popular DBMS systems! To read an image from a BLOB field with ADO.NET, you execute a SELECT statement on the column and use the ExecuteScalar method to catch the result and save it in an array of bytes. Next, you send this array down to the client through a binary write to the response stream. Let’s write an HTTP handler to serve a database-stored image: public class DbImageHandler : IHttpHandler { public void ProcessRequest(HttpContext ctx) { // Ensure the URL contains an ID argument that is a number var id = -1; var p1 = context.Request.Params["id"]; if (p1 != null) id = p1.ToInt32(-1); if (id < 0) { context.Response.End(); return; } var connString = "..."; const String cmdText = "SELECT photo FROM employees WHERE employeeid=@id"; // Get an array of bytes from the BLOB field byte[] img = null; var conn = new SqlConnection(connString); using (conn) { var cmd = new SqlCommand(cmdText, conn); cmd.Parameters.AddWithValue("@id", id); conn.Open(); img = (byte[])cmd.ExecuteScalar(); } // Prepare the response for the browser if (img != null) { ctx.Response.ContentType = "image/jpeg"; ctx.Response.BinaryWrite(img);
Any page you get from the Web these days is topped with so many images and is so well conceived and designed that often the overall page looks more like a magazine advertisement than an HTML page. Looking at the current pages displayed by portals, it’s rather hard to imagine there ever was a time—and it was only a decade ago—when one could create a Web site by using only a text editor and some assistance from a friend who had a bit of familiarity with Adobe PhotoShop. tag. By design, this tag points to a URL. As a result, to be displayable within a Web page, an image must be identifiable through a URL and its bits should be contained in the output stream returned by the Web server for that URL.
tags on the Web are bound to a static file does not mean there’s no other way to include images in Web pages.
The use of a database as the storage medium for images is controversial. Some people have good reasons to push it as a solution; others tell you bluntly they would never do it and that you shouldn’t either. Some people can tell you wonderful stories of how storing images in a properly equipped database was the best experience of their professional life. With no fear that facts could perhaps prove them wrong, other people will confess that they would never use a database again for such a task.