Loading images from byte arrays in Windows 8 Store apps (WinRT)

Win8Blue_WebI’ve been porting a library and test applications from .NET Framework 3.5 to a Windows 8 Modern UI Store App (WinRT). Windows 8 Store Apps have a cut down implementation of the .NET Framework so that they are none blocking and always responsive. This means that many of the synchronous APIs we are used to have been removed and we’re forced to use their asynchronous counter parts. It has also meant that we’ve needed to port our library to instead be a Portable Class Library so that we can reuse it in different versions of Windows.

The library I’m working on returns byte arrays contains JPEG image data. In the .NET Framework 3.5 it simply took the byte array fed it into a MemoryStream and then into the BitmapDecoder object. Unfortunately this implementation isn’t possible in Windows 8 Store Apps as the BitmapDecoder is blocking and we don’t have blocking APIs in Windows Store apps.

Our old implementation:

public void SetImage(byte[] data, 
    Windows.UI.Xaml.Controls.Image image)
{
    MemoryStream stream = new MemoryStream(data);
    var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
    image.Source = decoder.Frames[0];
}

The new implementation is based around using a InMemoryRandomAccessStream object which we first write the data too, reset the internal pointer to the beginning of the data, and create a BitmapImage object which we then feed into our XAML image object.

Our new implementation:

public async void SetImageFromByteArray(byte[] data, 
    Windows.UI.Xaml.Controls.Image image)
{
    using (InMemoryRandomAccessStream raStream = 
        new InMemoryRandomAccessStream())
    {
        using (DataWriter writer = new DataWriter(raStream))
        {
            // Write the bytes to the stream
            writer.WriteBytes(data);

            // Store the bytes to the MemoryStream
            await writer.StoreAsync();

            // Not necessary, but do it anyway
            await writer.FlushAsync();

            // Detach from the Memory stream so we don't close it
            writer.DetachStream();
        }

        raStream.Seek(0);

        BitmapImage bitMapImage = new BitmapImage();
        bitMapImage.SetSource(raStream);

        image.Source = bitMapImage;
    }
}

A round up of things I’ve been looking at recently

In the name of creativity and plan curiosity I’ve been looking at lots of different things recently. Quite simply anything which caught my eye or seemed interesting.

WebGL exampleWebGL – This has been around for a couple of years now and is simply 3D graphics for the web. Up until recently the only real way to get 3D graphics in browser has been to install a third party plug-in or bloatware software which in most cases doesn’t work and if it does will only work for a very small fraction of devices. WebGL is an open standard being developed by the Khronos Group and supported by Google (Chrome), Opera, Apple (Safari), Firefox a few others. With no great surprise Microsoft Internet Explorer doesn’t support it (though what does it support really?).

WebGL is something to get excited about, and their are lots of people currently developing for it and creating technical demonstrations (see Chrome Experiments and Three.js). I’ve recently started learning how to use WebGL from the book ‘WebGL: Up and Running’ which does a little bit on WebGL and quite a lot on the WebGL wrapper Three.js library. WebGL is development is certainly not an easy one to learn (much like any 3D or OpenGL programming) but there are fantastic libraries such as Three.js which takes quite a bit of the heavy lifting and help to explain some of the more difficult concepts.

Windows 8 Modern UIWindows 8 Modern UI – I decided to take a look at developing for the new Windows 8 UI, mainly to find out what was involved, how easy / hard it is and to create a comparison between it and Android programming. As this is quite a large change to traditional Windows  programming there is a reason amount to take in. Though it works heavily with existing Microsoft programming concepts such as XAML and objects and is generally fairly easy to navigate. I’m still not sure about the new Windows 8 probably as it’s such as major change to a operating system which hasn’t really seem such as dramatic change since the introduction of Windows 95.

Personally I’m sticking with Windows 7 as my main dev environment, why? Well my general philosophy is don’t hide anything from the user, but to show things when they need to be shown. Windows 8 hides a lot, with hidden menus around the screen I can see how it works for tables but it doesn’t work for desktop. I’m one who works in Virtual Machines quite a lot so you have to be quite precise if Windows 8 is on my right screen, the host Windows 7 on my left and I want to click on that precise point in the bottom left to bring up the start menu or top right to bring up this menu, or top left to bring up that menu …. Yes I could just use the Windows key but that not the point. Simply it’s bad design.

ImageHack Design – I may be a software engineer but I love design, even more I love good design. I also like to play with pens, pencils, paper, scissors, post it notes, glue and all those things you used in primary school. I also love to draw as a hobby at home, I may not be up to some peoples standards but I love it. Hack Design is a web site devoted to engineers, programmers, hackers or anyone who wants to move towards design or at least what design is, what it involves and how to start doing it. Hack Design is a weekly digest which uses text, pictures and videos to talk about and explain design, and design ideas.

These are just a selection of some of the top ones I’ve been looking at. In addition to these I’ve also been looking more into Node.js, Underscore.js, Backbone.js, Phantom.js and many others.