Click here to Skip to main content
15,867,141 members
Articles / Web Development / ASP.NET
Article

Displaying the .NET Image Codecs with ASP.NET

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
7 Nov 20023 min read 67.9K   146   19   4
Using ASP.NET and reflection to display the ImageCodecInfo objects available on the server machine.

Browser Output

Introduction

This sample demonstrates the GetImageEncoders() and GetImageDecoders() methods in the System.Drawing.Imaging.ImageCodecInfo class, presenting the results in two dynamically generated HTML tables.

I came up with this example while investigating some aspects of the System.Drawing.Imaging namespace recently.  My intent was to show what built-in image codecs are available in .NET, but the result is a nice example of dynamic HTML table generation and of using the System.Reflection namespace.

Background

The term CODEC is an acronym for "compression/decompression" and is used for modules that compress and/or decompress various types of data. An image codec, as you might guess, is a codec applied to a digital image. The .NET Framework provides the ImageCodecInfo class to encapsulate such a module as part of the System.Drawing.Imaging namespace. The Bitmap class includes a Save() method that accepts an ImageCodecInfo instance and saves the current bitmap in the format supported by the given codec.

At present, it is not possible to build and add customer codec modules to .NET.  A number of built-in compression and decompression modules, commonly called encoders and decoders, are provided that cover the most common file formats.  These codecs should work well for most digital imaging applications you may wish to build.

Using the code

The project can be downloaded from the link at the top of this page. You will need Visual Studio .NET or other compiler, of course, and access to an Internet Information Server (IIS) to act as the server machine.

To run the project from Visual Studio .NET, create a new ASP.NET Web Application project called "ImageCodecs." Close the new solution and replace the project's directory (also called "ImageCodecs") on the server with the contents of the downloaded zip file. Reload the project in Visual Studio .NET and you should be ready to go.

Points of Interest

There are two primary methods in the code, one for each type of codec. The method that loads the encoder table is shown here. The method for the decoder table is quite similar.

C#
private void LoadEncoderTable()
{
    // Clear any existing rows
    EncoderTable.Rows.Clear();

    // Build header based on the properties in the ImageCodecInfo class
    Type t = typeof(ImageCodecInfo);
(1) PropertyInfo[] properties = t.GetProperties();

    HtmlTableRow headerRow = new HtmlTableRow();
(2) BuildHeaderRow(headerRow, properties);
    EncoderTable.Rows.Add(headerRow);

    // Load the set of encoders
    ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();

    // Add a row for each encoder to the table
    foreach (ImageCodecInfo info in encoders)
    {
        HtmlTableRow row = new HtmlTableRow();
(3)     BuildTableRow(row, properties, info);
        EncoderTable.Rows.Add(row);
    }
}

As you can see, (1) the Type.GetProperties() method from the System.Reflection namespace is used to retrieve the array of properties for the ImageCodecInfo class type. This array is used both (2) to generate the table header and (3) to generate a table row for each encoder.

The BuildHeaderRow() and BuildTableRow() methods use reflection to enumerate the properties from the ImageCodecInfo class. The BuildTableRow() method is slightly more complex, and is shown here.

C#
private void BuildTableRow(HtmlTableRow row, PropertyInfo[] properties, object obj)
{
    // Add a cell containing the value of each property in the given object
    foreach (PropertyInfo prop in properties)
    {
        // Create a new cell for the value
        HtmlTableCell cell = new HtmlTableCell();

        // Get the property value and display it as a string.
(4)     object propValue = prop.GetValue(obj, null);
        if (propValue != null)
            cell.InnerText = propValue.ToString();
        else
            cell.InnerText = "<none>";

        // Add the new cell to the row
        row.Cells.Add(cell);
    }
}

Note the use (4) of the GetValue() method to retrieve the value of the property corresponding to the current PropertyInfo object. We simply display the ToString() result of this value in the table, unless the value is null, in which case we display the string "<none>" instead.

For More Information

The ImageCodeInfo class in .NET is based on the ImageCodeInfo class in GDI+. The .NET versions are not documented very extensively, but you can learn a lot from the Platform SDK documentation. A good place to start for more information on this topic is the article "Using Image Encoders and Decoders" in the Platform SDK GDI+ documentation.

Microsoft also provides the microsoft.public.dotnet.framework.drawing newsgroup for questions and comments on the .NET drawing namespaces.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Erik Brown is the author of "Windows Forms in Action" and a veteran of three startup companies, all successful. He holds a B.S. and M.S. in Mathematics from Carnegie-Mellon University and currently works as an architect and project manager at Unisys Corporation.

When not in front of his computer, Erik can be found on and around the soccer fields of Northern Virginia, where he lives with his wife and two daughters.

Comments and Discussions

 
GeneralI have a problem regarding image encoders Pin
rav_nan10-May-07 1:42
rav_nan10-May-07 1:42 
QuestionASP.NET?? Pin
leppie9-Nov-02 8:27
leppie9-Nov-02 8:27 
AnswerRe: ASP.NET?? Pin
Erik Brown11-Nov-02 4:13
Erik Brown11-Nov-02 4:13 
GeneralRe: ASP.NET?? Pin
Shockyboy12-Nov-02 2:07
Shockyboy12-Nov-02 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.