If you have the Uri then you can use RandomAccessStreamReference.CreateFromUri to create a stream (valid chemes are http, https, ms-appx, and ms-appdata, docs here), and read from the stream to create the WriteableBitmap.
Code sample:
private async Task<WriteableBitmap> UriToWriteableBitmap(Uri uri)
{
var randomAccessStreamReference = RandomAccessStreamReference.CreateFromUri(uri);
using (IRandomAccessStreamWithContentType stream = await randomAccessStreamReference.OpenReadAsync())
{
return await StreamToWriteableBitmap(stream);
}
}
private static async Task<WriteableBitmap> StreamToWriteableBitmap(IRandomAccessStream stream)
{
var decoder = await BitmapDecoder.CreateAsync(stream);
var writeableBitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
stream.Seek(0);
await writeableBitmap.SetSourceAsync(stream);
return writeableBitmap;
}
You can use method StreamToWriteableBitmap above with StorageFile too:
using (IRandomAccessStreamWithContentType stream = await storageFile.OpenReadAsync())
{
WriteableBitmap streamToWriteableBitmap = await StreamToWriteableBitmap(stream);
}
Here below another implementation of method StreamToWriteableBitmap that uses SoftwareBitmap class
private static async Task<WriteableBitmap> StreamToWriteableBitmap(IRandomAccessStream stream)
{
var decoder = await BitmapDecoder.CreateAsync(stream);
using (SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync())
{
var writeableBitmap = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight);
softwareBitmap.CopyToBuffer(writeableBitmap.PixelBuffer);
return writeableBitmap;
}
}