How do I save an image from byte[] ?

  • Thread starter Thread starter Du Dang
  • Start date Start date
D

Du Dang

I'm trying to learn how socket operates and I am successfully got
the server to reply, but now i don't know how to retreive the data, in this
case it's an image.

this is the header and follow it is an image

=================================
"HTTP/1.1 200 OK
Date: Tue, 16 Mar 2004 12:30:18 GMT
Server: Apache/1.3.28 (Unix) PHP/4.3.3
Last-Modified: Sat, 06 Mar 2004 01:01:09 GMT
ETag: "8f5a8a-4b2ea-404922d5"
Accept-Ranges: bytes
Content-Length: 307946
Keep-Alive: timeout=60, max=200
Connection: Keep-Alive
Content-Type: image/jpeg

"
<image content here>
=================================

I tried to use FileStream to write the image but I can't see the image after
it been downloaded and the size is inconsistant with the "Content-Length"
attribute.

buf[1024]
do {
size = socket.Receive(buf, buf.length, 0);
fs.write(buf, 0, buf.length);
}while (size > 0);

btw does "Content-length" includes the header size or is it just the data?

Any help is greatly appreciated !!!

Thanks,

Du
 
Du Dang said:
I tried to use FileStream to write the image but I can't see the image after
it been downloaded and the size is inconsistant with the "Content-Length"
attribute.

buf[1024]
do {
size = socket.Receive(buf, buf.length, 0);
fs.write(buf, 0, buf.length);
}while (size > 0);

These lines are the problem - you're dumping the whole buffer, however
much you actually read into it. You should call

fs.Write(buf, 0, size)

But you should also avoid calling Write at all if size is <= 0. I would
do:

while ( (size=socket.Receive(buf)) > 0)
{
fs.Write(buf, 0, size);
}
btw does "Content-length" includes the header size or is it just the data?

Just the content.
 
Hi Du,

Is the image being transfered using HTTP?
If so you should use a WebRequest object instead of a socket, if you use a
socket you would have to deal with the HTTP protocol itself.

Cheers,
 
thanks for the lightning fast reply :-)
fs.Write(buf, 0, size)

actually this is how I coded ... i made the error during the post
while ( (size=socket.Receive(buf)) > 0)
{
fs.Write(buf, 0, size);
}

this is much better .. so clean!!!


I tried what you suggested, but I still have the same problem
I tally up the "size" and it doesn't match with the "Content-Length"

// from the header
"Content-Length: 307946"

// total byte counted from content part only
Total received: 307224 bytes

and another weird thing when I call my very first Receive() (get header)
size = socket.Receive (buf);

size is always equal to the buffer size(1024)
but the header only 302bytes, and no data after 302th byte either

Thanks,

Du
Jon Skeet said:
Du Dang said:
I tried to use FileStream to write the image but I can't see the image after
it been downloaded and the size is inconsistant with the "Content-Length"
attribute.

buf[1024]
do {
size = socket.Receive(buf, buf.length, 0);
fs.write(buf, 0, buf.length);
}while (size > 0);

These lines are the problem - you're dumping the whole buffer, however
much you actually read into it. You should call

fs.Write(buf, 0, size)

But you should also avoid calling Write at all if size is <= 0. I would
do:

while ( (size=socket.Receive(buf)) > 0)
{
fs.Write(buf, 0, size);
}
btw does "Content-length" includes the header size or is it just the
data?

Just the content.
 
Actually I was try to learn how socket and http work
more so than trying to download the image.

regardless, thank you so much for your help
I'll use your advice on future projects

thanks again,

Du

Ignacio Machin ( .NET/ C# MVP ) said:
Hi Du,

Is the image being transfered using HTTP?
If so you should use a WebRequest object instead of a socket, if you use a
socket you would have to deal with the HTTP protocol itself.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Du Dang said:
I'm trying to learn how socket operates and I am successfully got
the server to reply, but now i don't know how to retreive the data, in this
case it's an image.

this is the header and follow it is an image

=================================
"HTTP/1.1 200 OK
Date: Tue, 16 Mar 2004 12:30:18 GMT
Server: Apache/1.3.28 (Unix) PHP/4.3.3
Last-Modified: Sat, 06 Mar 2004 01:01:09 GMT
ETag: "8f5a8a-4b2ea-404922d5"
Accept-Ranges: bytes
Content-Length: 307946
Keep-Alive: timeout=60, max=200
Connection: Keep-Alive
Content-Type: image/jpeg

"
<image content here>
=================================

I tried to use FileStream to write the image but I can't see the image after
it been downloaded and the size is inconsistant with the "Content-Length"
attribute.

buf[1024]
do {
size = socket.Receive(buf, buf.length, 0);
fs.write(buf, 0, buf.length);
}while (size > 0);

btw does "Content-length" includes the header size or is it just the data?

Any help is greatly appreciated !!!

Thanks,

Du
 
Du Dang said:
actually this is how I coded ... i made the error during the post

That's where it's important to actually post your real code.

Please post a short but complete example which demonstrates the
problem? See http://www.pobox.com/~skeet/csharp/complete.html for
exactly what I mean by this.

and another weird thing when I call my very first Receive() (get header)
size = socket.Receive (buf);

size is always equal to the buffer size(1024)
but the header only 302bytes, and no data after 302th byte either

What do you mean by "no data after 302th byte"? If socket.Receive is
returning 1024, then it's read 1024 bytes from the socket. I don't
think it's particularly odd for it to receive a full 1K of data - the
TCP layer isn't going to know the difference between header and
content, after all.
 
Just got it !!! :-) .. part of the data is trailling off the header all
along

That's where it's important to actually post your real code.

will do next time !!


thanks again for your help,

Du
 
Back
Top