Page 2 of 2
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(
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.