public TemplateMatch[] ProcessImage( Bitmap image, Bitmap template, Rectangle searchZone )
{
// check image format
if (
( ( image.PixelFormat != PixelFormat.Format8bppIndexed ) &&
( image.PixelFormat != PixelFormat.Format24bppRgb ) ) ||
( image.PixelFormat != template.PixelFormat ) )
{
throw new UnsupportedImageFormatException( "Unsupported pixel format of the source or template image." );
}
// check template's size
if ( ( template.Width > image.Width ) || ( template.Height > image.Height ) )
{
throw new InvalidImagePropertiesException( "Template's size should be smaller or equal to source image's size." );
}
// lock source and template images
BitmapData imageData = image.LockBits(
new Rectangle( 0, 0, image.Width, image.Height ),
ImageLockMode.ReadOnly, image.PixelFormat );
BitmapData templateData = template.LockBits(
new Rectangle( 0, 0, template.Width, template.Height ),
ImageLockMode.ReadOnly, template.PixelFormat );
TemplateMatch[] matchings;
try
{
// process the image
matchings = ProcessImage(
new UnmanagedImage( imageData ),
new UnmanagedImage( templateData ),
searchZone );
}
finally
{
// unlock images
image.UnlockBits( imageData );
template.UnlockBits( templateData );
}
return matchings;
}