Click here to Skip to main content
13,254,373 members (65,072 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 14 Feb 2013

BitmapImageExtension with NetworkCredentials

, 14 Feb 2013
Rate this:
Please Sign up or sign in to vote.
BitmapImageExtension with NetworkCredentials.


This code extension allows for a BitmapImage to use a remote image as source which requires NetworkCredentials in WinRT.


When I started writing an app which uses files on a remote computer which uses Basic network authentication I had to find a way of databind to these images. This is what I came up with.

Using the code

Basic Usage

The basic use of this extension is simple,. Create your BitmapImage and you will find two new overloads of the SetSourceAsync method.

var thumbnailSoure = new BitmapImage();
            "Username", "Password");


A basic way of implementing databinding to a source which requires NetworkCredentials:

<Image Source="{Binding ThumbnailSource}" />
private BitmapImage thumbnailSoure;
public BitmapImage ThumbnailSource
        if (thumbnailSoure == null)
            thumbnailSoure = new BitmapImage();
               "Username", "Password");

        return thumbnailSoure;

The Extension

Just add this class into your project

using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Windows.Storage.Streams;

namespace Windows.UI.Xaml.Media.Imaging
    public static class BitmapImageExtensions

        public static async Task<bool> SetSourceAsync(this BitmapImage image, 
               string url, string username, string password)
            return await image.SetSourceAsync(new Uri(url), username, password);

        public static async Task<bool> SetSourceAsync(this BitmapImage image, 
               Uri uri, string username, string password)
            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.Credentials = new NetworkCredential(username, password);            
            request.Method = "GET";            
            request.Accept = "image/gif;q=0.3, image/x-xbitmap;q=0.3, " + 
              "image/jpeg;q=0.3, image/pjpe;q=0.3g, image/png;q=0.3";

                var response = await request.GetResponseAsync();

                if (response != null)
                    Stream stream = response.GetResponseStream();
                    MemoryStream ms = new MemoryStream();
                    var randomAccessStream = await ConvertToRandomAccessStream(ms);
                    await image.SetSourceAsync(randomAccessStream);
                    return true;
                return false;
            return false;

        private static async Task<IRandomAccessStream> ConvertToRandomAccessStream(MemoryStream memoryStream)

            var randomAccessStream = new InMemoryRandomAccessStream();
            var outputStream = randomAccessStream.GetOutputStreamAt(0);            
            var dw = new DataWriter(outputStream);

            var task = Task.Factory.StartNew(() => dw.WriteBytes(memoryStream.ToArray()));
            await task;

            await dw.StoreAsync();
            await outputStream.FlushAsync();

            return randomAccessStream;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Fredrik Zetterlund
Software Developer (Senior) Empir AB
Sweden Sweden
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 14 Feb 2013
Article Copyright 2013 by Fredrik Zetterlund
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid