Valhalla Legends Forums Archive | .NET Platform | PNG Sent over TcpClient()

AuthorMessageTime
Spilled[DW]
Ok so here is the problem I'm having. I'm using TcpClient to connect to a proxy and request a webpage. When i recieve the webpage my program finds an Image and decides to download it. So using the GET method i request the Image and it is sent to me. Here is the packet log:

[Code]
0000  48 54 54 50 2F 31 2E 31 20 32 30 30 20 4F 4B 0D    HTTP/1.1 200 OK.
0010  0A 44 61 74 65 3A 20 54 68 75 2C 20 32 35 20 44    .Date: Thu, 25 D
0020  65 63 20 32 30 30 38 20 30 35 3A 32 34 3A 35 37    ec 2008 05:24:57
0030  20 47 4D 54 0D 0A 53 65 72 76 65 72 3A 20 41 70    GMT..Server: Ap
0040  61 63 68 65 2F 31 2E 33 2E 32 37 20 28 55 6E 69    ache/1.3.27 (Uni
0050  78 29 20 6D 6F 64 5F 70 65 72 6C 2F 31 2E 32 37    x) mod_perl/1.27
0060  20 50 48 50 2F 34 2E 34 2E 36 0D 0A 50 72 61 67    PHP/4.4.6..Prag
0070  6D 61 3A 20 6E 6F 2D 63 61 63 68 65 43 61 63 68    ma: no-cacheCach
0080  65 2D 63 6F 6E 74 72 6F 6C 3A 20 6E 6F 2D 63 61    e-control: no-ca
0090  63 68 65 0D 0A 53 65 74 2D 43 6F 6F 6B 69 65 3A    che..Set-Cookie:
00A0  20 4D 46 49 6D 61 67 65 3D 53 59 47 6B 68 4A 44    MFImage=SYGkhJD
00B0  51 58 63 3B 20 65 78 70 69 72 65 73 3D 46 72 69    QXc; expires=Fri
00C0  2C 20 32 35 2D 44 65 63 2D 32 30 30 39 20 30 35    , 25-Dec-2009 05
00D0  3A 32 34 3A 35 37 20 47 4D 54 3B 20 70 61 74 68    :24:57 GMT; path
00E0  3D 2F 3B 0D 0A 50 72 61 67 6D 61 3A 20 6E 6F 2D    =/;..Pragma: no-
00F0  63 61 63 68 65 0D 0A 43 61 63 68 65 2D 63 6F 6E    cache..Cache-con
0100  74 72 6F 6C 3A 20 6E 6F 2D 63 61 63 68 65 0D 0A    trol: no-cache..
0110  4C 61 73 74 2D 4D 6F 64 69 66 69 65 64 3A 20 46    Last-Modified: F
0120  72 69 2C 20 31 32 20 41 75 67 20 32 30 30 35 20    ri, 12 Aug 2005
0130  32 33 3A 34 39 3A 31 33 20 47 4D 54 0D 0A 43 6F    23:49:13 GMT..Co
0140  6E 6E 65 63 74 69 6F 6E 3A 20 63 6C 6F 73 65 0D    nnection: close.
0150  0A 43 6F 6E 74 65 6E 74 2D 54 79 70 65 3A 20 69    .Content-Type: i
0160  6D 61 67 65 2F 70 6E 67 0D 0A 45 78 70 69 72 65    mage/png..Expire
0170  73 3A 20 54 68 75 2C 20 32 35 20 44 65 63 20 32    s: Thu, 25 Dec 2
0180  30 30 38 20 30 35 3A 32 34 3A 35 37 20 47 4D 54    008 05:24:57 GMT
0190  0D 0A 0D 0A 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D    .....PNG........
01A0  49 48 44 52 00 00 00 5A 00 00 00 14 04 03 00 00    IHDR...Z........
01B0  00 FF D4 37 2D 00 00 00 30 50 4C 54 45 04 02 04    ...7-...0PLTE...
01C0  24 22 24 74 76 74 44 46 44 8C 8A 8C BC BA BC 54    $"$tvtDFD......T
01D0  56 54 9C 9A 9C 64 66 64 34 32 34 14 12 14 FC FE    VT...dfd424.....
01E0  FC EC EE EC CC CE CC AC AA AC DC DE DC 7E 9E 46    .............~.F
01F0  91 00 00 01 74 49 44 41 54 78 9C B5 92 BD 4B 03    ....tIDATx....K.
0200  41 10 C5 5F A2 89 7A 41 02 B6 69 FC E8 BC 32 92    A.._..zA..i...2.
0210  E6 10 D2 DB 88 90 C6 43 B4 4F 61 1A 41 48 13 B0    .......C.Oa.AH..
0220  B4 11 B4 4B 8A 03 B1 0A 42 CC 87 31 CE 9D 09 82    ...K....B..1....
0230  85 49 63 AD 7F C2 81 D8 A7 F1 ED 1D D1 70 9B C6    .Ic..........p..
0240  C2 57 CC EC 6F 66 77 66 76 59 E0 6F 5A 59 A7 56    .W..ofwfvY.oZY.V
0250  B9 D8 2A AC 29 4E 9E 9C 06 F1 AB C2 E6 0C B4 84    ..*.)N..........
0260  6A 00 0B 74 15 C0 B0 45 F6 19 5E 14 71 2F 34 0C    j..t...E..^.q/4.
0270  77 0F 80 A2 1C C8 50 1D 7A 91 27 86 33 72 28 5F    w.....P.z.'.3r(_
0280  1A E2 3A 9B DD 56 45 FD 7B 98 0F 0C BB 4E 55 58    ..:..VE.{....NUX
0290  A5 DC 84 AF A3 52 B1 4D 63 37 60 35 01 B3 CD 7A    .....R.Mc7`5...z
02A0  79 60 FC 88 62 57 43 CA 18 A9 1E 66 0B 66 8F AE    y`..bWC....f.f..
02B0  89 B8 EC 02 EC 9B E9 68 48 CD CB 11 ED 2D E7 2E    .......hH....-..
02C0  05 C5 E2 BC C6 1C B7 A4 44 43 75 03 4F D9 D8 48    ........DCu.O..H
02D0  3C 07 A8 F6 9D 34 9F 68 92 8E A0 9A 21 18 BF C6    <....4.h....!...
02E0  DA EF 40 42 F6 EC A9 74 04 A9 F1 20 B0 2D EC F0    ..@B...t... .-..
02F0  1E C6 87 78 53 E9 08 82 AB 92 1A 44 DE 50 77 B9    ...xS......D.Pw.
0300  88 7F 9E 31 B5 A4 D2 7D 1D 79 C9 4A 70 E6 A7 57    ...1...}.y.Jp..W
0310  5C 1D 67 41 AB 33 03 13 E2 04 B5 87 B0 78 FA A6    \.gA.3.......x..
0320  C0 00 DF C8 EE A9 D7 8B 22 90 72 83 12 B6 97 1B    ........".r.....
0330  93 D3 B2 51 56 2D CF BD DC E8 4E 43 A0 1E 36 AC    ...QV-....NC..6.
0340  F1 BB F0 4D 92 74 AF C4 65 FA BC 86 98 8C 17 F3    ...M.t..e.......
0350  A5 AB 46 3A 96 8E FA 6C 86 2F CF D0 F1 57 D9 D0    ..F:...l./...W..
0360  5D 3A E1 7F 98 8D FF A6 6F 32 59 DC 82 11 A2 B0    ]:......o2Y.....
0370  A5 00 00 00 00 49 45 4E 44 AE 42 60 82            .....IEND.B`.
[/Code]

Content-Type: image/png Tells my program that it is a PNG image file. Now my question is, How do i handle this data and create the png file? is the whole source what needs to be output and what do i use to output the data? BinaryWriter or StreamWriter? If its not the whole source what is the delimiter? Thanks in advance.
December 26, 2008, 7:03 AM
l2k-Shadow
just.. take the header out and dump the rest into a file ending with a .png extention? xD
December 26, 2008, 7:44 AM
Yegg
I'm not sure what the delimiter would be as I don't know much HTTP. I'd go with BinaryWriter when outputting the content data to a .png file. If you don't go with BinaryWriter, the data may not be written to the file properly and when you attempt to open the image file, you may get a corrupt file message or the image may look different than expected.
December 26, 2008, 8:52 AM
Barabajagal
0x0D 0x0A 0x0D 0x0A

Double NewLine is the header delimiter.
December 26, 2008, 8:54 AM
Ringo
I posted some VB6 code here to load MNG files, idk if this helps at all.
MNG is basicly just a PNG animation.
The "IDAT" block is almost always compressed with zlib compression.

/e google brought up this: http://www.w3.org/TR/PNG/
December 26, 2008, 12:55 PM
Spilled[DW]
[quote author=l2k-Shadow link=topic=17762.msg180953#msg180953 date=1230277452]
just.. take the header out and dump the rest into a file ending with a .png extention? xD
[/quote]

Ive tried this with StreamWriter class, Let me give BinaryWriter a try and if i run into any problems ill upload some code. Thanks for the help guys appreciate it.

Edit:

Ok I gave BinaryWriter a try and the image still isn't being correctly download. heres my code:

[Code]
                BinaryWriter BW = new BinaryWriter(File.Open("asdf.png", FileMode.Create));
                BW.Write(Response.Substring(Response.IndexOf("\r\n\r\n")+4));
                BW.Flush(); BW.Close();
[/Code]

Response is Header and source, Any ideas?
December 26, 2008, 2:50 PM
Myndfyr
Have you considered using the HttpWebRequest class instead of TcpClient?
December 27, 2008, 11:42 AM
Spilled[DW]
[quote author=MyndFyre[vL] link=topic=17762.msg180965#msg180965 date=1230378120]
Have you considered using the HttpWebRequest class instead of TcpClient?
[/quote]

Yes, but i would like to use TcpClient instead and i have everything done except this part of the project. Do you have any idea as to why this is not working?
December 27, 2008, 11:49 AM
Yegg
Did you Flush() and Close() the stream?

Edit: I'm sending this from my iPhone so things are smaller. Just realized you did do those steps.

What is getting written to the file?
December 27, 2008, 3:26 PM
Quarantine
If you just want to display the image, the Image class in System.Drawing supports being fed a Stream to create the image. You can probably save it after words, but it's probably more performant than saving, loading, and displaying.

Anyhow, you're going to need to strip the HTTP junk out of the stream anyway, so you'll probably want to read into how all that jazz works.
December 28, 2008, 7:54 PM
Yegg
[quote author=Warrior link=topic=17762.msg180972#msg180972 date=1230494050]
If you just want to display the image, the Image class in System.Drawing supports being fed a Stream to create the image. You can probably save it after words, but it's probably more performant than saving, loading, and displaying.

Anyhow, you're going to need to strip the HTTP junk out of the stream anyway, so you'll probably want to read into how all that jazz works.
[/quote]

He did strip out HTTP. He output everything after delimiter (0D 0A 0D 0A) into a file.
December 28, 2008, 9:23 PM
K
I thought for sure someone else would catch this.

When you write a string with the BinaryWriter it is formatted as a length-prefixed string of a specific encoding of text.  In other words, not what you are trying to write.

You need to treat and write the data as an array of bytes.
December 29, 2008, 4:06 AM
Quarantine
I see, it should still be possible to use the solution I described of passing in the actual stream (after it has been stripped of it's header) to the Image object and have it display it, then save the image directly from the object.
December 30, 2008, 4:42 AM

Search