Home > DeveloperSection > Forums > Need function for convert image to byte and byte to image.
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    June-15-2013 6:18 AM

 C# C# 
Ratings:


 2 Reply(s)
 884  View(s)
Rate this:
Hi Expert,

How to convert image to byte and byte to image in C#?

Your help is a great appreciated.


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    June-15-2013 6:22 AM

Convert image to byte:

public static byte[] _ImageToByte(Image img)
{
         ImageConverter converter = new ImageConverter();
         return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

Convert Image to byte:

public static Bitmap _ByteToImage(byte[] blob)
        {
            MemoryStream mStream = new MemoryStream();
            byte[] pData = blob;
            mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
            Bitmap bm = new Bitmap(mStream, false);
            mStream.Dispose();
            return bm;
        }

Manmohan Jha

Total Post:35

Points:245
Posted on    July-24-2013 2:59 AM

        private byte[] ReadImg2Byte(Image fileName)
        {
            try
            {
                using (Bitmap image = new Bitmap(fileName))
                {
                    stream = new MemoryStream();
                    image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            return stream.ToArray();
        }


      private Image view_img()
      {
      MemoryStream stream = new MemoryStream((byte[])byteimg);
      Bitmap image = new Bitmap(stream);
      return image;
       }

Mj

Don't want to miss updates? Please click the below button!

Follow MindStick