Convert to Byte

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am converting a variable, source, of type HttpPostedFileBase to
Byte[]:

Byte[] destination = new Byte[source.File.ContentLength];
source.File.InputStream.Read(destination, 0,
source.File.ContentLength);
return destination;

The destination length is the same as the source.

However, all the bytes in destination are 0.

Any idea what I might be doing wrong?

Thanks,

Miguel
 
I am converting a variable, source, of type HttpPostedFileBase to
Byte[]:

Byte[] destination = new Byte[source.File.ContentLength];
source.File.InputStream.Read(destination, 0,
source.File.ContentLength);
return destination;

The destination length is the same as the source.

However, all the bytes in destination are 0.

Any idea what I might be doing wrong?

You should use a while loop to read in.

But even with the above code you should at least read
one byte from the file.

Maybe you should post some more context.

Arne
 
I am converting a variable, source, of type HttpPostedFileBase to
Byte[]:
         Byte[] destination = new Byte[source.File.ContentLength];
         source.File.InputStream.Read(destination, 0,
source.File.ContentLength);
         return destination;
The destination length is the same as the source.
However, all the bytes in destination are 0.
Any idea what I might be doing wrong?

You should use a while loop to read in.

But even with the above code you should at least read
one byte from the file.

Maybe you should post some more context.

Arne

I think I found the problem but not the solution. Before this code I
have the following:
Bitmap image = new Bitmap(source.File.InputStream);

So I think before I create the Byte I need to move the stream to the
start?!
If I remove this Bitmap part it works fine ...

Any idea?

Thanks,
Miguel
 
I am converting a variable, source, of type HttpPostedFileBase to
Byte[]:
         Byte[] destination = new Byte[source.File.ContentLength];
         source.File.InputStream.Read(destination, 0,
source.File.ContentLength);
         return destination;
The destination length is the same as the source.
However, all the bytes in destination are 0.
Any idea what I might be doing wrong?

You should use a while loop to read in.

But even with the above code you should at least read
one byte from the file.

Maybe you should post some more context.

Arne

I solved it by using:

Byte[] destination = new Byte[source.File.ContentLength];
source.File.InputStream.Position = 0;
source.File.InputStream.Read(destination, 0,
source.File.ContentLength);
return destination;

Thank You,
Miguel
 
I am converting a variable, source, of type HttpPostedFileBase to
Byte[]:
Byte[] destination = new Byte[source.File.ContentLength];
source.File.InputStream.Read(destination, 0,
source.File.ContentLength);
return destination;
The destination length is the same as the source.
However, all the bytes in destination are 0.
Any idea what I might be doing wrong?

You should use a while loop to read in.

But even with the above code you should at least read
one byte from the file.

Maybe you should post some more context.

I solved it by using:

Byte[] destination = new Byte[source.File.ContentLength];
source.File.InputStream.Position = 0;
source.File.InputStream.Read(destination, 0,
source.File.ContentLength);
return destination;

Something relevant *was* missing.

:-)

BTW, I will still recommend the while loop.

Arne
 
shapper said:
I am converting a variable, source, of type HttpPostedFileBase to
Byte[]:
Byte[] destination = new Byte[source.File.ContentLength];
source.File.InputStream.Read(destination, 0,
source.File.ContentLength);
return destination;
The destination length is the same as the source.
However, all the bytes in destination are 0.
Any idea what I might be doing wrong?
You should use a while loop to read in.

But even with the above code you should at least read
one byte from the file.

Maybe you should post some more context.

Arne

I think I found the problem but not the solution. Before this code I
have the following:
Bitmap image = new Bitmap(source.File.InputStream);

So I think before I create the Byte I need to move the stream to the
start?!
If I remove this Bitmap part it works fine ...

Any idea?

Thanks,
Miguel

Read the data into the byte array first:

int size = source.File.ContentLength;
byte[] destination = new byte[size];
int pos = 0;
while (pos < size) {
int len = source.File.InputStream.Read(destination, pos,
source.File.ContentLength);
pos += len;
}

Then create the bitmap from the byte array:

using (var m = new MemoryStream(destination)) {
Bitmap image = new Bitmap(m);
}
 
Back
Top