Getting Image Size
<p>I have found a good way to Find Image Size in C# , but the code was build for .net 3.5 , I want to use for .net 2.0 .</p>

<p>is there any way like here or any .net code of this ?</p>

<pre><code>public static class ImageHelper
 {
     const string errorMessage = "Could not recognise image format.";
     private static Dictionary&lt;byte[], Func&lt;BinaryReader, Size&gt;&gt; imageFormatDecoders = new Dictionary&lt;byte[], Func&lt;BinaryReader, Size&gt;&gt;()
 {
     { new byte[]{ 0x42, 0x4D }, DecodeBitmap},
     { new byte[]{ 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
     { new byte[]{ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif },
     { new byte[]{ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng },
     { new byte[]{ 0xff, 0xd8 }, DecodeJfif },
 };
     /// &lt;summary&gt;
     /// Gets the dimensions of an image.
     /// &lt;/summary&gt;
     /// &lt;param name="path"&gt;The path of the image to get the dimensions of.&lt;/param&gt;
     /// &lt;returns&gt;The dimensions of the specified image.&lt;/returns&gt;
     /// &lt;exception cref="ArgumentException"&gt;The image was of an unrecognised format.&lt;/exception&gt;
     public static Size GetDimensions(string path)
     {
         using (BinaryReader binaryReader = new BinaryReader(File.OpenRead(path)))
         {
             try
             {
                 return GetDimensions(binaryReader);
             }
             catch (ArgumentException e)
             {
                 if (e.Message.StartsWith(errorMessage))
                 {
                     throw new ArgumentException(errorMessage, "path", e);
                 }
                 else
                 {
                     throw e;
                 }
             }
         }
     }
     /// &lt;summary&gt;
     /// Gets the dimensions of an image.
     /// &lt;/summary&gt;
     /// &lt;param name="path"&gt;The path of the image to get the dimensions of.&lt;/param&gt;
     /// &lt;returns&gt;The dimensions of the specified image.&lt;/returns&gt;
     /// &lt;exception cref="ArgumentException"&gt;The image was of an unrecognised format.&lt;/exception&gt;
     public static Size GetDimensions(BinaryReader binaryReader)
     {
         int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x =&gt; x.Length).First().Length;
         byte[] magicBytes = new byte[maxMagicBytesLength];
         for (int i = 0; i &lt; maxMagicBytesLength; i += 1)
         {
             magicBytes[i] = binaryReader.ReadByte();
             foreach (var kvPair in imageFormatDecoders)
             {
                 if (magicBytes.StartsWith(kvPair.Key))
                 {
                     return kvPair.Value(binaryReader);
                 }
             }
         }
         throw new ArgumentException(errorMessage, "binaryReader");
     }
     private static bool StartsWith(this byte[] thisBytes, byte[] thatBytes)
     {
         for (int i = 0; i &lt; thatBytes.Length; i += 1)
         {
             if (thisBytes[i] != thatBytes[i])
             {
                 return false;
             }
         }
         return true;
     }
     private static short ReadLittleEndianInt16(this BinaryReader binaryReader)
     {
         byte[] bytes = new byte[sizeof(short)];
         for (int i = 0; i &lt; sizeof(short); i += 1)
         {
             bytes[sizeof(short) - 1 - i] = binaryReader.ReadByte();
         }
         return BitConverter.ToInt16(bytes, 0);
     }
     private static int ReadLittleEndianInt32(this BinaryReader binaryReader)
     {
         byte[] bytes = new byte[sizeof(int)];
         for (int i = 0; i &lt; sizeof(int); i += 1)
         {
             bytes[sizeof(int) - 1 - i] = binaryReader.ReadByte();
         }
         return BitConverter.ToInt32(bytes, 0);
     }
     private static Size DecodeBitmap(BinaryReader binaryReader)
     {
         binaryReader.ReadBytes(16);
         int width = binaryReader.ReadInt32();
         int height = binaryReader.ReadInt32();
         return new Size(width, height);
     }
     private static Size DecodeGif(BinaryReader binaryReader)
     {
         int width = binaryReader.ReadInt16();
         int height = binaryReader.ReadInt16();
         return new Size(width, height);
     }
     private static Size DecodePng(BinaryReader binaryReader)
     {
         binaryReader.ReadBytes(8);
         int width = binaryReader.ReadLittleEndianInt32();
         int height = binaryReader.ReadLittleEndianInt32();
         return new Size(width, height);
     }
     private static Size DecodeJfif(BinaryReader binaryReader)
     {
         while (binaryReader.ReadByte() == 0xff)
         {
             byte marker = binaryReader.ReadByte();
             short chunkLength = binaryReader.ReadLittleEndianInt16();
             if (marker == 0xc0)
             {
                 binaryReader.ReadByte();
                 int height = binaryReader.ReadLittleEndianInt16();
                 int width = binaryReader.ReadLittleEndianInt16();
                 return new Size(width, height);
             }
             binaryReader.ReadBytes(chunkLength - 2);
         }
         throw new ArgumentException(errorMessage);
     }
 }
</code></pre>
