Obtaining Image Information on your own

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

 frequent problem in uploading image files is that the size (width, height) isn’t known and that consequently you have to determine them on your own. Under ASP the free component ImageSize by ServerObjects can be of help, it however is limited to a few (important) formats.

With help from the .NET Framework, everybody can program his own ImageInfo component without this project turning into an inordinate effort – you just have to use the Bitmap class to your advantage. And presto – we have size information for many more image file formats!

Creating the ImageInfo Component

I have choosen to encapsulate the functionality of obtaining image information in its own class. This class has the following methods and properties:

  • void Load(string): Loads the image file specified.
  • [get] int Width: Reads the image width.
  • [get] int Height: Reads the image height.
  • [get] string Format: Reads the image format (“JPEG”, etc).

The Properties obviously are read-only properties implemented with the help of accessors. For reasons of lazyness, the class does not catch any exceptions, but passes them through to the caller (file does not exist, invalid format, …).

The complete source code looks as follows (ImageInfo.cs):

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace AspHeute
 public class ImageInfo
   Bitmap m_bmpRepresentation;

   public void Load(string strImageFile)
    m_bmpRepresentation = new Bitmap(strImageFile, false);

   public int Height
     get { return m_bmpRepresentation.Height; }

   public int Width
     get { return m_bmpRepresentation.Height; }

   public string Format
      ImageFormat bmpFormat = m_bmpRepresentation.RawFormat;
      string strFormat = "unidentified format";

	  if (bmpFormat.Equals(ImageFormat.Bmp)) strFormat = "BMP";
	  else if (bmpFormat.Equals(ImageFormat.Emf)) strFormat = "EMF";
	  else if (bmpFormat.Equals(ImageFormat.Exif)) strFormat = "EXIF";
	  else if (bmpFormat.Equals(ImageFormat.Gif)) strFormat = "GIF";
	  else if (bmpFormat.Equals(ImageFormat.Icon)) strFormat = "Icon";
	  else if (bmpFormat.Equals(ImageFormat.Jpeg)) strFormat = "JPEG";
	  else if (bmpFormat.Equals(ImageFormat.MemoryBmp)) strFormat = "MemoryBMP";
	  else if (bmpFormat.Equals(ImageFormat.Png)) strFormat = "PNG";
	  else if (bmpFormat.Equals(ImageFormat.Tiff)) strFormat = "TIFF";
	  else if (bmpFormat.Equals(ImageFormat.Wmf)) strFormat = "WMF";

      return strFormat;

The majority of the code is to be found in the accessor for the image format (Format), as the image format is defined as GUID and I therefore have to perform all comparisons with Equals. The user however receives a simple-to-use String, e.g. for a Select Case in VB.NET.

Loading the bitmap is done using the constructor of the Bitmap class in the Load method. From this time on, the complete bitmap is in memory. As memory is managed in .NET by means of the garbage collector, the Bitmap class and the ImageInfo object should be removed explicitly (=on your own) from memory using a Dispose method I did not build in (left as an exercise for the reader):

public void Dispose()

This method should be called after the last use of the ImageInfo class which will significantly improve performance of the application. In general the rule under .NET is that resources should be freed explicitly as soon as possible! Waiting for the garbage collector can turn into a (performance) trap.

The component is compiled using the compile.bat batch file contained in the download. After compilation, the component has to be copied to the bin directory of the Web site and can then be used in all ASP.NET pages.

Using the Component

As the component does not exactly offer “a lot” of functionality, use is conceivably simple. The only important point is handling errors – as I left exception handling out of the component this has to be done in the ASP.NET file (demo.aspx):

<% @Page Language="C#" %>
<% @Import Namespace="AspHeute" %>
string strImageFile = Page.MapPath("myImage.jpg");
bool bLoadedOK = true;
ImageInfo imgInfo = new ImageInfo();

catch (Exception e)
  bLoadedOK = false;

if (bLoadedOK)
  Response.Write("Width: " + imgInfo.Width + "<br>");
  Response.Write("Height: " + imgInfo.Height + "<br>");
  Response.Write("Image format: " + imgInfo.Format + "<br>");

As for this source code, everything should be quite clear. You are cordially invited to try various image files and formats!