2010-09-07 2 views
5

मैं एक साधारण बात करने के लिए कोशिश कर रहा हूँ से एक छवि डाउनलोड लेकिन मैं नहीं कर सकता ...Synchronously यूआरएल

मैं सिर्फ एक इंटरनेट URL से एक BitmapImage प्राप्त करना चाहते हैं, लेकिन मेरे समारोह काम करने के लिए प्रतीत नहीं होता ठीक है, यह केवल मुझे छवि का एक छोटा सा हिस्सा लौटाता है। मुझे पता है कि WebResponse async काम कर रहा है और यह निश्चित रूप से मुझे यह समस्या क्यों है, लेकिन मैं इसे सिंक्रनाइज़ तरीके से कैसे कर सकता हूं?

internal static BitmapImage GetImageFromUrl(string url) 
    { 
     Uri urlUri = new Uri(url); 
     WebRequest webRequest = WebRequest.CreateDefault(urlUri); 
     webRequest.ContentType = "image/jpeg"; 
     WebResponse webResponse = webRequest.GetResponse(); 

     BitmapImage image = new BitmapImage(); 
     image.BeginInit(); 
     image.StreamSource = webResponse.GetResponseStream(); 
     image.EndInit(); 

     return image; 
    } 

आपकी मदद के लिए एक बहुत धन्यवाद:

यहाँ मेरी कार्य है।

उत्तर

10

सबसे पहले आपको केवल छवि डाउनलोड करनी चाहिए, और इसे स्थानीय रूप से अस्थायी फ़ाइल में या MemoryStream में स्टोर करना चाहिए। और उसके बाद BitmapImage ऑब्जेक्ट बनाएं।

आप इस तरह के उदाहरण के लिए छवि डाउनलोड कर सकते हैं:

Uri urlUri = new Uri(url); 
var request = WebRequest.CreateDefault(urlUri); 

byte[] buffer = new byte[4096]; 

using (var target = new FileStream(targetFileName, FileMode.Create, FileAccess.Write)) 
{ 
    using (var response = request.GetResponse()) 
    {  
     using (var stream = response.GetResponseStream()) 
     { 
      int read; 

      while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) 
      { 
       target.Write(buffer, 0, read); 
      } 
     } 
    } 
} 
+0

मुझे इसके साथ बहुत भाग्य नहीं है, मेरी छवि अभी भी आंशिक रूप से मेमोरीस्ट्रीम पर डाउनलोड की गई है, शायद आप मुझे नमूना कोड प्रदान कर सकते हैं? – Karnalta

+0

प्रतिक्रिया स्ट्रीम से पढ़ने पर, यह बफर को भर नहीं पाएगा, जैसा स्थानीय फ़ाइल से पढ़ते समय होगा। तो बफर के आकार की तुलना में पढ़ने वाले बाइट्स की मात्रा कम होगी। हालांकि यह 0 से अधिक होगा, जो इंगित करता है कि फ़ाइल का अंत अभी तक नहीं पहुंचा है। मुझे लगता है कि यह यूआरएल से पूरी तरह से छवि पढ़ने की विफलता की कुंजी है। – treaschf

+0

तस्वीर को डाउनलोड करने के लिए यह नमूना काम ठीक है, अब मुझे उस फ़ाइल को बिटमैप इमेज में कनवर्ट करने में सक्षम होना चाहिए। – Karnalta

0

इस कोड को मैं एक यूआरएल से एक छवि को आकर्षित करने के लिए उपयोग ....

// get a stream of the image from the webclient 
    using (Stream stream = webClient.OpenRead(imgeUri)) 
    { 
     // make a new bmp using the stream 
     using (Bitmap bitmap = new Bitmap(stream)) 
     { 
      //flush and close the stream 
      stream.Flush(); 
      stream.Close(); 
      // write the bmp out to disk 
      bitmap.Save(saveto); 
     } 
    } 
1

क्यों System.Net.WebClient.DownloadFile का उपयोग नहीं कर रहा है?

string url = @"http://www.google.ru/images/srpr/logo3w.png"; 
string file = System.IO.Path.GetFileName(url); 
System.Net.WebClient cln = new System.Net.WebClient(); 
cln.DownloadFile(url,file); 
-3

simpliest

Uri pictureUri = new Uri(pictureUrl); 
BitmapImage image = new BitmapImage(pictureUri); 

है आप तो बहाली प्रक्रिया शुरू करने के लिए BitmapCacheOption बदल सकते हैं। हालांकि, छवि async में पुनर्प्राप्त किया गया है। लेकिन आपको अधिक ध्यान नहीं देना चाहिए