无法将“byte[]”隐式转换为“byte”

4

我的代码存在类型转换错误

 int imglength = FileUpload2.PostedFile.ContentLength;
 byte imgarray=new byte[imglength];

5
"byte" 和 "byte[]" 不是相同的概念。前者表示一个字节,后者表示一个字节数组。请尝试使用以下代码:byte[] imgarray = new byte[imglength]; - Tim
5个回答

8

您正在尝试将一个字节数组 (byte[]) 赋值给单个字节,因此出现错误。

请尝试以下代码:

byte[] imgarray = new byte[imglength];

4

你不能将一个字节数组分配给一个字节

尝试这样做

byte[] bytearray = new byte[imglength];

1
结构就像这样。
byte[] Buffer = new byte[imglength];

1
你可以使用以下代码:

int imageSize = fuImage.PostedFile.ContentLength;
System.IO.Stream imageStream = fuImage.PostedFile.InputStream;
byte[] imageContent = new byte[imageSize];
int status = imageStream.Read(imageContent, 0, imageSize);

这段代码将postedfile转换为字节流。

0

请检查:

int imgLength = FileUpload2.PostedFile.ContentLength;
byte[] revLength= BitConverter.GetBytes(imgLength);
Array.Reverse(revLength);
byte[] imgLengthB = revLength;

4
如果您发布代码、XML或数据示例,请在文本编辑器中突出显示这些行,并单击编辑器工具栏上的“代码示例”按钮({})进行格式化和语法突出显示! - marc_s

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接