PNG image from website -> picture box (using sockets)

1

1388-2/HB

I've got a small sockets application where I communicate with a web server
via async sockets method.

Using the ASCIIEncoding Class, I convert strings to byte arrays (and vice
versa) in conjunction with beginSend and beginReceive (and their
corresponding callbacks). So long as the web server and I just deal in
text, life is easy.

Now there's an image I want to request and then display in my app, it's a
dynamic PNG image that's generated by some .aspx page on the web server. So
my GET, to request this image, looks like this:

GET /MakeThePNG.aspx HTTP/1.1
Host: www.url.com


And here's the website's response:

HTTP/1.1 200 OK
Date: Thu, 01 Dec 2005 20:28:01 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Transfer-Encoding: chunked
Cache-Control: private, max-age=1800
Content-Type: image/png

39c9
?PNG
<a bunch of data>
0


I need to turn this response (sans the header, I'm assuming, but I'm unsure
which portions of the body) into an image object that I can display in a
picture box & save to disk. I've tried messing around with a Memory Stream,
and with that, tried including the whole body portion, just the <a bunch of
data> portion, but my attempts thus far are ultimately met with an Invalid
Argument exception when I try to make a new Image.FromStream with my Memory
Stream. I've tried to save said portions of the buffer to disk, but it does
not make a valid png file. So I'm not carving the data out of the body
correctly, or, it's not encoded correctly, or... ? I'm stumped.
 
1

1388-2/HB

Ok, the answer was to read up on how PNGs are put together.

http://www.w3.org/TR/PNG/

PNGs start with with 8 "signature" bytes, 137 80 78 71 13 10 26 10. Those
are the first 8 bytes of the PNG file.

PNGs end with an "IEND" chunk, literally "IEND" (73 69 78 68), followed by 4
more bytes (CRC portion of the chunk). Those are the last 8 bytes of the
PNG file.

As soon as I exctracted the bytes from the Signature to the IEND chunk
(inclusive) and fed it to a memory stream, thus correctly carving out the
whole PNG file and nothing but the PNG file, Image.FromStream magically
turned the memory stream into an image.
 
B

Brian Henry

why dont you just use a HTTP stream and read it as binary?! its so much
easier
 

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