none
Converting an Image/BitmapImage object into Byte Array and vice versa RRS feed

  • Question

  • Hi,  I have an requirement of developing an application (WPF) that recevies image object from WCF service.

    my approch is to convert the image object into stream of bytes (Byte array) in WCF part and then send to  client application. Then client application converts the byte array back to image object.

    I am not able to find right sample for my approach.

    would you give me guidance and would be gr8 if you provide some samples.

    thanks,
    Senthil
         
    Monday, April 21, 2008 8:30 PM

Answers

  • The following two helper methods should be able to do the trick:
    public BitmapImage ImageFromBuffer(Byte[] bytes)
    {
        MemoryStream stream = new MemoryStream(bytes);
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = stream;
        image.EndInit();
        return image;
    }

    public Byte[] BufferFromImage(BitmapImage imageSource)
    {
        Stream stream = imageSource.StreamSource;
        Byte[] buffer = null;
        if (stream != null && stream.Length > 0)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                buffer = br.ReadBytes((Int32)stream.Length);
            }
        }

        return buffer;
    }

    I don't fully test the two methods, but the same concept should be true.

    Hope this helps
    Wednesday, April 23, 2008 5:26 AM

All replies

  • The following two helper methods should be able to do the trick:
    public BitmapImage ImageFromBuffer(Byte[] bytes)
    {
        MemoryStream stream = new MemoryStream(bytes);
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = stream;
        image.EndInit();
        return image;
    }

    public Byte[] BufferFromImage(BitmapImage imageSource)
    {
        Stream stream = imageSource.StreamSource;
        Byte[] buffer = null;
        if (stream != null && stream.Length > 0)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                buffer = br.ReadBytes((Int32)stream.Length);
            }
        }

        return buffer;
    }

    I don't fully test the two methods, but the same concept should be true.

    Hope this helps
    Wednesday, April 23, 2008 5:26 AM
  • I am doing the same thing as Senthil, but am curious about one part. I was able to get this working with the stream, but in the past I've been told you always close a stream - no questions asked. When I do that my ImageSource goes back to being essentially nothing. If I leave the stream open it functions fine. Is this an acceptable practice with this circumstance?
    Monday, March 2, 2009 10:16 PM
  • As digitallnet , I'm very curious to know the answer to that also...
    Friday, August 14, 2009 7:03 AM
  • according to http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.streamsource.aspx Stream might be closed by BitmapImage class itself or it may be garbage collected.

    S.G.
    Tuesday, December 15, 2009 10:15 AM
    Moderator
  • just to be safe, set the stream.Position = 0 before calling ReadBytes.
    Saturday, January 23, 2010 12:59 AM
  • The following two helper methods should be able to do the trick:
    public BitmapImage ImageFromBuffer(Byte[] bytes)
    {
        MemoryStream stream = new MemoryStream(bytes);
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = stream;
        image.EndInit();
        return image;
    }

    i've just tried this an it doesn't work. i get a "System.NotSupportedException: No imaging component suitable to complete this operation was found."

    not really surprising as nowhere do you set the width,height or the pixel format of the image - so there is no way this could work.

    just to be clear, my byte[] pixelArray is a simple standard RGB arrangement of pixels. so pixelArray[0], pixelArray[1], pixelArray[2] represent the RGB colours of the first pixel and pixelArray[3], pixelArray[4], pixelArray[5] the second one and so on. obviously you'd need to at least know the width in pixels to make sense of this image.

    any other suggestion??

    Wednesday, August 18, 2010 4:15 PM
  • Can you please post your solution?

     

    Thanks

    Wednesday, March 16, 2011 10:12 PM
  • No go for silverlight 4.0...there is no property in BitMapImage that returns a Stream.
    JP
    Saturday, April 2, 2011 2:22 AM
  • BeginInit and EndInit causes an error, what shall I do?!
    Friday, November 29, 2013 5:47 PM