|Monday, 03 May 2010|
Page 2 of 5
We also need to implement a constructor. If you want the new class to be usable from XAML then you need a parameterless constructor. However in this case you would also need to solve the problem of creating the test card bitmap as soon as the width and height properties were set. This is best tackled by implementing the ISupportInitialize interface but this would complicated the example - so for simplicity we will create a parameterless constructor that creates the test bitmap pattern at a default size and a parameterized constructor that allows you to specify the size.
Both constructors make use of a private method CreateTest Bitmap that actually creates the bitmap resource and both store this is in the private field TestBS:
private BitmapSource TestBS;
Notice that the TestBS BitmapSource is encapsulated within our new custom class an it is this BitmapSource that is going to provide most of the properties and methods by delegation but first we have to actually create it and the test pattern.
All of the work is done by CreateTestBitmap but from the point of view of creating the custom BitmapSouce class this is the least interesting method of all:
private BitmapSource CreateTestBitmap(
This uses two for loops to set the bits in the byte array "bits" into a crosshatch pattern. It then uses the byte array to create and return a new instance of BitmapSource using reasonable values for the resolution, format etc.
If you find any of this difficult to follow then read: BitmapSource: WPF Bitmaps.
The for loops use a SetPixel method which again isn't really part of the task in hand i.e. building a custom BitmapSource but for completeness this is:
private void setpixel(
|Last Updated ( Sunday, 02 May 2010 )|