Wednesday, 27 February 2013

Windows Store app Lock Screen Badge Icon fix

There is a requirement in Windows 8 Store apps that lock screen badges be white only, with varying transparency levels. Visual Studio doesn't enforce this; you only find out when you run the App Cert Kit and your app fails with:

Image reference "Foo.png": The image "\Foo.png" has an ABGR value "0x1CAAAAAA" at position (0, 0) that is not valid. The pixel must be white (##FFFFFF) or transparent (00######).

You then jump on Google and find this or this.

Manually altering 24*24 pixels individually is not my cup of tea.

Here is some trivial VB.Net code that will take any image and convert it to a format suitable to use for a lock screen badge icon (ie every pixel has RGB of FFFFFF, with a varying A). You can then use Paint.Net or whatever to resize the image.


Imports System.Drawing
Private Function conv2Badge(img As Bitmap)
    Dim destImage = New Bitmap(img.Width, img.Height, Imaging.PixelFormat.Format32bppArgb)
    For x = 0 To img.Width - 1
        For y = 0 To img.Height - 1
            Dim srcCol = img.GetPixel(x, y)

            Dim avgInt = (((CInt(srcCol.R) + CInt(srcCol.G) CInt(srcCol.B)) / 3) * CInt(srcCol.A)) / 255

            Dim destCol = Color.FromArgb(CByte(avgInt), Color.White)

            destImage.SetPixel(x, y, destCol)
        Next
    Next

    Return destImage
End Function

No comments: