Image.FromStream() method returns Invalid Argument exception

前端 未结 10 2502
臣服心动
臣服心动 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:11

    I've had this problem when doing this:

    MemoryStream stream = new MemoryStream();
    screenshot.Save(stream, ImageFormat.Png);
    byte[] bytes = new byte[stream.Length];
    stream.Save(bytes, 0, steam.Length);
    

    With the last 2 lines being the problem. I fixed it by doing this:

    MemoryStream stream = new MemoryStream();
    screenshot.Save(stream, ImageFormat.Png);
    byte[] bytes = stream.ToArray();
    

    And then this worked:

    MemoryStream stream = new MemoryStream(bytes);
    var newImage = System.Drawing.Image.FromStream(stream);
    stream.Dispose();
    

提交回复
热议问题