Image from URL to stream
Asked Answered
B

3

20

I'm getting images from a url:

BitmapImage image = new BitmapImage(new Uri(article.ImageURL));
NLBI.Thumbnail.Source = image;

This works perfect, now i need to put it in a stream, to make it into byte array. I'm doing this:

WriteableBitmap wb = new WriteableBitmap(image);
MemoryStream ms = new MemoryStream();
wb.SaveJpeg(ms, image.PixelWidth, image.PixelHeight, 0, 100);
byte[] imageBytes = ms.ToArray();

And code fails with NullReference, how to fix it?

Brigettebrigg answered 26/7, 2013 at 8:0 Comment(0)
F
16

You get a NullReference exception because the image is still not loaded when you use it. You can wait to the ImageOpened event, and then work with it:

var image = new BitmapImage(new Uri(article.ImageURL));               
image.ImageOpened += (s, e) =>
    {
        image.CreateOptions = BitmapCreateOptions.None;
        WriteableBitmap wb = new WriteableBitmap(image);
        MemoryStream ms = new MemoryStream();
        wb.SaveJpeg(ms, image.PixelWidth, image.PixelHeight, 0, 100);
        byte[] imageBytes = ms.ToArray();
    };
NLBI.Thumbnail.Source = image;

Other option is to get the stream of the image file directly using WebClient:

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));
Flattop answered 26/7, 2013 at 8:42 Comment(2)
You should go for the second option. Download the image buffer by means of a WebClient as shown above, then write the stream to a byte array and directly create the BitmapImage from the same stream by calling BitmapImage.SetSource.Allhallows
I have completed the answer with suggestion from @AllhallowsFlattop
C
40
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData(article.ImageURL);
Conni answered 26/7, 2013 at 8:10 Comment(3)
WebClient.DownloadData is not available in Silverlight.Allhallows
Thanks , but i get The remote server returned an error: (404) Not Found.Hallock
Worked for me in c# mvc with .net 4.5.1Leodora
F
16

You get a NullReference exception because the image is still not loaded when you use it. You can wait to the ImageOpened event, and then work with it:

var image = new BitmapImage(new Uri(article.ImageURL));               
image.ImageOpened += (s, e) =>
    {
        image.CreateOptions = BitmapCreateOptions.None;
        WriteableBitmap wb = new WriteableBitmap(image);
        MemoryStream ms = new MemoryStream();
        wb.SaveJpeg(ms, image.PixelWidth, image.PixelHeight, 0, 100);
        byte[] imageBytes = ms.ToArray();
    };
NLBI.Thumbnail.Source = image;

Other option is to get the stream of the image file directly using WebClient:

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));
Flattop answered 26/7, 2013 at 8:42 Comment(2)
You should go for the second option. Download the image buffer by means of a WebClient as shown above, then write the stream to a byte array and directly create the BitmapImage from the same stream by calling BitmapImage.SetSource.Allhallows
I have completed the answer with suggestion from @AllhallowsFlattop
D
2

you can use this:

    private async Task<byte[]> GetImageAsByteArray(string urlImage, string urlBase)
    {

        var client = new HttpClient();
        client.BaseAddress = new Uri(urlBase);
        var response = await client.GetAsync(urlImage);

        return await response.Content.ReadAsByteArrayAsync();
    }
Disadvantaged answered 25/1, 2018 at 18:39 Comment(1)
i think when i asked the question there was no such thing as async in windows phone 7 :)Brigettebrigg

© 2022 - 2024 — McMap. All rights reserved.