Raghu
Raghu

Reputation: 41

Cannot implicitly convert 'byte[]' to 'byte'

My given code has an error of type conversion

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

Upvotes: 4

Views: 18644

Answers (5)

Manoj Nayak
Manoj Nayak

Reputation: 2509

Check this:

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

Upvotes: 0

Chirag Vidani
Chirag Vidani

Reputation: 2587

You can use the below code:

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

This code coverts postedfile to byte stream

Upvotes: 1

you can not assign a byte array to byte

try this

byte[] bytearray = new byte[imglength];

Upvotes: 4

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

Structure is like this

byte[] Buffer = new byte[imglength];

Upvotes: 1

Tim
Tim

Reputation: 28530

You're trying to assign an array of bytes (byte[]) to a single byte, hence the error.

Try the following code:

byte[] imgarray = new byte[imglength];

Upvotes: 8

Related Questions