HttpWebRequest to save images

L

Logician

I am running on my PC Windows Forms to collect data from websites,
including images.

I hit a problem with images and javascript, and I would appreciate any
help.

The current code fails with a copy error. My Internet connected PC is
not the development machine, so I cannot debug on that PC and my other
PC has no Internet connection. So I cannot debug using .NET as normal.


My Code:
private string copyWebImage(string url, string TargetFile)
{
// add error handling
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.Timeout=5000;
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader myTextReader = new StreamReader(responseStream);
char[] strBuffer = new char[25];
myTextReader.ReadBlock(strBuffer,0,25);
string stringBuffer = new string(strBuffer);
//get image name from tiff or jpg or gif or jpeg
// insert name into database
// store image on pc as standard name

if (stringBuffer.IndexOf("GIF8")>-1 ||
stringBuffer.IndexOf("JFIF")>-1)
{

// image found
//Image thisImage = Image.FromStream(responseStream);
//thisImage.Save(res
//thisImage.
//Stream ToStream = File.Create(TargetFile);
//
thisImage.Save(ToStream,System.Drawing.Imaging.ImageFormat.Gif);
//BinaryReader br = new BinaryReader(responseStream);
byte[] b;
using (BinaryReader br = new BinaryReader(responseStream))
{
b= br.ReadBytes((int)responseStream.Length);
br.Close();
}
FileStream fs= new FileStream(TargetFile,FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
w.Write(b);

// BinaryWriter bw = new BinaryWriter(ToStream);
// bw.Write(br.ReadBytes((int)responseStream.Length));
// bw.Flush();
// bw.Close();
//ToStream.Close();
// br.Close();

}
else
{
// this is a text page
}


}
catch (WebException e)
{
return "image copy failed error "+e.ToString();
}
catch (Exception e)
{
return "image copy failed error "+e.ToString();
}

return "";
}
 
N

Nicholas Paldino [.NET/C# MVP]

Locician,

I don't see why you are doing all this stuff to get the index of GIF8 or
JFIF. I know you are trying to get the header of the image to determine
what type it is so that you can put it into an Image instance and then save
that, but I don't see why you just don't save the contents to disk directly,
since that is what you are doing in the end?
 
G

Guest

As Nick indicated, all you need to do is issue the WebClient.DownloadData
(urlOfImage) method and save the resultant byte[] array to disk with the name
of the image. Essentially, two lines of code.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com



Logician said:
I am running on my PC Windows Forms to collect data from websites,
including images.

I hit a problem with images and javascript, and I would appreciate any
help.

The current code fails with a copy error. My Internet connected PC is
not the development machine, so I cannot debug on that PC and my other
PC has no Internet connection. So I cannot debug using .NET as normal.


My Code:
private string copyWebImage(string url, string TargetFile)
{
// add error handling
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.Timeout=5000;
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader myTextReader = new StreamReader(responseStream);
char[] strBuffer = new char[25];
myTextReader.ReadBlock(strBuffer,0,25);
string stringBuffer = new string(strBuffer);
//get image name from tiff or jpg or gif or jpeg
// insert name into database
// store image on pc as standard name

if (stringBuffer.IndexOf("GIF8")>-1 ||
stringBuffer.IndexOf("JFIF")>-1)
{

// image found
//Image thisImage = Image.FromStream(responseStream);
//thisImage.Save(res
//thisImage.
//Stream ToStream = File.Create(TargetFile);
//
thisImage.Save(ToStream,System.Drawing.Imaging.ImageFormat.Gif);
//BinaryReader br = new BinaryReader(responseStream);
byte[] b;
using (BinaryReader br = new BinaryReader(responseStream))
{
b= br.ReadBytes((int)responseStream.Length);
br.Close();
}
FileStream fs= new FileStream(TargetFile,FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
w.Write(b);

// BinaryWriter bw = new BinaryWriter(ToStream);
// bw.Write(br.ReadBytes((int)responseStream.Length));
// bw.Flush();
// bw.Close();
//ToStream.Close();
// br.Close();

}
else
{
// this is a text page
}


}
catch (WebException e)
{
return "image copy failed error "+e.ToString();
}
catch (Exception e)
{
return "image copy failed error "+e.ToString();
}

return "";
}
 
L

Logician

As Nick indicated, all you need to do is issue the WebClient.DownloadData
(urlOfImage) method and save the resultant byte[] array to disk with the name
of the image. Essentially, two lines of code.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com



Logician said:
I am running on my PC Windows Forms to collect data from websites,
including images.
I hit a problem with images and javascript, and I would appreciate any
help.
The current code fails with a copy error. My Internet connected PC is
not the development machine, so I cannot debug on that PC and my other
PC has no Internet connection. So I cannot debug using .NET as normal.
My Code:
private string copyWebImage(string url, string TargetFile)
{
// add error handling
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout=5000;
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader myTextReader = new StreamReader(responseStream);
char[] strBuffer = new char[25];
myTextReader.ReadBlock(strBuffer,0,25);
string stringBuffer = new string(strBuffer);
//get image name from tiff or jpg or gif or jpeg
// insert name into database
// store image on pc as standard name
if (stringBuffer.IndexOf("GIF8")>-1 ||
stringBuffer.IndexOf("JFIF")>-1)
{
// image found
//Image thisImage = Image.FromStream(responseStream);
//thisImage.Save(res
//thisImage.
//Stream ToStream = File.Create(TargetFile);
//
thisImage.Save(ToStream,System.Drawing.Imaging.ImageFormat.Gif);
//BinaryReader br = new BinaryReader(responseStream);
byte[] b;
using (BinaryReader br = new BinaryReader(responseStream))
{
b= br.ReadBytes((int)responseStream.Length);
br.Close();
}
FileStream fs= new FileStream(TargetFile,FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
w.Write(b);
// BinaryWriter bw = new BinaryWriter(ToStream);
// bw.Write(br.ReadBytes((int)responseStream.Length));
// bw.Flush();
// bw.Close();
//ToStream.Close();
// br.Close();
}
else
{
// this is a text page
}
}
catch (WebException e)
{
return "image copy failed error "+e.ToString();
}
catch (Exception e)
{
return "image copy failed error "+e.ToString();
}
return "";
}- Hide quoted text -

- Show quoted text -

It works with DownloadFile(url,imageName). Thanks for the suggestion
about using WebClient.

I want to run this under UNIX and also implement Javascript link
following which is common on many websites.

Can C# work well under UNIX? Do you have experience of using WebClient
with .js based links?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Logician said:
It works with DownloadFile(url,imageName). Thanks for the suggestion
about using WebClient.

I want to run this under UNIX and also implement Javascript link
following which is common on many websites.

Can C# work well under UNIX? Do you have experience of using WebClient
with .js based links?

I would expect both (Http)WebRequest and WebClient to work with
Mono on Unix (assuming Mono is available for your flavor of Unix).

The code will not interpret JavaScript.

Maybe you can do that by embedding a browser in your app. But that
will not work on Unix.

Arne
 

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