Chitika

Wednesday, October 3, 2012

convert image to byte array in c#


The code below is converting the image to byte array. It is pretty straightforward.
public void NewImage_Compute(string imagePath)
{
  byte[] _tempByte;
  byte[] result;
  // Set result to the desired field value
  string path = @” imagePath ";
  System.IO.FileInfo _fi1eInfo = new System.IO.FileInfo(path);

  long _NumBytes  = _fi1eInfo.Length;
  System.IO.FileStream _FStream = newFileStream(path, FileMode.Open, FileAccess.Read);
  BinaryReader _BinaryReader = newBinaryReader(_FStream);
  _tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes));

  result = _tempByte;
}

Converting image to bye array using file stream is bit slower,if you are converting more images to byte arrays, then using file stream is not good.
In that case use memory stream instead of file stream it will give good performance to your application.
Happy coding,,, :-)

No comments:

Post a Comment