C# 位图图像、字节数组和流!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/932353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:36:45  来源:igfitidea点击:

C# bitmap images, byte arrays and streams!

c#arraysimagestreambyte

提问by iasksillyquestions

I have a function which extracts a file into a byte array (data).

我有一个将文件提取到字节数组(数据)中的函数。

        int contentLength = postedFile.ContentLength;
        byte[] data = new byte[contentLength];
        postedFile.InputStream.Read(data, 0, contentLength);

Later I use this byte array to construct an System.Drawing.Image object (where data is the byte array)

后来我用这个字节数组构造了一个 System.Drawing.Image 对象(其中 data 是字节数组)

       MemoryStream ms = new MemoryStream(data);
       Image bitmap = Image.FromStream(ms);

I get the following exception "ArgumentException: Parameter is not valid."

我收到以下异常“ArgumentException:参数无效。”

The original posted file contained a 500k jpeg image...

原始发布的文件包含一个 500k jpeg 图像...

Any ideas why this isnt working?

任何想法为什么这不起作用?

Note: I assure you I have a valid reason for converting to a byte array and then to a memorystream!!

注意:我向您保证,我有充分的理由先转换为字节数组,然后再转换为内存流!!

采纳答案by Guffa

That's most likely because you didn't get all the file data into the byte array. The Read method doesn't have to return as many bytes as you request, and it returns the number of bytes actually put in the array. You have to loop until you have gotten all the data:

这很可能是因为您没有将所有文件数据都放入字节数组中。Read 方法不必返回与您请求的一样多的字节,它返回实际放入数组的字节数。您必须循环直到获得所有数据:

int contentLength = postedFile.ContentLength;
byte[] data = new byte[contentLength];
for (int pos = 0; pos < contentLength; ) {
   pos += postedFile.InputStream.Read(data, pos, contentLength - pos);
}

This is a common mistake when reading from a stream. I have seen this problem a lot of times.

这是从流中读取时的常见错误。我已经多次看到这个问题。

Edit:
With the check for an early end of stream, as Matthew suggested, the code would be:

编辑:
通过检查流的早期结束,正如马修建议的那样,代码将是:

int contentLength = postedFile.ContentLength;
byte[] data = new byte[contentLength];
for (int pos = 0; pos < contentLength; ) {
   int len = postedFile.InputStream.Read(data, pos, contentLength - pos);
   if (len == 0) {
      throw new ApplicationException("Upload aborted.");
   }
   pos += len;
}

回答by Matthew Flaschen

You're not checking the return value of postedFile.InputStream.Read. It is notat all guaranteed to fill the array on the first call. That will leave a corrupt JPEG in data (0's instead of file content).

您没有检查postedFile.InputStream 的返回值。阅读。根本不能保证在第一次调用时填充数组。这将在数​​据中留下损坏的 JPEG(0 而不是文件内容)。

回答by Eclipse

I have had problems loading images in .NET that were openable by more robust image libraries. It's possible that the specific jpeg image you have is not supported by .NET. jpeg files are not just one type of encoding, there's a variety of possible compression schemes allowed.

我在 .NET 中加载图像时遇到了问题,这些图像可由更强大的图像库打开。.NET 可能不支持您拥有的特定 jpeg 图像。jpeg 文件不仅仅是一种编码类型,还允许使用多种可能的压缩方案。

You could try it with another image that you know is in a supported format.

您可以尝试使用其他已知格式受支持的图像。

回答by Kevin Pullin

Have you checked the return value from the Read() call to verify that is actually reading all of the content? Perhaps Read() is only returning a portion of the stream, requiring you to loop the Read() call until all of the bytes are consumed.

您是否检查了 Read() 调用的返回值以验证是否实际读取了所有内容?也许 Read() 只返回流的一部分,需要您循环 Read() 调用,直到消耗完所有字节。

回答by jrista

Any reason why you don't simply do this:

你不简单地这样做的任何原因:

Image bitmap = Image.FromStream(postedFile.InputStream);