views:

762

answers:

7

I am serializing a JPEG Image in c#.net. I am simply converting it into a byte steam and sending it through web service. I observed that serialized byte stream is 30 times more than that of the size of actual image. Can any one suggest me a better approach to serialize and stay relative to the size of the actual image?

A: 

Disclaimer: Non-informed person speaking
Its a tradeoff between openness/standards and performance.. Maybe you're using something like SOAP that adds a lot of protocol overhead bytes to the data packet. If size is a vital constraint, try sending it across as a pure binary stream... the actual syntax maybe someone else can pitch in.

Gishu
+2  A: 

JPEG is a compression technology, and it is expected that it will expand greatly once you read it in. This is the nature of the file format. Try to find a way to send the original JPEG file without reading it as an image first.

Mark Ransom
+2  A: 
  1. You need to read original image stream using FileStream and then pass it to the Serializer using MemoryStream.
  2. If you can only use Image class the try to specify output format of byte array you're receiving.
dimarzionist
A: 

And if the size of the images you send by webservices can be large, maybe you can take a look to MTOM. It's a WS-* standard to optimize the size of message with binary attachments. It's now very integrated in stacks like Axis2 or Metro for Java or in .NET :

http://msdn.microsoft.com/en-us/library/aa528822.aspx (wse 3.0) http://msdn.microsoft.com/en-us/library/ms733742.aspx (wcf)

Matthieu
+1  A: 

Consider using WCF streaming. I didn't notice an overhead transmitting files via this service.

MSDN:

Large Data and Streaming

aku
A: 

Why not convert it to a Base64String?

byte[] arr = File.ReadAllBytes(filename);
string str = Convert.ToBase64String(arr);

On the other end you can change it back to a byte[] by going:

byte[] arr = Convert.FromBase64String(string);
Ash
Don't do that—adding another layer of encodings can only increase the size.
Hank Gay
A: 

Maybe just host the images on a web server and send a URL in the web service reply rather than the serialised image. This will also allow the client to cache the image locally when it can.

X-Cubed