|Written by Administrator|
|Monday, 03 May 2010|
Page 3 of 5
At this point you might be tempted to try out the class with something like:
TestBitmapSource TBS =
However if you do the result will be an exception. The reason is that you have to implement many of the properties and methods that the base class seems to supply by inheritance to your class but in fact doesn't.
In this case it is fairly easy to implement these missing methods because all we have to do is use the encapsulated BitmapSource and defer the calls to its methods.
For example, consider the problem of the Format property which returns a PixelFormat object to specify the format of the bitmap. We could arrange to return the PixelFormat that was used to construct the encapsulated bitmap but as the encapsulated bitmap knows its format why not ask it. That is:
public override PixelFormat Format
This is typical coding when you are delegating calls to the wrapper class to the encapsulated class.
You can see that all of the BitmapSource properties and methods can be overridden in the same simple way by deferring to the encapsulated class TestBS. To show how things work we will only implement the methods and properties actually used to display a BitmapSource in say an Image control. The Image control needs to know the format of the bitmap (already listed), its size:
public override int PixelHeight
It also needs to know the resolution to display the bitmap at:
public override double DpiX
More surprisingly it also needs to know the palette in used even if the format is such that no palette is in use:
public override BitmapPalette Palette
These are all the properties that need to be implemented for the Image control to know enough about the bitmap to display it - that is:
However if you try the class out again you will find that it still throws an exception. There are two things that still need to be implemented. The first is relatively uninteresting. When the Image control tries to retrieve the bitmap specified by the BitmapSource it first registers the DecodeFailed event so that it can signal the fact that it has failed to make sense of the bitmap data. If you don't provide even a default local implementation of the event then the result is a method not found exception. The simplest thing to do is to allow the compiler to generate a default:
public override event
|Last Updated ( Sunday, 02 May 2010 )|