6

I just want to get a BitmapImage from a internet URL, but my function doesn't seem to work properly, it only return me a small part of the image. I know WebResponse is working async and that's certainly why I have this problem, but how can I do it synchronously?

    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;
    }
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Karnalta
  • 518
  • 1
  • 9
  • 24

4 Answers4

10

First you should just download the image, and store it locally in a temporary file or in a MemoryStream. And then create the BitmapImage object from it.

You can download the image for example like this:

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);
            }
        }
    }
}
treaschf
  • 5,788
  • 1
  • 25
  • 24
  • I don't have much luck with it, my image is still partially downloaded to the MemoryStream, maybe you can provide me a sample code ? – Karnalta Sep 07 '10 at 14:42
  • When reading from the response stream, it won't fill up the buffer, as would be the case when reading from a local file. So the amount of bytes read would be less, than the size of the buffer. However it will be greater than 0, which indicates, that the end of the file has not yet been reached. I suppose this is the key of the failure of reading the image completely from the url. – treaschf Sep 07 '10 at 14:46
  • Ok this sample work to download the picture, I should now be able to convert that file into BitmapImage. – Karnalta Sep 07 '10 at 14:53
2

Why not use 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);
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
mdn
  • 29
  • 1
0

this is the code i use to grab an image from a url....

   // 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 );
       }
    }
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
-3

The simpliest is

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

you can then change BitmapCacheOption to start the retrieval process. However, image is retrieved in async. But you shouldn't care much