GustavoFSx GustavoFSx - 10 months ago 75
C# Question

Using WCF to send image to Windows Universal App

I want serve an

System.Drawing.Image
with WCF to my UWP App.

The problem is that the client needs a
Windows.UI.Xaml.Media.Imaging.BitmapImage


I've tried lot's of solutions but nothing worked. I'm currently passing the image as a byte array to the client, but I can't find a way to read the byte array into an BitmapImage.

The WCF client is using .Net 4.6 and the Universal Windows App is targeting Windows 10 Anniversary Edition

Answer Source

Here is a method that creates a BitmapImage from a byte array:

using System;
using System.IO;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;
...

private async Task<BitmapImage> CreateBitmapImage(byte[] bytes)
{
    var bitmapImage = new BitmapImage();

    using (var stream = new MemoryStream(bytes))
    {
        await bitmapImage.SetSourceAsync(stream.AsRandomAccessStream());
    }

    return bitmapImage;
}