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

Banner

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;
public TestBitmapSource()
{
TestBS = CreateTestBitmap(100, 100);
}

public TestBitmapSource(int width,
int height)
{
TestBS = CreateTestBitmap(
width, height);
}

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(
int width,int height)
{
int stride = width * 3 +
(width * 3) % 4;
byte[] bits = new
byte[height * stride];
for (int y = 0; y < height; y += 10)
{
for (int x = 0; x < width; x++)
{
setpixel(ref bits, x, y,
stride, Colors.White);
}
}
for (int x = 0; x < width; x += 10)
{
for (int y = 0; y < height; y++)
{
setpixel(ref bits, x, y,
stride, Colors.White);
}
}
return BitmapSource.Create(
width, height,
96, 96,
PixelFormats.Rgb24,
null,
bits,stride);
}

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(
ref byte[] bits,
int x, int y,
int stride, Color c)
{
bits[x * 3 + y * stride] = c.R;
bits[x * 3 + y * stride + 1] = c.G;
bits[x * 3 + y * stride + 2] = c.B;
}

This simply uses the appropriate storage mapping function for the image size and format selected. Again this is described in BitmapSource: WPF Bitmaps and in many other articles. 

Banner

<ASIN:1430272406>

<ASIN:0470502223>

<ASIN:1430272406>

<ASIN:0672331195>

<ASIN:0470502258>



Last Updated ( Sunday, 02 May 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.