Custom BitmapSource
Written by Administrator   
Monday, 03 May 2010
Article Index
Custom BitmapSource
Implementing a constructor
Properties
Image control
Advanced BitmapSource

Properties

At this point you might be tempted to try out the class with something like:

TestBitmapSource TBS = 
new TestBitmapSource(1002, 809);
image1.Source = 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.

Banner

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
{
get
{
return TestBS.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
{
get
{
return TestBS.PixelHeight;
}
}
public override int PixelWidth
{
get
{
return TestBS.PixelWidth;
}
}

It also needs to know the resolution to display the bitmap at:

public override double DpiX
{
get
{
return TestBS.DpiX;
}
}
public override double DpiY
{
get
{
return TestBS.DpiY;
}
}

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
{
get
{
return TestBS.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:

  • Format
  • PixelWidth
  • PixelHeight
  • DpiX
  • DpiY
  • Palette

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
EventHandler<ExceptionEventArgs>
DecodeFailed;

Banner

<ASIN:1430225254>

<ASIN:1847199763>

<ASIN:0470502266>

<ASIN:0470477229>

<ASIN:159059990X>



Last Updated ( Sunday, 02 May 2010 )