>> Home
ASP.NET (13)
Security (5)
"Classic" ASP (2)
C# (1)
VB.NET (1)
ASP and Flash (1)
About Us
Chinese Content German Content


Obtaining Image Information on your own

Written by: Christoph Wille
Translated by: Bernhard Spuida
First published: 11/30/2000

A 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!


And yet again .NET saves the day by supplying enormously much functionality for free - we just have to look for it. And the best part is that today's component can be used in all .NET Applications not just ASP.NET Pages.

Downloading the Code

Click here, to start the download.

All rights reserved. The content of these pages is copyrighted.
All content and images on this site are copyright. Reuse (even parts) needs our written consent.