convert image to byte array and the other way?

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
What is the image size and total size? How do you calculate the image size?
 
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
 
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.
 
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'?
 
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
 
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.
 
i don't calculate the image size. I just check the image size from window
explorer. Is there a way to do it?
 
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.
 
Back
Top