c# - Getting Image Size -


i have found way find image size in c# , code build .net 3.5 , want use .net 2.0 .

is there way here or .net code of ?

public static class imagehelper  {      const string errormessage = "could not recognise image format.";      private static dictionary<byte[], func<binaryreader, size>> imageformatdecoders = new dictionary<byte[], func<binaryreader, size>>()  {      { 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 },  };      /// <summary>      /// gets dimensions of image.      /// </summary>      /// <param name="path">the path of image dimensions of.</param>      /// <returns>the dimensions of specified image.</returns>      /// <exception cref="argumentexception">the image of unrecognised format.</exception>      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;                  }              }          }      }      /// <summary>      /// gets dimensions of image.      /// </summary>      /// <param name="path">the path of image dimensions of.</param>      /// <returns>the dimensions of specified image.</returns>      /// <exception cref="argumentexception">the image of unrecognised format.</exception>      public static size getdimensions(binaryreader binaryreader)      {          int maxmagicbyteslength = imageformatdecoders.keys.orderbydescending(x => x.length).first().length;          byte[] magicbytes = new byte[maxmagicbyteslength];          (int = 0; < maxmagicbyteslength; += 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)      {          (int = 0; < thatbytes.length; += 1)          {              if (thisbytes[i] != thatbytes[i])              {                  return false;              }          }          return true;      }      private static short readlittleendianint16(this binaryreader binaryreader)      {          byte[] bytes = new byte[sizeof(short)];          (int = 0; < sizeof(short); += 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)];          (int = 0; < sizeof(int); += 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);      }  } 

that code not needed in c#/.net. use image class:

image newimage = image.fromfile("sampimag.jpg"); newimage.size(); 

managed gdi+ has built-in encoders , decoders support following file types:

bmp gif jpeg png tiff 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -