views:

117

answers:

1

Hi All,

I am trying to download images from the site. The code which i am using is working fine till the image is available. If the image it not available it is creating problem. How to validate availability of the image.

code:

Method 1:

WebRequest requestPic = WebRequest.Create(imageUrl);

WebResponse responsePic = requestPic.GetResponse();

Image webImage = Image.FromStream(responsePic.GetResponseStream()); // Error

webImage.Save("D:\\Images\\Book\\" + fileName + ".jpg");

Method 2:

 WebClient client = new WebClient();
            Stream stream = client.OpenRead(imageUrl);

            bitmap = new Bitmap(stream); // Error : Parameter is not valid.
            stream.Flush();
            stream.Close();
            client.dispose();

            if (bitmap != null)
            {
                bitmap.Save("D:\\Images\\" + fileName + ".jpg");
            }

Edit:

Stream has the following statements:

      Length    '((System.Net.ConnectStream)(str)).Length' threw an exception of type  'System.NotSupportedException'   long {System.NotSupportedException}
    Position    '((System.Net.ConnectStream)(str)).Position' threw an exception of type 'System.NotSupportedException'  long {System.NotSupportedException}
 ReadTimeout    300000  int
WriteTimeout    300000  int
+6  A: 

There is no need to involve any image classes, you can simply call WebClient.DownloadFile:

string localFilename = @"c:\localpath\tofile.jpg";
using(WebClient client = new WebClient())
{
    client.DownloadFile("http://www.example.com/image.jpg", localFilename);
}

Update
Since you will want to check whether the file exists and download the file if it does, it's better to do this within the same request. So here is a method that will do that:

private static void DownloadRemoteImageFile(string uri, string fileName)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    // Check that the remote file was found. The ContentType
    // check is performed since a request for a non-existent
    // image file might be redirected to a 404-page, which would
    // yield the StatusCode "OK", even though the image was not
    // found.
    if ((response.StatusCode == HttpStatusCode.OK || 
        response.StatusCode == HttpStatusCode.Moved || 
        response.StatusCode == HttpStatusCode.Redirect) &&
        response.ContentType.StartsWith("image",StringComparison.OrdinalIgnoreCase))
    {

        // if the remote file was found, download oit
        using (Stream inputStream = response.GetResponseStream())
        using (Stream outputStream = File.OpenWrite(fileName))
        {
            byte[] buffer = new byte[4096];
            int bytesRead;
            do
            {
                bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                outputStream.Write(buffer, 0, bytesRead);
            } while (bytesRead != 0);
        }
    }
}

In brief, it makes a request for the file, verifies that the response code is one of OK, Moved or Redirect and also that the ContentType is an image. If those conditions are true, the file is downloaded.

Fredrik Mörk
Don't forget to dispose the `WebClient`.
Darin Dimitrov
@Darin: thanks. Fixed that.
Fredrik Mörk
@Geetha: do you get an image if you try to navigate to the given URL in a web browser?
Fredrik Mörk
@Geetha: it sounds as if you would first want to check if the image exists, then download it. For that first step, check here: http://stackoverflow.com/questions/1379371/c-is-there-a-faster-way-to-check-if-an-external-web-page-exists
Fredrik Mörk
It is taking too much of time and also downloading blank image. the line bitmap = new Bitmap(stream); shows error: Parameter is not valid
Geetha
@Geetha: see updated answer.
Fredrik Mörk