« Posts tagged WBF

WPF C#, CalculateAverageColor for bitmap

Here is a simple method to calculate the average color in a bitmap (WPF C#). The result is not always the best, it can tend to be muddy or more like a grey-scale effect. An improved method is to find the dominant color, though more computationally expensive – I will post an example soon.

public static Color CalculateAverageColor(BitmapSource source)
            if (source.Format.BitsPerPixel != 32)
                throw new ApplicationException("expected 32bit image");
            Color cl;
            System.Windows.Size sz = new System.Windows.Size(source.PixelWidth, source.PixelHeight);
            //read bitmap 
            int pixelsSz = (int)sz.Width * (int)sz.Height * (source.Format.BitsPerPixel / 8);
            int stride = ((int)sz.Width * source.Format.BitsPerPixel + 7) / 8;
            byte[] pixels = new byte[pixelsSz];
            source.CopyPixels(pixels, stride, 0);
            //find the average color for the image
            const int alphaThershold = 10;
            UInt64 r, g, b, a; r = g = b = a = 0;
            UInt64 pixelCount = 0;
            for (int y = 0; y < sz.Height; y++)
                for (int x = 0; x < sz.Width; x++)
                    int index = (int)((y * sz.Width) + x) * 4;
                    if (pixels[index + 3] <= alphaThershold) //ignore transparent
                    a += pixels[index + 3];
                    r += pixels[index + 2];
                    g += pixels[index + 1];
                    b += pixels[index];
            //average color result
            cl = Color.FromArgb((int)(a / pixelCount),
                                (int)(r / pixelCount),
                                (int)(g / pixelCount),
                                (int)(b / pixelCount));
            return cl;           

WinBioEnrollCommit Hang

Recently I have been working with the Windows Biometric Framework (WBF) for some fingerprint scanner applications. One of the methods in the API is WinBioEnrollCommit.

I have been using the async calls of the API exclusively, and naturally I was calling the function WinBioEnrollCommit within the callback that was passed to WinBioEnrollCaptureWithCallback. This is slightly different from the only sample code that is provided with the MSDN documentation but I didn’t think twice about it as there is nothing mentioned about the requirements or threading characteristics of the framework and the callbacks.

So this lead to some very surprising application hangs or blocking in this call. It just would never return. However this could be related to the manufacturer implementation of the drivers.

Either way the solution was to move the WinBioEnrollCommit out of the callback function and into the same function that made the original call to WinBioEnrollCaptureWithCallback, while also making use of WinBioWait.

- On a another topic I have also found that none of the WBF API’s work correctly when run in an active x control when it is contained in the newer version of IE, 8 and above. There are some incompatibilities with the process architecture that s preventing any of the WBF calls from working correctly in that environment.

- Oh and WBF doesn’t really work over RDP.. strange