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;
                }
            }
windowsgm
  • 1,566
  • 4
  • 23
  • 55
  • 1
    Are you sure the bytes are correct? Try to write them on disk and check if you can open the file with an image editor. – Pragmateek Jun 06 '13 at 17:14
  • 1
    Your byte array includes headers? (.bmp, .jpg, etc) or it's raw data? – Nic007 Jun 06 '13 at 17:15
  • 1
    Are you debugging this to determine at which line the exception occurs? – DonBoitnott Jun 06 '13 at 17:15
  • @DonBoitnott and Serious That's difficult because it works for awhile so I can't pinpoint the time I'd want to debug in the method without F5ing with each frame and don't really fancy having to open every image that saves to disk. Nic007 raw byte[] I'm pretty sure. – windowsgm Jun 06 '13 at 17:36
  • @killianmcc This might have been addressed already: http://stackoverflow.com/questions/9724876/how-to-create-filestreame-from-byte – DonBoitnott Jun 06 '13 at 18:30

2 Answers2

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.

windowsgm
  • 1,566
  • 4
  • 23
  • 55
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;?

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720