Saving TIF Image from the Internet

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an app that (at the moment) moves through files that are on a web
site, and deletes them, wat i want to do for the next stage, is to be able to
download each file before i delete it.

i have tried POSTin to be able to view the image, then saving it, but it
doesnt work, the problem i have is that all the files on the server have the
same name, which is
http://1.1.1.1/DH/repository/content.tif

and for me to be able to view one, i had to send the follow command (that
has a fwe ids that tell me the server which file i want)

show=&did=31374&rid=6_2_6_1_2_1&sid=174709756905990&WidthSize=136&useSavedParam=&dummy=

once i did this i got the followin content response

HTTP/1.1 200 OK
Server: Web-Server/3.0 GW-Repository
Content-Length: 24434
Content-Type: image/x-tiff
Set-Cookie: cookieOnOffChecker=on; path=/
Connection: close

MM

i have tried to use the
WebClient.DownloadData(http://1.1.1.1/DH/repository/content.tif)

but it simply returns an error.

Does enyone have any ideas.


Thanx in advance
 
Thaynann,

What is the error that you are getting? You should be able to set the
query string using the web client as well, and get the correct response, for
you to save.

Hope this helps.
 
There is no specific message with the exception...just says "Error has
occured in WebClient", i tried grabbing the InnerException, and the
staceTrace, but no actual message with exception, just above.

i have another idea as to how to save it, that is to create a file (probably
with a FileStream) then simply write the response bytes straight to it. im
hopin this will work. ill keep u posted



Nicholas Paldino said:
Thaynann,

What is the error that you are getting? You should be able to set the
query string using the web client as well, and get the correct response, for
you to save.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thaynann said:
I have an app that (at the moment) moves through files that are on a web
site, and deletes them, wat i want to do for the next stage, is to be able
to
download each file before i delete it.

i have tried POSTin to be able to view the image, then saving it, but it
doesnt work, the problem i have is that all the files on the server have
the
same name, which is
http://1.1.1.1/DH/repository/content.tif

and for me to be able to view one, i had to send the follow command (that
has a fwe ids that tell me the server which file i want)

show=&did=31374&rid=6_2_6_1_2_1&sid=174709756905990&WidthSize=136&useSavedParam=&dummy=

once i did this i got the followin content response

HTTP/1.1 200 OK
Server: Web-Server/3.0 GW-Repository
Content-Length: 24434
Content-Type: image/x-tiff
Set-Cookie: cookieOnOffChecker=on; path=/
Connection: close

MM

i have tried to use the
WebClient.DownloadData(http://1.1.1.1/DH/repository/content.tif)

but it simply returns an error.

Does enyone have any ideas.


Thanx in advance
 
Nicholas,

i have managed to get the image to save, by using the following code

Stream stream = webResponse.GetResponseStream();
Stream imageFile = File.Create(@"C:\IMAGES\image.tif");
Bitmap b = new Bitmap(stream);
b.Save(imageFile, ImageFormat.Tiff);

which works fine, the only problem is that, most of the fils im tryin to
save off teh web page are multi-page Tif files, and using the code above, it
seems to be only saving the first page,

i thought maybe the content returned from the server maybe only for the
first page, but i ran the web page via IE (with Fiddler running) and the
commands are identical to wat i have to view the image and IE lets me move
through the multiple pages of the image,

Any ideas?
Thanx for your help so far.


Nicholas Paldino said:
Thaynann,

What is the error that you are getting? You should be able to set the
query string using the web client as well, and get the correct response, for
you to save.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thaynann said:
I have an app that (at the moment) moves through files that are on a web
site, and deletes them, wat i want to do for the next stage, is to be able
to
download each file before i delete it.

i have tried POSTin to be able to view the image, then saving it, but it
doesnt work, the problem i have is that all the files on the server have
the
same name, which is
http://1.1.1.1/DH/repository/content.tif

and for me to be able to view one, i had to send the follow command (that
has a fwe ids that tell me the server which file i want)

show=&did=31374&rid=6_2_6_1_2_1&sid=174709756905990&WidthSize=136&useSavedParam=&dummy=

once i did this i got the followin content response

HTTP/1.1 200 OK
Server: Web-Server/3.0 GW-Repository
Content-Length: 24434
Content-Type: image/x-tiff
Set-Cookie: cookieOnOffChecker=on; path=/
Connection: close

MM

i have tried to use the
WebClient.DownloadData(http://1.1.1.1/DH/repository/content.tif)

but it simply returns an error.

Does enyone have any ideas.


Thanx in advance
 
Thaynann said:
Nicholas,

i have managed to get the image to save, by using the following code

Stream stream = webResponse.GetResponseStream();
Stream imageFile = File.Create(@"C:\IMAGES\image.tif");
Bitmap b = new Bitmap(stream);
b.Save(imageFile, ImageFormat.Tiff);

You can dump the bytes read from the response stream directly to your
destination file. This can be refactored into a neat helper method.

public void Download(string url, string fileName)
{
// Add error handling for null params and other evil
// stuff on your own ;-)
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();

using (Stream responseStream = response.GetResponseStream())
using (FileStream fileStream =
new FileStream(fileName, FileMode.Create))
{
Copy(responseStream, fileStream);
}
}

public void Copy(Stream source, Stream target)
{
// Add error handling for null params and other evil
// stuff on your own ;-)
byte[] buffer = new byte[0x1000];
int bytes;
try
{
while ((bytes = source.Read(buffer, 0, buffer.Length)) > 0)
{
target.Write(buffer, 0, bytes);
}
}
finally
{
target.Flush();
}
}

Note that there's also Webclient.DownloadFile(), which wraps these
tasks nicey ;-)

cheers,
 
Thanx for the code, i managed to get it working, with the code u gave me, i
tried to use the webClient.DownloadFile(), but i couldnt, i didnt have diret
access to the URL of the images, as all the Images i was getting had the
exact same URL, the only way to distinguish between each image was via IDs
that the server return when i posted requests.

thanx again for ur input, the app works perfectly, it was deployed yesterday

Joerg Jooss said:
Thaynann said:
Nicholas,

i have managed to get the image to save, by using the following code

Stream stream = webResponse.GetResponseStream();
Stream imageFile = File.Create(@"C:\IMAGES\image.tif");
Bitmap b = new Bitmap(stream);
b.Save(imageFile, ImageFormat.Tiff);

You can dump the bytes read from the response stream directly to your
destination file. This can be refactored into a neat helper method.

public void Download(string url, string fileName)
{
// Add error handling for null params and other evil
// stuff on your own ;-)
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();

using (Stream responseStream = response.GetResponseStream())
using (FileStream fileStream =
new FileStream(fileName, FileMode.Create))
{
Copy(responseStream, fileStream);
}
}

public void Copy(Stream source, Stream target)
{
// Add error handling for null params and other evil
// stuff on your own ;-)
byte[] buffer = new byte[0x1000];
int bytes;
try
{
while ((bytes = source.Read(buffer, 0, buffer.Length)) > 0)
{
target.Write(buffer, 0, bytes);
}
}
finally
{
target.Flush();
}
}

Note that there's also Webclient.DownloadFile(), which wraps these
tasks nicey ;-)

cheers,
 

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

Back
Top