Finding the most used color in a WriteableBitmap

I had a simple idea which would require the most used color in an Image. Sounds like something simple, just walk through all the pixels and for each occurring color track the occurrence count. I started with a bit of googling to find if someone else already found a solution. Indeed someone answered a similar question on codeproject.


It’s not compatible with the Windows Universal Platform I’m working on right now, however rewriting it is not that difficult. Just had to use the GetPixel operation from the WriteableBitmapEx. A working solution, but it was slow, really slow. It took about 40 seconds for a bitmap of 182 by 182, way too slow be acceptable for my usage. I did look at possible optimizations using Task that run on the ThreadPool but that didn’t help me anything, accessing the WriteableBitmap via a different thread is not supported.

WriteableBitmapEx to the Rescue

So I thought again, why wouldn’t WriteableBitmapEx have a solution I can use to do the calculation? And indeed it has a solution, a ForEach method. After rewriting my code to make use of the ForEach method, it runs fast, really fast. It now takes 0.03 seconds for a bitmap of 182 by 182. That is more than a factor 1000 faster.

I did not investigate what’s happening inside the ForEach implementation of WriteableBitmapEx, but it is better than the double looping over the x and y axis to get all pixels. Don’t forget to add the NuGet Package of WriteableBitmapEx to your project.

public static Color GetMostUsedColor(this WriteableBitmap bitmap)
    var colorUsage = new Dictionary<int, int>();

    bitmap.ForEach((x, y, colorin) =>
        var pixelColor = colorin.ToArgb();

        if (colorUsage.Keys.Contains(pixelColor))
            colorUsage.Add(pixelColor, 1);
        return colorin;

    var sortedColorUsage = colorUsage
        .OrderByDescending(x => x.Value)
        .ToDictionary(x => x.Key, x => x.Value);

    return sortedColorUsage.First().Key.FromArgb();

public static int ToArgb(this Color color)
    return (color.A << 24) | (color.R << 16) | (color.G << 8) | color.B;

public static Color FromArgb(this int argb)
    return Color.FromArgb((byte)(argb >> 24),
        (byte)(argb >> 16),
        (byte)(argb >> 8),

Bonus: Perceived brightness, is the color bright or dark?

My intention is to use the most-used-color as a background color. Because the image I want to base this on can be anything, I need to know a good color to use for text on top of this color. Someone else already wrote a good working solution about the perceived brightness and explains how he came to that solution.

public static Color BestForeground(this Color backgroundColor)
    return backgroundColor.PerceivedBrightness() > 130 ? Colors.Black : Colors.White;

public static int PerceivedBrightness(this Color color)
    return (int)Math.Sqrt(
        color.R * color.R * .241 +
        color.G * color.G * .691 +
        color.B * color.B * .068);

Windows Store Apps, Error: The certificate specified has expired.

Today I tried to update one of my Windows Store Apps. When I tried to create an App Package for the Store I got an error. One that was completely new to me.

The certificate specified has expired. For more information about renewing certificates, see

We’re lucky this time, renewing the certificate is very easy. Just follow below steps (copied from MSDN).

  1. In Solution Explorer, open the shortcut menu for the .appxmanifest file, choose Open With, and then choose App Manifest Designer.
  2. In the App Manifest Designer, choose the Packaging tab, and then choose the Choose Certificate button.
  3. In the Choose Certificate dialog box, expand the Configure Certificate list, and then choose Create test certificate.
  4. In the Create test certificate dialog box, click the OK button.

In the end Visual Studio will generate the new certificate with a new expiry date. The new certificate will be valid for a full year.

A small tale of bringing push notifications to Windows Azure

Some time ago I got more and more problems on my shared hosting because it couldn’t handle the amount of push notifications I wanted to send. I heard my hosting provider tell me that I was quite often taking more than 90% of the CPU on the server. So I thought to give Windows Azure a try.

I wanted to make optimal use of Windows Azure so I designed my solution to make use of Windows Azure Table Storage to store registered devices and pushchannels. When making use of Azure Table Storage it’s important to choose your RowKey and PartitionKey carefully.

To start with the PartitionKey, I chose to put the ApplicationName in there. I want to send push notifications to different applications I created, like Fokke & Sukke and iBood. So far I haven’t found a reason I made a wrong decision.

And now the RowKey, something that I need to use more carefully. The combination of PartitionKey and RowKey needs to be unique. So I wanted to put the DeviceId of the Device that should receive the PushNotification in there, that combined with the platform identifier. As far as I know there’s nothing that guarantees that the DeviceId is unqiue over different platforms. So I prefix the DeviceId with “WP|” for Windows Phone and “RT|” for Windows 8. The rest was just the copy of the DeviceId. I tested this using the emulator, and everything seems to work fine.

Windows Phone app trouble

After a while I notices reviews telling me that Push Notification don’t work, even further, it didn’t work on my own Windows Phone. After searching for many different reasons for this trouble, I found the source, more or less.

When saving an entity to the Azure Table Storage, every now and then a StorageException occurred. There aren’t many details in the exception, so after attaching Fiddler to my Nokia Lumia 920, I saw interesting stuff happening on the line.

The DeviceId contained special characters. I didn’t notice this when using the emulator, because the DeviceId on the emulator didn’t contain any special characters. So in total I had a percentage of users that could never register because of the StorageException, I still have no idea how large that percentage is, DeviceIds at least regularly contain the ‘/’ character.

Lesson learned, make sure the RowKey and PartitionKey don’t contain special characters: /, \, #, ?

Windows Style app trouble

Besides the Windows Phone issues, I had a very strange behavior on Windows 8 as well. It happened that I was sending a push notification to my local (installed through Visual Studio) app that did not appear. For example I sent a BadgeNotification with value 1 and sometimes the value 17 appeared. I have been trying to find the reason behind it, I never found it. Because when I tried to debug it explicitly with a value like 4 it did show 4. I never got feedback about issues with the push notifications on Windows 8 since my move to Windows Azure, but my dev-machine had troubles.

So after a couple of weeks trying to find causes for the problem I did something that was my final call. I did uninstall the app, and installed the app from the Store. What happened? The pushnotifications started behaving correctly. I have no understanding about the differences between the apps, but it’s good to be aware that there seem to be differences between the app installed from the store and the app installed by Visual Studio.

Your app, featured in the Store, WOW!

The first week of March, one of my Windows 8 apps got featured in the Dutch Windows 8 Store. There are a lot of things you can do to increase the amount of downloads your app is getting, but getting featured helps a lot. Normally the app get’s about 10 to 15 downloads a day, during the featured period it was around 110 each day (178 on March 3rd).



Of course the “how to get featured” is a big secret. We do know, not providing the promotional art during the submission process will not help your app getting featured.

If you have any tips, please share them in the comments.

dotNed Podcast–Windows 8 and Windows Phone 8

A couple of weeks ago Maurice de Beijer recorded a podcast interview with me for the dotNed usergroup. It’s about Windows 8 and Windows Phone 8, but completely in Dutch. If you’re interested but haven’t listened to it yet, give it a try.

dotNed Podcast - Windows 8 en Windows Phone 8 met Mark Monster