locked
how do i convert byte array to image RRS feed

  • Question

  • how do i convert byte array to image file and display it in picture box
    Wednesday, July 8, 2009 6:42 PM

Answers

  • Hi Seah,


    use this code for your requirement


    public Image byteArrayToImage(byte[] byteArrayIn)
    {

        MemoryStream ms = new MemoryStream(byteArrayIn);

        Image returnImage = Image.FromStream(ms);

        return returnImage;

    }


    Hope this may be help.


    Pavan Pareta | If you find my post is helpful for you then mark as a answer.
    • Proposed as answer by WmDev Wednesday, July 8, 2009 7:07 PM
    • Edited by WmDev Wednesday, July 8, 2009 7:09 PM Code Formating
    • Marked as answer by warrentang Wednesday, July 15, 2009 12:41 AM
    Wednesday, July 8, 2009 7:05 PM
  • Hi,

    The following should help you :-

            private static byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, ImageFormat formatOfImage)
            {
                byte[] ret;
    
                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        imageToConvert.Save(ms, formatOfImage);
                        ret = ms.ToArray();
                    }
                }
                catch (Exception) { throw; }
    
                return ret;
            }
    
            private static Image ConvertByteArrayToImage(byte[] byteArrayToConvert)
            {
                Image ret;
    
                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.Write(byteArrayToConvert, 0, byteArrayToConvert.Length);
                        ret = new Bitmap(ms);
                    }
                }
                catch (Exception) { throw; }
    
                return ret;
            }
    WmDev, Image.FromStream doesn't seem to be supported on the Compact Framework.

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by warrentang Wednesday, July 15, 2009 12:41 AM
    Wednesday, July 8, 2009 7:13 PM

All replies

  • Hi Seah,


    use this code for your requirement


    public Image byteArrayToImage(byte[] byteArrayIn)
    {

        MemoryStream ms = new MemoryStream(byteArrayIn);

        Image returnImage = Image.FromStream(ms);

        return returnImage;

    }


    Hope this may be help.


    Pavan Pareta | If you find my post is helpful for you then mark as a answer.
    • Proposed as answer by WmDev Wednesday, July 8, 2009 7:07 PM
    • Edited by WmDev Wednesday, July 8, 2009 7:09 PM Code Formating
    • Marked as answer by warrentang Wednesday, July 15, 2009 12:41 AM
    Wednesday, July 8, 2009 7:05 PM
  • Hi,

    The following should help you :-

            private static byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, ImageFormat formatOfImage)
            {
                byte[] ret;
    
                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        imageToConvert.Save(ms, formatOfImage);
                        ret = ms.ToArray();
                    }
                }
                catch (Exception) { throw; }
    
                return ret;
            }
    
            private static Image ConvertByteArrayToImage(byte[] byteArrayToConvert)
            {
                Image ret;
    
                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.Write(byteArrayToConvert, 0, byteArrayToConvert.Length);
                        ret = new Bitmap(ms);
                    }
                }
                catch (Exception) { throw; }
    
                return ret;
            }
    WmDev, Image.FromStream doesn't seem to be supported on the Compact Framework.

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by warrentang Wednesday, July 15, 2009 12:41 AM
    Wednesday, July 8, 2009 7:13 PM
  • But WmDev...

    Please, how do I Call this method from my Button click event?

    for instance, i copied your code to my project:

     public Image byteArrayToImage(byte[] byteArrayIn)
            {
                MemoryStream ms = new MemoryStream(byteArrayIn);

                Image returnImage = Image.FromStream(ms);
                pictureBox1.Image = returnImage;
                return returnImage;

            }

    How do I use it in this event:

               

     private void btnShowImage_Click(object sender, EventArgs e)
            {
                byteArrayToImage(.......?????);

            }

    My question is, which argument fits???


    • Edited by ALASDev Tuesday, March 8, 2016 10:38 AM
    Tuesday, March 8, 2016 10:34 AM
  • Is anybody here to help me???
    Tuesday, March 8, 2016 10:40 AM