Exception Converting Byte Array to Image

0

I'm using the following to convert incoming Byte array data from a network stream into an image to display on screen but after awhile of running fine I keep getting the exception "No imaging component suitable to complete this operation was found." with the inner exception of "The component cannot be found. (Exception from HRESULT: 0x88982F50)". I've looked at buffer size issues but I don't think that is it. Any thoughts?

public static ImageSource ByteToImage(byte[] imageData)
    {
        BitmapImage biImg = new BitmapImage();
        MemoryStream ms = new MemoryStream(imageData);
        try
        {
            biImg.BeginInit();
            biImg.StreamSource = ms;
            biImg.EndInit();
        }
        catch ( Exception e)
        {
            MessageBox.Show("1"+ e.InnerException);
        }

        ImageSource imgSrc = biImg as ImageSource;

        return imgSrc;
    }

Additional Info

Here is what I'm using in my HandlerThread which is receiving the Network Stream;

NetworkStream networkStream = new NetworkStream(handlerSocket);
            int thisRead = 0;
            int blockSize = 256000;
            Byte[] dataByte = new Byte[blockSize];
            lock (this)
            {
                while (running)
                {
                    thisRead = networkStream.Read(dataByte, 0, blockSize);
                    Dispatcher.BeginInvoke(new ThreadStart(delegate 
       { pictureBox1.Source = ByteToImage(dataByte); }));

                    if (thisRead == 0) break;
                }
            }
c#
image
bytearray
asked on Stack Overflow Jun 6, 2013 by windowsgm • edited Jun 6, 2013 by windowsgm

2 Answers

0

Temporary solution (and I know it's not good programming) but simply doing nothing in the catch block will allow me to bypass the error and continue running. Will do for the time being till I get a better solution.

answered on Stack Overflow Jun 6, 2013 by windowsgm
0

You're breaking the stream into blocks of 256kB (a little less than 256kiB) which I assume is a size chosen arbitrarily, but then you assume that each block contains exactly one image. When that assumption break (probably always) your code breaks.

Why aren't you just using StreamSource = networkStream;?

answered on Stack Overflow Jun 6, 2013 by Ben Voigt

User contributions licensed under CC BY-SA 3.0