Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [73]

By Root 5313 0
luckier. ASP.NET applications, in fact, can rely on a powerful and integrated graphic engine integrated in the .NET Framework.

In ASP.NET, writing images to disk might require some security adjustments. Normally, the ASP.NET runtime runs under the aegis of the NETWORK SERVICE user account. In the case of anonymous access with impersonation disabled—which are the default settings in ASP.NET—the worker process lends its own identity and security token to the thread that executes the user request of creating the file. With regard to the default scenario, an access-denied exception might be thrown if NETWORK SERVICE (or the selected application pool identity) lacks writing permissions on virtual directories—a pretty common situation.

ASP.NET provides an interesting alternative to writing files on disk without changing security settings: in-memory generation of images. In other words, the dynamically generated image is saved directly to the output stream in the needed image format or in a memory stream.

Writing Copyright Notes on Images


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.

The next example wraps up all the points we’ve touched on. 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:

The page contains no ASP.NET code and displays an image through a static HTML 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 graphics

var bmp = new Bitmap(file);

var g = Graphics.FromImage(bmp);

// Define text alignment

var 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 size

var btmFont = new Font("Verdana",

Return Main Page Previous Page Next Page

®Online Book Reader