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://
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


RenderTargetBitmap - Visual vector to bitmap

RenderTargetBitmap will convert any Visual to a bitmap but sometimes it isn't quite as straighforward as just calling Render().



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.



Routed Events

Routed events were new in .NET 3.5 but there is still plenty to discover about using them. We look at bubbling and tunneling and how to create your own routed even and how WPF changes the underlying m [ ... ]



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 [ ... ]



BitmapSource: WPF Bitmaps

Although bitmap graphics are not a main focus of WPF it does offer some useful facilities


Other Articles

<ASIN:0672328917>

<ASIN:1590598849>



Last Updated ( Sunday, 06 June 2010 )
 
 
Banner

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