Image.FromStream() method returns Invalid Argument exception

前端 未结 10 2449
臣服心动
臣服心动 2020-11-29 09:18

I am capturing images from a smart camera imager and receiving the byte array from the camera through socket programming (.NET application is the client, camera is the serve

相关标签:
10条回答
  • 2020-11-29 10:14

    Image.FromStream() expects a stream that contains ONLY one image!

    It resets the stream.Position to 0. I've you have a stream that contains multiple images or other stuff, you have to read your image data into a byte array and to initialize a MemoryStream with that:

    Image.FromStream(new MemoryStream(myImageByteArray));
    

    The MemoryStream has to remain open as long as the image is in use.

    I've learned that the hard way, too.

    0 讨论(0)
  • 2020-11-29 10:15

    Maybe the image is embedded in an OLE field and you have to consider the 88 bytes OLE header plus payload:

    byteBlobData = (Byte[]) reader.GetValue(0);
    stream = new MemoryStream(byteBlobData, 88, byteBlobData.Length - 88);
    img = Image.FromStream(stream);
    
    0 讨论(0)
  • 2020-11-29 10:19

    Try this:

    public Image byteArrayToImage(byte[] item)
    {          
       Image img=Image.FromStream(new MemoryStream(item)); 
       img.Save(Response.OutputStream, ImageFormat.Gif);
       return img;
    }
    

    Hope it helps!

    0 讨论(0)
  • 2020-11-29 10:20

    I've had the same problem in the past and it was caused by a leak within the windows GDI libraries, which is what 'Bitmap' uses. If this happening all the time for you then its probably unrelated, however.

    0 讨论(0)
提交回复
热议问题