Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
Windows Phone

Raster Graphics - The Bitmap Class Hierarchy

3/28/2011 9:45:03 AM
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:
DependencyObject (abstract)
ImageSource (abstract)
BitmapSource (abstract)

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:

  • By rendering the visuals of any UIElement on the bitmap.

  • By directly manipulating the pixel bits.

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.

Other -----------------
- Vector Graphics : The Path Markup Syntax
- Vector Graphics : Bézier Curves
- Vector Graphics : The Versatile PathGeometry
- Vector Graphics : Grouping Geometries
- Vector Graphics : Geometries and Transforms
- Vector Graphics : The Path Element
- Vector Graphics : Dynamic Polygons
- Vector Graphics : The Stretch Property
- Vector Graphics : Polygon and Fill
- Vector Graphics : Caps, Joins, and Dashes
- Vector Graphics : Polylines and Custom Curves
- Vector Graphics : Overlapping and ZIndex
- Vector Graphics : Canvas and Grid
- Vector Graphics : The Shapes Library
- Data Bindings : TextBox Binding Updates
- Data Bindings : Give and Take
- Data Bindings : Converters with Properties
- Data Bindings : Simple Decision Making
- Data Bindings : Setting the DataContext
- Data Bindings : A Simple Binding Server
Most view of day
- Windows Server 2008 Server Core : Decompressing Files with the Expand Utility, Performing Advanced File Comparison with the FC Utility
- Microsoft Systems Management Server 2003 : Analysis and Troubleshooting Tools - Using SMS Trace (part 1) - Obtaining SMS Trace
- Windows Server 2003 on HP ProLiant Servers : File Replication Service Design and Implementation (part 1)
- Windows 7 Mobility Features : Power Management (part 2) - Power Options Control Panel
- Managing Change through Group Policy (part 3) - Working with GPO tools
- Windows Phone 8 : Developing for the Phone - Application Lifecycle (part 2) - Navigation
- Working with the Windows Home Server Registry : Working with Registry Entries - Changing the Value of a Registry Entry
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 3) - Debugging Scheduled Tasks
- Customizing Windows 7 : Set Up a Desktop Slide Show
- Microsoft Visio 2010 : Modifying a Graphic (part 1) - Resizing a Graphic
Top 10
- Windows Phone 8 : Scheduled Tasks - Scheduled Task API Limitations
- Windows Phone 8 : Scheduled Tasks - Updating Tiles Using a Scheduled Task Agent
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 5) - Editing an Existing To-Do Item
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 4) - Creating the To-Do Item Shell Tile, Saving a To-Do Item
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 3) - Debugging Scheduled Tasks
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 2) - TodoService, TodoItemViewModel
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 1) - TodoItem,TodoDataContext
- Windows Phone 8 : Scheduled Tasks - Using Scheduled Tasks
- Windows Phone 8 : Scheduled Tasks - Background Agent Types
- Windows Phone 8 : Windows Phone Toolkit Animated Page Transitions - Reusing the Transition Attached Properties
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro