1

I am creating icons on the fly for a set of winforms/clickonce applications. I have it all working by following http://www.vbforums.com/showthread.php?396650-Create-Valid-Icon-Files!-In-24-bit-true-color and adapting to C#.

I would like to have a different background colour for different environments, e.g. Live has a white background, QA is yellow, and DEV should be orange. All seems pretty easy and I have been able to create my temp.ico with white and yellow backgrounds using the following:

    private static void CreateTempIcon(int size)
    {
        var myBmp = new Bitmap(size, size);
        var graphics = Graphics.FromImage(myBmp);
        var bgColor = GetIconBgColor();

        graphics.Clear(bgColor);

        var myIcon = Icon.FromHandle(myBmp.GetHicon());
        var st = new FileStream(TempIcon, FileMode.Create);
        var wr = new BinaryWriter(st);
        myIcon.Save(st);
        myBmp.Dispose();
        wr.Close();
    }

    private static Color GetIconBgColor()
    {
        switch (ApplicationSettings.Branch)
        {
            case BranchType.Live:
                return Color.White;
            case BranchType.Dev:
                return Color.Orange;
            case BranchType.QA:
                return Color.Yellow;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }

All of the colours show correctly, except for orange which shows as red.

I have tried various things including

  1. Creating the bitmap with a pixelformat

    var myBmp = new Bitmap(size, size, PixelFormat.Format32bppArgb);
    
  2. Creating the colour from argb

    return Color.FromArgb(255, 255, 165, 0);
    

and variations on using/not using alpha channels, using Color.FromColor, etc. It's like my colour palette is limited to only a few basic colours. The link above says the initial icon is created in 24 bit colour which as far as I know includes orange.

I don't know a huge amount about image formats (clearly!). Could anyone point me in the direction of what I am doing wrong?

pook
  • 632
  • 7
  • 10
  • Have you tried replacing the Color.Orange with some other colour to see if it's a problem related to the Color or to some other part of your logic? Consider replacing it with Yellow, as you know that this works downstream of your `GetIconBgcolor` method correctly – LordWilmore Nov 15 '16 at 12:22
  • I know it's not what you want but, what happens if you save bitmap `myBmp.Save(filepath)` right after you clear image `graphics.Clear(bgColor);`. See if the file saved at *filepath* has orange color. – Kamalesh Wankhede Nov 15 '16 at 12:24
  • @LordWilmore - yes it does work with other colors including Red, Yellow, White. I have managed to get murky green, purple, grey and various other shades in testing (when setting the colour using Color.FromArgb – pook Nov 15 '16 at 12:45
  • @krw12572 - The .bmp is saved with an orange background! So it must be doing something when getting the icon from the bmp in Icon.FromHandle – pook Nov 15 '16 at 12:48
  • @krw12572 - Thank you, that helped a lot. Narrowing it down to the Icon.FromHandle led me to this: [vbforums.com-Icon only saved as 16 colors](http://www.vbforums.com/showthread.php?395933-2003-Icon-only-saved-as-16-colors), meaning I can't save my icons because ".NET Framework does not have an encoder that allows you to save files as WMF, EMF, or ICON files." So! Back to the drawing board - instead of setting the background on the initial temp icon I am setting it on each of the bmp's that make up the 4x IconDeviceImages used in the icon. So I have something working now :) Thanks both – pook Nov 15 '16 at 13:07
  • 1
    GetHicon() does not do what you hope it does. [Consider this](http://stackoverflow.com/a/21389253/17034). – Hans Passant Nov 15 '16 at 13:17
  • @HansPassant - thanks, I will delve into that this afternoon... – pook Nov 15 '16 at 13:25

0 Answers0