简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM