Notification icon transparency issues when dragging

84 views Asked by At

I have a WinForms app, the major component of which is an icon down in the notification area. I've noticed that if I drag this icon (to reorder it, or move it to/from the list of icons that are hidden by Windows) then its transparent pixels are not respected correctly, unlike other icons.

This is illustrated in the animation below; other icons look OK when dragged, but my icon (the red circle) does not (excuse the animation's compression artefacts).

Dragging icons

Looking at that more closely, the icon that normally looks like this:

Thermostat icon

Looks like this when dragged:

Thermostat icon being dragged

A NotifyIcon control is used, and the icon is generated dynamically in various colours and with different numbers overlaid.

In order to maintain translucency around the edges of the icon, PNG format is used (using a code sample from CodeProject) to take a Bitmap and return the Icon that is used by the NotifyIcon:

private static readonly byte[] _pngIconHeader = { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

using (var bmp = new Bitmap(image, new Size(image.Width, image.Height)))
{
    byte[] png;
    using (var ms = new MemoryStream())
    {
        bmp.Save(ms, ImageFormat.Png);
        ms.Position = 0;
        png = ms.ToArray();
    }

    using (var ms = new MemoryStream())
    {
        _pngIconHeader[6] = (byte)image.Width;
        _pngIconHeader[7] = (byte)image.Height;
        _pngIconHeader[14] = (byte)(png.Length & 255);
        _pngIconHeader[15] = (byte)(png.Length / 256);
        _pngIconHeader[18] = (byte)(_pngIconHeader.Length);

        ms.Write(_pngIconHeader, 0, _pngIconHeader.Length);
        ms.Write(png, 0, png.Length);
        ms.Position = 0;

        return new Icon(ms);
    }
}

Is there more that needs to be done to ensure that Windows treats this correctly when it is being dragged?

0

There are 0 answers