Convert to Byte

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
 
A

Arne Vajhøj

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
 
S

shapper

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
 
S

shapper

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
 
A

Arne Vajhøj

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
 
G

Göran Andersson

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top