BitmapImage and local files
BitmapImage and local files
Monday, 21 December 2009
Article Index
BitmapImage and local files
Working with static resources

Image as resource

If the image that you want to load never changes, i.e. it really is a static resource, then you should consider treating the image as a resource that is bundled up with the assembly for installation.

To do this you first need to create the image resource. Select Project, Properties and the Resources tab. Next select the Image tab and use the Add Resource, Add Existing File command and browse to the location of the image.

When you have finished the Solution Explorer window should show Resource directory with the image file listed. Select the image file and then select the Properties window. Set the image file's Build Action to Resource and while debugging set Copy to OutputDirectory to Copy always. With this completed you can now refer to your image file using a pack URI:

Uri uri= new Uri(@"pack://

To find out how to do more with resource-based bitmaps you need to look up Resources and pack URIs in .NET.


Although the BitmapImage inherits from BitmapSource you can't use the Create method as this is a static method and only creates BitmapSource objects. What this means is that you can't use the same techniques to create a BitmapImage from a raw byte array. However you can read in a correctly formatted stream and hence a memory stream derived from a byte array using the StreamSource property.Notice that this has to be byte stream that represents a jpeg, png or one of the other formats supported - not just raw pixel data.

This is useful when the bitmap in question is stored or generated in a way that isn't naturally connected with a URI - for example if you retrieve a bitmap from a SQL database. 

To demonstrate how this works as simply as possible let's first read a valid JPEG file into byte array to act as a source for a MemoryStream:

 BinaryReader br=new BinaryReader(
new FileStream(

Notice that this uses a relative file path and this means that the JPEG is stored in the same file as the executable - e.g. the Debug folder while you are debugging the program. Also notice that this code snippet reads in the entire bitmap to the array so you need to make sure that it isn't too big. The Length property of the stream is a Long and this is truncated to an int for the purpose of reading in the entire file - if the file is longer than can be accommodated in maxint bytes then things won't work as you expect!

Now we have the file read in as a byte array we can proceed to use it as a MemoryStream and as an source for a BitmapImage:

MemoryStream ms=new MemoryStream(buff);
BitmapImage bmi = new BitmapImage();
bmi.StreamSource = ms;

In this case the bitmap is already read into memory so the result is more or less instantaneous. Notice that the stream remains open unless you explicitly close it.

Unlike the case with BitmapSource the byte array, and hence the MemoryStream, contains a bitmap in a particular image format- JPEG in this case - and not just raw pixel data. If you want to work with raw pixel data use a BitmapSource or a WriteableBitmap. The bitmap system works out the format of the data from the headers etc that it finds in the data and this sometimes causes problems. You can specify a particular decoder to use but this requires a deeper look at more of the bitmap classes.


Drawing Bitmaps – DrawingImage and DrawingVisual


WPF provides multiple ways to convert vector drawings to bitmaps. Find out how DrawingImage and DrawingVisual work and when to use which. On the way we look at how to create 2D vector drawings.

Bitmap Effects

WPF bitmap effects are easy to use but subtle. Before moving on to consider custom effects we take a careful look at what is provided as standard.

Custom Shape

Creating a custom Shape isn't difficult but it does have some hidden gotchas. We take a look at how best to do the job.

Simple WPF data binding

Find out how WPF data binding really works. It's not the binding object that matters - it's the binding expression.

Custom BitmapSource

Even if you don't anticipate implementing your own custom BitmapSource finding out how to reveals quite a lot about the way that WPF bitmaps work.

Other Articles



Last Updated ( Sunday, 06 June 2010 )

RSS feed of all content
I Programmer - full contents
Copyright © 2015 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.