2010-09-01 14 views
58

मैं साइट से छवियों को डाउनलोड करने का प्रयास कर रहा हूं। जिस कोड का मैं उपयोग कर रहा हूं वह ठीक काम कर रहा है जबकि छवि उपलब्ध है। अगर यह छवि उपलब्ध नहीं है तो यह एक समस्या पैदा कर रही है। छवि की उपलब्धता को कैसे सत्यापित करें?साइट से छवि डाउनलोड करें .NET/C#

कोड:

विधि 1:

WebRequest requestPic = WebRequest.Create(imageUrl); 

WebResponse responsePic = requestPic.GetResponse(); 

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

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

विधि 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"); 
} 

संपादित करें:

स्ट्रीम है निम्नलिखित बयानों:

+०१२३५१६४१०
 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 
+0

लपेटें अपमानजनक बयान का उपयोग करने के लिए 'कोशिश - catch', और अपवाद विवरण उपलब्ध कराएं। – gimel

+0

लाइन बिटमैप = नया बिटमैप (स्ट्रीम); त्रुटि दिखाता है: पैरामीटर वैध नहीं है। – Geeth

उत्तर

153

किसी भी छवि वर्गों को शामिल करने की कोई जरूरत नहीं है, तो आप बस WebClient.DownloadFile कॉल कर सकते हैं:

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

अद्यतन
जब से तुम चाहे फ़ाइल मौजूद जाँच करना चाहते हैं जाएगा और फाइल डाउनलोड यदि ऐसा होता है, तो यह वही अनुरोध के भीतर करना बेहतर है।

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); 
     } 
    } 
} 

संक्षेप में, यह फ़ाइल के लिए अनुरोध करता है, पुष्टि करता है कि प्रतिक्रिया कोड OK, Moved या Redirectमें से एक है और यह भी है कि ContentType एक है: तो यहाँ एक विधि है कि ऐसा करने में करता है छवि। यदि वे शर्तें सत्य हैं, तो फ़ाइल डाउनलोड की जाती है।

+4

'वेब क्लाइंट' का निपटान करना न भूलें। –

+0

@ डारिन: धन्यवाद। निश्चित है कि। –

+0

@ गीता: यदि आप किसी वेब ब्राउज़र में दिए गए यूआरएल पर नेविगेट करने का प्रयास करते हैं तो क्या आपको एक छवि मिलती है? –

24

मैं कुछ मामूली परिवर्तनों के साथ एक परियोजना में ऊपर फ्रेड्रिक के कोड का इस्तेमाल किया है, सोचा था कि मैं हिस्सा चाहते हैं:

private static bool DownloadRemoteImageFile(string uri, string fileName) 
{ 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
    HttpWebResponse response; 
    try 
    { 
     response = (HttpWebResponse)request.GetResponse(); 
    } 
    catch (Exception) 
    { 
     return false; 
    } 

    // 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 it 
     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); 
     } 
     return true; 
    } 
    else 
     return false; 
} 

मुख्य परिवर्तन कर रहे हैं:

  • GetResponse के लिए आज़माएं/कैच का उपयोग कर () के रूप में मैं एक अपवाद में चल रहा था जब दूरस्थ फ़ाइल लौटे 404
  • एक बूलियन
+1

धन्यवाद - अच्छी तरह लिखित कोड की तरह दिखता है और मेरे आवेदन में अच्छी तरह से काम करता है – bernhardrusch

+0

जब आप अपवाद फेंक सकते हैं तो बुलियन ध्वज क्यों लौटाते हैं? जब तक आपके पास प्रदर्शन की समस्या न हो तब तक यह हमेशा बेहतर होता है। एसआरपी इन तरीकों की अनुमति नहीं देता है। –

0
012 लौटने
+0

शायद आप इस कोड को समझा सकते हैं, और _why_ यह काम करता है? –

2

भी संभव साथ DownloadData विधि

private byte[] GetImage(string iconPath) 
    { 
     using (WebClient client = new WebClient()) 
     { 
      byte[] pic = client.DownloadData(iconPath); 
      //string checkPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) [email protected]"\1.png"; 
      //File.WriteAllBytes(checkPath, pic); 
      return pic; 
     } 
    } 
संबंधित मुद्दे