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


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.



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.



The bitmap transform classes

WPF bitmaps are immutable but that doesn't mean you can't transform them. Find out about rotation, scaling, changing pixel formatting and how to mangage color profiles.



Loading Bitmaps: DoEvents and the closure pattern

Sometimes loading a bitmap causes an asynchronous download and you have to wait for it to complete before using it - but how best to wait? The standard solution is to use an event but this breaks what [ ... ]



WPF .NET Core - Creating Objects With XAML

If you've never encountered WPF (Windows Presentation Foundation) you are missing a versatile tool. This article is part of a series devoted to it. XAML can be confusing - especially if you think it i [ ... ]


Other Articles

<ASIN:0672328917>

<ASIN:1590598849>



Last Updated ( Sunday, 06 June 2010 )