1

I'm trying to display an image on a Windows 8 app. The image data is gathered from a web service, and provided as a Base64 encoded string.

I found the following on Stack Overflow:

How do i read a base64 image in WPF?

However, when I come to use the BitmapImage Class, I can't seem to access System.Windows.Media.Imaging, even though the following Microsoft documentation leads us to believe that it is available for use in .NET 4.5, and with Windows 8 apps:

http://msdn.microsoft.com/en-us/library/ms619218.aspx

Many thanks for your help.

Community
  • 1
  • 1
Cheese1223
  • 107
  • 1
  • 11
  • What code do you have so far? What are trying that isn't working or available in the api's? – Chris Pietschmann Feb 13 '13 at 14:34
  • The problem I'm having is accessing the BitmapImage class, because from within my application I can't add `using System.Windows.Media.Imaging;` because "The type or namespace 'Media' does not exist in the namespace System.Windows", even though the Microsoft documentation says it does. – Cheese1223 Feb 13 '13 at 14:46
  • 1
    `System.Windows.Media.Imaging` is a full .NET namespace for WPF and Silverlight. Windows Store apps use a different, limited crop of the framework and the UI stack is actually not in it at all. It is in the Windows Runtime namespace: `Windows.UI.Xaml.Media.Imaging`. It is also slightly different in terms of API. – Filip Skakun Feb 13 '13 at 17:45

1 Answers1

3

The classes you want are in the Windows.UI.Xaml.Media.Imaging namespace. Here is an example of taking a Base64 image and creating an image out of it ...

var img = @"/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQ ... "; // Full Base64 image as string here
var imgBytes = Convert.FromBase64String(img);
var ms = new InMemoryRandomAccessStream();
var dw = new Windows.Storage.Streams.DataWriter(ms);
dw.WriteBytes(imgBytes);
await dw.StoreAsync();
ms.Seek(0);

var bm = new BitmapImage();
bm.SetSource(ms);

// img1 is an Image Control in XAML
bm.ImageOpened += (s, e) => { this.img1.Source = bm; };
bm.ImageFailed += (s, e) => { Debug.WriteLine(e.ErrorMessage); };

I could not copy a full Base64 image into the answer.

JP Alioto
  • 44,864
  • 6
  • 88
  • 112