title: Download image from the site in .NET/C#
body: <p>I am trying to download images from the site. The code which I am using is working fine while the image is available. If the image it not available it is creating a problem. How to validate availability of the image?</p>
  
  <p>Code:</p>
  
  <p>Method 1:</p>
  
  <pre><code>WebRequest requestPic = WebRequest.Create(imageUrl);
  
  WebResponse responsePic = requestPic.GetResponse();
  
  Image webImage = Image.FromStream(responsePic.GetResponseStream()); // Error
  
  webImage.Save("D:\\Images\\Book\\" + fileName + ".jpg");
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code>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");
  }
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>Stream has the following statements:</p>
  
  <pre><code>      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
  </code></pre>