convert image to byte array and the other way?

G

Guest

I am trying to convert an image to an byte array (tobytes() method) and from
byte array back to an image (frombytes() method). The problem i have here is
say the image size is 285 bytes but the total bytes is 635. I know it would
include the header but the header should be the same size regardless of the
image size. I tested the other image size of 407 bytes and the total bytes
array is 727. I am not sure what is wrong? your help would be appreciate
it.
 
S

Stoitcho Goutsev \(100\)

What is the image size and total size? How do you calculate the image size?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

What is the problem with a variable size?

Just handle the entire array as one entity.

You can you use a MemoryStream to convert to/from byte
 
J

JustinC

If you have an image, you can have a file. If you have a file you have
a filestream. If you have a filestream you can get an array of bytes.
You might have to mess around with what streams you need, probably a
mix of FileStream and MemoryStream.
 
T

Tom Spink

piggy said:
I am trying to convert an image to an byte array (tobytes() method) and
from
byte array back to an image (frombytes() method). The problem i have here
is
say the image size is 285 bytes but the total bytes is 635. I know it
would include the header but the header should be the same size regardless
of the
image size. I tested the other image size of 407 bytes and the total
bytes
array is 727. I am not sure what is wrong? your help would be appreciate
it.

Hi Piggy,

Can you post some code, demonstrating what you are doing, and how you are
obtaining 'image size' and 'total bytes'?
 
G

Guest

Your Image Class sports a Save and a FromStream method, both of which can
work with a stream such as a MemoryStream. MemoryStream class has a
contructor overload that accepts a byte array, and it has a ToArray method
that gives you back a byte array. The header (Jpeg, gif, etc is simply part
of this array and you should not need to worry about it at all.
Hope that helps,
Peter
 
G

Guest

The problem is that we try to send as small as package size over the network
as possible. I am just curious. If that is how it works and there are no
ways to fix it then have to accept it. Thanks for your comments.
 
G

Guest

i don't calculate the image size. I just check the image size from window
explorer. Is there a way to do it?
 
G

Guest

I agree that header is a part of the array but shoudn't it be the same size
regarless of the image size? It will be different depending on the image
type as well.
 

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

Top