As you’ll recall, you can display a bitmap in one of two ways: using the Image element or creating an ImageBrush. The Source property of the Image element and the ImageSource property of the ImageBrush are both of type ImageSource, a class occupying a very fundamental place in the region of the Silverlight class hierarchy devoted to bitmaps:
Object
DependencyObject (abstract)
ImageSource (abstract)
BitmapSource (abstract)
BitmapImage
WriteableBitmap
ImageSource
has only one descendent class and defines nothing public on its own, so
it might seem a little superfluous. That’s true in Silverlight but not
in the Windows Presentation Foundation, where ImageSource is a parent to classes that define images derived from vector graphics as well as those involving raster graphics.
The remaining three classes are all defined in the System.Windows.Media.Imaging namespace.
BitmapSource defines two public get-only properties and one method:
A PixelWidth property of type int.
A PixelHeight property of type int.
A SetSource method that has one argument of type Stream.
That Stream argument can be a file stream, a network stream, or a memory stream of some sort. But the Stream must provide bitmap data in either the JPEG or PNG file format. Once a bitmap has been created, it has a fixed size that cannot be changed.
The BitmapImage class expands on the functionality of BitmapSource by letting you reference a bitmap with a URI. BitmapImage defines the following:
A constructor that accepts an argument of type Uri.
A UriSource property of type Uri.
A CreateOptions property.
Three events that let you track downloading progress, and report upon success or failure
The CreateOptions property is of type CreateOptions, an enumeration with three members: None, DelayCreation, and IgnoreImageCache. The default is DelayCreation, which doesn’t start loading an image until it’s actually needed for rendering. The IgnoreImageCache is useful when a program knows that a previously loaded image has become invalid. You can combine DelayCreation and IgnoreImageCache with the C# bitwise OR operator.
By combining the features of BitmapSource and BitmapImage, the BitmapImage class lets you load a bitmap in JPEG or PNG format using either a Stream object or a Uri object. There is no facility to save bitmaps.
The WriteableBitmap class itself continues this trend. Taken by itself, WriteableBitmap does not including any facility to save bitmaps. However, the WriteableBitmap class does give you access to all the pixels
that define the bitmap. Only one pixel format is supported, where each
pixel is a 32-bit value. You can obtain the pixel bits from an existing
bitmap, or set new pixel bits on a WriteableBitmap
to define the image. Access to these pixel bits allows you a great deal
of flexibility in how you save or load bitmaps. You can provide your
own bitmap “encoder” to save pixel bits in a particular bitmap format,
or your own “decoder” to access a file of a particular format and
convert to the uncompressed pixel bits.
WriteableBitmap also provides a facility to “draw” images on the bitmap based on Silverlight elements. Although you can indeed draw Button elements and SliderShape. In other words, WriteableBitmap allows you to convert a vector drawing into a raster image. elements on a bitmap, it’s most common to use elements that derive from
Here are the constructors, methods, and property defined by WriteableBitmap:
A constructor that accepts a UIElement and a transform.
A constructor that accepts a pixel width and height.
A constructor that accepts a BitmapSource object.
A Render method that accepts a UIElement and a transform.
An Invalidate method to update bitmap visuals.
A property named Pixels of type int array.
Keep in mind that WriteableBitmap derives from BitmapSource rather than BitmapImage, so there is no facility in WriteableBitmap to load a bitmap from a URI. However, you can load a BitmapImage object from a URI and then create a WriteableBitmap from that using the third constructor I’ve listed.
WriteableBitmap lets you put images on the bitmap using two techniques:
You can combine these techniques in whatever manner you want.
In addition, Windows
Phone 7 provides several subsidiary methods that provide you with
alternative ways to load JPEG files and to save them:
A static PictureDecoder.DecodeJpeg method in the Microsoft.Phone namespace lets you load a JPEG file from a Stream but with a maximum Width and Height.
This is useful if you know that a particular JPEG might be much larger
than what you need to display on the phone. The method returns a WritableBitmap.
An Extensions class in the System.Windows.Media.Imaging namespace has two extension methods to WriteableBitmap: LoadJpeg (which doesn’t provide additional functionality over the SetSource method defined by BitmapSource) and SaveJpeg, which lets you alter the width and height of the image and specify a compression quality.
The SavePicture method of the XNA MediaLibrary class lets you save a bitmap to the phone’s picture library from a Stream or a byte array in JPEG format. You’ll probably use this in conjunction with the SaveJpeg extension method with a MemoryStream intermediary.