BitmapImage and local files
Written by Administrator   
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://
application:,,,/Resources/MyImage.jpg");

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

URIstream

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(
@"test.jpg",FileMode.Open));
byte[]buff=br.ReadBytes(
(int)br.BaseStream.Length);

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.BeginInit();
bmi.StreamSource = ms;
bmi.EndInit();

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.

Banner


WPF .NET Core - Routed Events

Routed events are a key feature of WPF. We look at bubbling and tunnelling and how to create your own routed event and how WPF changes the underlying mechanics of Windows.



FlexGrid - A Lightweight Data Grid

There are more data grids available than the standard one that comes with WPF. In this article we take a look at FlexGrid for WPF and discover how easy it is to use.



WPF The Easy 3D Way

WPF provides a full 3D graphics experience without the need to master DirectX. It really is the easy 3D approach.



WriteableBitmap

WriteableBitmap gives you a bitmap object that you can modify dynamically - but exactly how to do this isn't always obvious.



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.


Other Articles

<ASIN:0672328917>

<ASIN:1590598849>



Last Updated ( Sunday, 06 June 2010 )