Image from URL to stream

Cheese picture Cheese · Jul 26, 2013 · Viewed 55.5k times · Source

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?

Answer

Ashok Damani picture Ashok Damani · Jul 26, 2013
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData(article.ImageURL);