download JPEG

B

bbla32

How to downloaded a JPEG image from server? I use the collowing
statements:

using (Stream stream = web.OpenRead(link)) {
long len = -1;
bool ok = true;
string s = web.ResponseHeaders["Content-Type"];
if (s != null && s != "image/jpeg")
ok = false;
s = web.ResponseHeaders["Content-Length"];
if (s != null)
len = int.Parse(s);
if (ok && (len == -1 || len > 4)) {
using (StreamReader sr = new StreamReader(stream)) {
int HEADER = 6;
sr.Read(buffer, 4, HEADER);
1 >>>
//if (buffer[0] == 0xFF &&
// buffer[1] == 0xD8 &&
// buffer[2] == 0xFF &&
// buffer[3] == 0xE0) {
if (buffer[4] == 0x00 &&
buffer[5] == 0x10 &&
buffer[6] == 0x4A &&
buffer[7] == 0x46 &&
buffer[8] == 0x49 &&
buffer[9] == 0x46) {

// download
HEADER += 4;
2 >>>
buffer[0] = (char) 0xFF;
buffer[1] = (char) 0xD8;
buffer[2] = (char) 0xFF;
buffer[3] = (char) 0xE0;
int dloaded = sr.Read(buffer, HEADER, bufferSize -
HEADER);
dloaded += HEADER;
using (StreamWriter sw = new StreamWriter(fname)) {
while (true) {
sw.Write(buffer, 0, dloaded);
if (dloaded != bufferSize) break;
dloaded = sr.Read(buffer, 0, bufferSize);
}
sw.Close();
}
....


The response stream does not seem to contain (1) the Start Of Image
(FF D8) and JFIF (FF E0) markers! So I cannot save the stream directly
to disk. That's what's the (2) hack for, but it does not write the
given bytes, for example instead of FF it writes C3 BF. I guess it's
due to Unicode but I don't know how to make it work. sw.Write does not
support writing bytes.

Similarly, the stream does not contain End Of Image (FF D9) marker.
What else is it missing?

Lukasz
 
P

Patrice

What are you trying to do ? It looks like you try to read a file to stream
it to a browser. You could likely use Response.WriteFile instead to stream
the file (or do you try to steal images from another website ???)

Similarly you have higher level capabilites such as
System.Net.WebClient.DowloadFile that should do the job for you instead of
having to deal wiht low level details (at least first)...

Generally my approach in such cases is to try with a known file and issue
then a "fc" command to see how the downloaded filed differ from my
"reference" file (first in size and then in content if they have the same
size).
 
B

bbla32

What are you trying to do ? It looks like you try to read a file to stream
it to a browser. You could likely use Response.WriteFile instead to stream
the file (or do you try to steal images from another website ???)

Similarly you have higher level capabilites such as
System.Net.WebClient.DowloadFile that should do the job for you instead of
having to deal wiht low level details (at least first)...


I've used WebClient.DownloadFile, but I want to show progress of
downloading.
 
P

Patrice

It is read from another site that is not under your control ? And you don't
want your users to know that the image doesn't come from your site ?

I would realy start fresh on this one possibly by donwloading a simple text
file to see what happens and without messing with buffers. Also remove first
your hack and add them back only if you are sure that is a normal behavior
(it would be very surprising to find out that those markers are removed ).
 
B

bruce barker

the page content is also being downloaded. call Request.ClearContent()
before sending data, then Response.End() to prevent extra data being sent.

-- bruce (sqlwork.com)
 
B

bbla32

the page content is also being downloaded. call Request.ClearContent()
before sending data, then Response.End() to prevent extra data being sent.


Errr.. it's a Win Forms application, I'm using WebClient class and its
OpenRead method.
 
B

bruce barker

sorry i though you sending data. your problem is using a stream
reader/writer, use a binary reader/writer.


-- bruce (sqlwork.com)
 
B

bbla32

sorry i though you sending data. your problem is using a stream
reader/writer, use a binary reader/writer.


Good point! Using BinaryReader and BinaryWriter does the job.

By the way I noticed using

while (true) {
sw.Write(buffer, 0, dloaded);
if (dloaded != bufferSize) break;
dloaded = sr.Read(buffer, 0, bufferSize);
}

stops before whole image is downloaded. How can I correctly determine
when I read all in case there is no Content-Length header?
 

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