Home > Uncategorized > Display Data URI Image in Silverlight (Windows Phone)

Display Data URI Image in Silverlight (Windows Phone)

The data uri is a string based representation of an image, which is base-64 encoded, so it’s easy to store in Javascript, and pass it in Post data. It bloats the image size, so it isn’t an efficient format, but it’s certainly easy to handle.

Here is an example of how to render a image from a data URI in Silverlight – which should also be suitable for Windows Phone 7, 8, and 8.1 (Silverlight).

Here’s my XAML:

<Grid x:Name=”LayoutRoot” Background=”White” Loaded=”LayoutRoot_Loaded”>
<Image x:Name=”imgDisplay”></Image>

And this is the underlying C# code:

public static BitmapSource BitmapSourceForDataUri(Uri dataUri)
var dataString = dataUri.OriginalString;
var b64Start = dataString.IndexOf(“,”, StringComparison.Ordinal);
if (b64Start == -1) return null;
var base64 = dataString.Substring(b64Start + 1);
Stream stream = new MemoryStream(Convert.FromBase64String(base64));
var image = new BitmapImage();
return image;

private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
const string strDataUri =
var bmp = BitmapSourceForDataUri(new Uri(strDataUri));
imgDisplay.Source = bmp;
imgDisplay.Width = bmp.PixelWidth;
imgDisplay.Height = bmp.PixelHeight;

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: