Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 位图图像、字节数组和流!_C#_Arrays_Image_Stream_Byte - Fatal编程技术网

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

C# 位图图像、字节数组和流!,c#,arrays,image,stream,byte,C#,Arrays,Image,Stream,Byte,我有一个将文件提取到字节数组(数据)的函数 稍后,我将使用此字节数组构造System.Drawing.Image对象 (其中数据是字节数组) 我得到以下异常“ArgumentException:参数无效” 原始发布的文件包含500k jpeg图像 你知道为什么这不起作用吗 注意:我向您保证,我有一个有效的理由将其转换为字节数组,然后再转换为memorystream 您没有检查postedFile.InputStream.的返回值。。它根本不能保证在第一次调用时填充数组。这将在数据中留下损坏的JP

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

稍后,我将使用此字节数组构造System.Drawing.Image对象 (其中数据是字节数组)

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

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

你知道为什么这不起作用吗


注意:我向您保证,我有一个有效的理由将其转换为字节数组,然后再转换为memorystream

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

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

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


您可以尝试使用另一个您知道是支持格式的图像。

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


您可以尝试使用另一个您知道是支持格式的图像。

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

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

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

int contentLength = postedFile.ContentLength;
byte[] data = new byte[contentLength];
for (int pos = 0; pos < contentLength; ) {
   pos += postedFile.InputStream.Read(data, pos, contentLength - pos);
}
int contentLength=postedFile.contentLength;
字节[]数据=新字节[contentLength];
对于(int pos=0;pos
这是读取流时的常见错误。这个问题我已经见过很多次了

编辑:
正如Matthew所建议的,通过检查流的早期结束,代码将是:

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;
}
int contentLength=postedFile.contentLength;
字节[]数据=新字节[contentLength];
对于(int pos=0;pos
这很可能是因为您没有将所有文件数据放入字节数组。Read方法不必返回您请求的字节数,它返回实际放入数组的字节数。您必须循环,直到获得所有数据:

int contentLength = postedFile.ContentLength;
byte[] data = new byte[contentLength];
for (int pos = 0; pos < contentLength; ) {
   pos += postedFile.InputStream.Read(data, pos, contentLength - pos);
}
int contentLength=postedFile.contentLength;
字节[]数据=新字节[contentLength];
对于(int pos=0;pos
这是读取流时的常见错误。这个问题我已经见过很多次了

编辑:
正如Matthew所建议的,通过检查流的早期结束,代码将是:

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;
}
int contentLength=postedFile.contentLength;
字节[]数据=新字节[contentLength];
对于(int pos=0;pos
您不这么做的任何原因:

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

您不这么做的任何原因:

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

数据是否只包含图像数据?你没有说什么类型的
postedFile
。你从哪里得到这个错误?您发布的代码应该可以正常工作…postedFile是一个HttpPostedFileBase。该错误发生在行图像位图=Image.FromStream(ms);:(数据是否只包含图像数据?您没有说什么类型的
postedFile
。您从哪里得到这个错误?您发布的代码应该可以正常工作…postedFile是一个HttpPostedFileBase。错误发生在image bitmap=image.FromStream(ms);:(使用了建议的代码:int contentLength=postedFile.contentLength;byte[]data=new byte[contentLength];for(int pos=0;pos