471,066 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

download JPEG

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

Feb 15 '07 #1
7 1913
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).

---
Patrice

<bb****@op.pla écrit dans le message de news:
11**********************@a75g2000cwd.googlegroups. com...
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

Feb 15 '07 #2
On Feb 15, 1:39 pm, "Patrice" <http://www.chez.com/scribe/wrote:
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.

Feb 15 '07 #3
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 ).

--
Patrice

<bb****@op.pla écrit dans le message de news:
11**********************@p10g2000cwp.googlegroups. com...
On Feb 15, 1:39 pm, "Patrice" <http://www.chez.com/scribe/wrote:
>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.

Feb 15 '07 #4
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)

bb****@op.pl wrote:
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
Feb 15 '07 #5
On Feb 15, 5:54 pm, bruce barker <nos...@nospam.comwrote:
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.

Feb 15 '07 #6
sorry i though you sending data. your problem is using a stream
reader/writer, use a binary reader/writer.
-- bruce (sqlwork.com)
bb****@op.pl wrote:
On Feb 15, 5:54 pm, bruce barker <nos...@nospam.comwrote:
>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.
Feb 15 '07 #7
On Feb 15, 11:05 pm, bruce barker <nos...@nospam.comwrote:
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?

Feb 15 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Sam Felsenfeld | last post: by
13 posts views Thread by Ken | last post: by
3 posts views Thread by GMane Python | last post: by
6 posts views Thread by fgarciarico | last post: by
17 posts views Thread by Kevin Goodsell | last post: by
5 posts views Thread by Baren | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.