Pages

2013-04-08

DotNet (ASP.Net): Writing Copyright Notes on Images via HTTPHandlers


The .NET Framework graphic engine supports quite a few image formats, including JPEG, GIF, BMP, and  PNG. The whole collection of image formats is in the ImageFormat structure of the System.Drawing namespace. You can save a memory-resident Bitmap object to any of the supported formats by using one of the overloads of the Save method:


Bitmap bmp = new Bitmap(file);
...
bmp.Save(outputStream, ImageFormat.Gif);
When you attempt to save an image to a stream or disk file, the system attempts to locate an encoder for the requested format. The encoder is a module that converts from the native format to the specified format. Note that the encoder is a piece of unmanaged code that lives in the underlying Win32 platform. For each save format, the Save method looks up the right encoder and proceeds.

This example shows how to load an existing image, add some copyright notes, and serve the modified version to the user. In doing so, we’ll load an image into a Bitmap object, obtain a Graphics for that bitmap, and use graphics primitives to write. When finished, we’ll save the result to the page’s output stream and indicate a particular MIME type.



The sample page that triggers the example is easily created, as shown in the following listing:
<html><body><img id="picture" src="dynimage.axd?url=images/pic1.jpg" /></body></html>
The page contains no ASP.NET code and displays an image through a static HTML <img> Tag. The source of the image, though, is an HTTP handler that loads the image passed through the query string and then manipulates and displays it. Here’s the source code for the ProcessRequest method of the HTTP handler:


public void ProcessRequest (HttpContext context){var o = context.Request["url"];if (o == null){context.Response.Write("No image found.");context.Response.End();return;}var file = context.Server.MapPath(o);var msg = ConfigurationManager.AppSettings["CopyrightNote"];if (File.Exists(file)){Bitmap bmp = AddCopyright(file, msg);context.Response.ContentType = "image/jpeg";bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);bmp.Dispose();}else{context.Response.Write("No image found.");context.Response.End();}}
Note that the server-side page performs two different tasks indeed. First, it writes copyright text on the image canvas; next, it converts whatever the original format was to JPEG:


Bitmap AddCopyright(String file, String msg){// Load the file and create the graphicsvar bmp = new Bitmap(file);var g = Graphics.FromImage(bmp);// Define text alignmentvar strFmt = new StringFormat();strFmt.Alignment = StringAlignment.Center;// Create brushes for the bottom writing// (green text on black background)var btmForeColor = new SolidBrush(Color.PaleGreen);var btmBackColor = new SolidBrush(Color.Black);
// To calculate writing coordinates, obtain the size of // the text given the font typeface and sizevar btmFont = new Font("Verdana", 7);var textSize = g.MeasureString(msg, btmFont);// Calculate the output rectangle and fillfloat 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 resourcesg.DrawString(msg, btmFont, btmForeColor, textArea);btmForeColor.Dispose();btmBackColor.Dispose();btmFont.Dispose();g.Dispose();return bmp;





No comments:

Post a Comment