从流中获取图像时参数无效 [英] Parameter is not valid when getting image from stream
问题描述
我有这个代码:
MemoryStream ms = new MemoryStream(newbytes, 0,
newbytes.Length);
ms.Position = 0;
ms.Write(newbytes, 0, newbytes.Length);
Image img = Image.FromStream(ms);
img.Save(@"C:\Users\gsira\Pictures\Blue hills5.jpg");
我在 Image.FromStream(ms) 调用中收到此错误:
I get this error at the Image.FromStream(ms) call:
System.ArgumentException:参数无效.在 System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateIma
System.ArgumentException: Parameter is not valid. at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateIma
我该如何解决这个问题?解决这个问题的几个链接(一个在 MSDN 线程上)被破坏了,所以我迷路了.
How can I resolve this? A couple of links which solve this problem (one on an MSDN thread) are broken so I am lost.
推荐答案
如果您使用字节数组(我假设 newbytes
是这样)初始化 MemoryStream,则不需要写信给它.
If you initialise a MemoryStream with a byte array (which is what I am assuming newbytes
to be), you should not need to write to it.
示例中对 Write(newbytes, 0, newbytes.Length)
的调用是完全多余的.
The call to Write(newbytes, 0, newbytes.Length)
in your sample is completely redundant.
var s = new MemoryStream(newbytes, 0, newbytes.Length);
var i = Image.FromStream(s);
i.Save(@"C:\Users\gsira\Pictures\Blue hills5.jpg");
以上对我有用,其中 newbytes
是我硬盘上图像文件内容的字节数组.
The above works for me where newbytes
is a byte array of the contents of an image file on my hard drive.
这篇关于从流中获取图像时参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!