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;
}
}
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.
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;
?
User contributions licensed under CC BY-SA 3.0