473,320 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1988
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sam Felsenfeld | last post by:
Thanks in advance to anyone who can help me ... I have a link on a page that allows people to automatically download a jpeg. The link basically goes to "download.jpg?pic=filename" Here's what I...
13
by: Ken | last post by:
How do I download files from the internet server to my computer? How do I tell the script the correct hard drive folder on my computer to copy the files to? After connecting to the internet...
3
by: GMane Python | last post by:
Hello All. Using a network camera with built-in webserver, I'd like to have a python program download .jpg files on a local lan. the location is http://<ip-address>/jpg/image.jpg. Currently,...
0
by: Michael | last post by:
I have a problem forcing files to download. If I select Save the document is saved with no problems. If I select "Open" the document is empty or I get a "File not found" error from the application...
5
by: Thomas Andersson | last post by:
Hi, I am trying to find a working solution for download of large files (400-800 MB)... But this seems almost impossible to find a working example. I have tried Response.Transmitfile, this...
6
by: fgarciarico | last post by:
I´m developping an application that needs to show some videos, but in a protected envinroment. Any user must be authenticated to see the videos. But for example, if anyone know the path of the...
17
by: Kevin Goodsell | last post by:
Is there a place I can download the last public draft for C89? What about a draft for C95? What I really need is a list of all standard library functions, macros, types, etc. for a "keyword...
5
by: Baren | last post by:
Hi! I am using impersonate="true" to upload and download files from a network share. I have created common users on both the webserver and the file server. The user has permission to the...
12
by: comp.lang.php | last post by:
index.php: // STUFF // STEP 1: imagecreatetruecolor ONLY IF GD 2.0+ SUPPORTED AND FOUND if ($this->isSuccessful && !$hasMogrified && $image && !$newImage &&...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.