Programming Microsoft ASP.NET 4 - Dino Esposito [74]
var textSize = g.MeasureString(msg, btmFont);
// Calculate the output rectangle and fill
float x = (bmp.Width-textSize.Width-3);
float y = (bmp.Height-textSize.Height-3);
float w = (x + textSize.Width);
float h = (y + textSize.Height);
var textArea = new RectangleF(x, y, w, h);
g.FillRectangle(btmBackColor, textArea);
// Draw the text and free resources
g.DrawString(msg, btmFont, btmForeColor, textArea);
btmForeColor.Dispose();
btmBackColor.Dispose();
btmFont.Dispose();
g.Dispose();
return bmp;
}
Figure 4-6 shows the results.
Figure 4-6. A server-resident image has been modified before being displayed.
Note that the additional text is part of the image the user downloads on her client browser. If the user saves the picture by using the Save Picture As menu from the browser, the text (in this case, the copyright note) is saved along with the image.
Important
All examples demonstrating programmatic manipulation of images take advantage of the classes in the System.Drawing assembly. The use of this assembly is not recommended in ASP.NET and is explicitly not supported in ASP.NET Web services. (See http://msdn.microsoft.com/en-us/library/system.drawing.aspx.) This fact simply means that you are advised not to use classes in System.Drawing because Microsoft can’t guarantee it is always safe to use them in all possible scenarios. If your code is currently using System.Drawing—the GDI+ subsystem—and it works just fine, you’re probably OK. In any case, if you use GDI+ classes and encounter a malfunction, Microsoft will not assist you. Forewarned is forearmed.
You might be better off using an alternative to GDI+, especially for new applications. Which one? For both speed and reliability, you can consider the WPF Imaging API. Here’s an interesting post that shows how to use Windows Presentation Foundation (WPF) for resizing images: http://weblogs.asp.net/bleroy/archive/2010/01/21/server-side-resizing-with-wpf-now-with-jpg.aspx.
Controlling Images via an HTTP Handler
What if the user requests the JPG file directly from the address bar? And what if the image is linked by another Web site or referenced in a blog post? By default, the original image is served without any further modification. Why is this so?
For performance reasons, IIS serves static files, such as JPG images, directly without involving any external module, including the ASP.NET runtime. In this way, the HTTP handler that does the trick of adding a copyright note is therefore blissfully ignored when the request is made via the address bar or a hyperlink. What can you do about it?
In IIS 6, you must register the JPG extension as an ASP.NET extension for a particular application using IIS Manager. In this case, each request for JPG resources is forwarded to your application and resolved through the HTTP handler.
In IIS 7, things are even simpler for developers. All you have to do is add the following lines to the application’s web.config file:
verb="*" path="*.jpg" type="DynImageHandler, AspNetGallery.Extensions" />
You might want to add the same setting also under This is yet another benefit of the unified runtime pipeline we experience when the ASP.NET application runs under IIS 7 integrated mode. Note An HTTP handler that needs to access session-state values must implement the IRequiresSessionState interface. Like INamingContainer, it’s a marker interface and requires no method implementation. Note that the IRequiresSessionState interface indicates that the HTTP handler requires read and write access to the session state. If read-only access is needed, use the IReadOnlySessionState interface instead. Advanced HTTP Handler Programming
HTTP handlers are not a tool for everybody. They serve a very