elodie
elodie

Reputation: 11

WP8 C# // Save image from web, in isolate storage

I know how to save an image in isolate storage using the following :

 private void addButton_Click(object sender, RoutedEventArgs e)
        {
            MemoryStream stream = new MemoryStream();
            WriteableBitmap wb = new WriteableBitmap(myImage, null);
            BitmapImage bi = new BitmapImage();

            wb.SaveJpeg(stream, wb.PixelWidth, wb.PixelHeight, 0, 100);
            stream.Seek(0, SeekOrigin.Begin);
            string data = Convert.ToBase64String(stream.GetBuffer());

            appSettings.Add("image", data);
        }

I know how to load it using the following :

private void loadImage_Click(object sender, RoutedEventArgs e)
    {
        byte[] imageBytes = Convert.FromBase64String(appSettings["image"].ToString());

        MemoryStream ms = new MemoryStream(imageBytes);
        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.SetSource(ms);
        myImage.Source = bitmapImage;
    }

But I don't know how to load and read it from a URL, how can this be accomplished?

Thx for your help.

Upvotes: 0

Views: 280

Answers (1)

Pseudonym
Pseudonym

Reputation: 2072

From this Image from URL to stream:

WebClient client = new WebClient();
client.OpenReadCompleted += (s, e) =>
     {
         byte[] imageBytes = new byte[e.Result.Length];
         e.Result.Read(imageBytes, 0, imageBytes.Length);

         // Now you can use the returned stream to set the image source too
         var image = new BitmapImage();
         image.SetSource(e.Result);
         NLBI.Thumbnail.Source = image;
     };
client.OpenReadAsync(new Uri(article.ImageURL));

Edit: here is some more info on OpenReadComplete(MSDN) and how to use it

Upvotes: 1

Related Questions