Search code examples
c#opentk

Trouble Successfully Setting OpenTK Window Icon


I've been trying to set the icon for my OpenTK window by using the ImageSharp library to load the image from my device and then converting the data to a byte array which I then set as the window icon using the WindowIcon method.

Although this did set the icon to something, it doesn't look anything like it should; it should be a comical picture of my cat. However, the result was three black horizontal lines on top of a grey and pink background.

...

If anyone could help me it would be greatly appreciated :)

(I'm using Visual Studio 2019 as my IDE with, of course, the language C#, and .NET Framework 5.0)

My code:

        public static byte[] ImageToByteArray(string Icon)
        {
            var image = (Image<Rgba32>)SixLabors.ImageSharp.Image.Load(Configuration.Default, Icon);

            image.Mutate(x => x.Flip(FlipMode.Vertical));

            var pixels = new byte[4 * image.Width * image.Height];
            image.CopyPixelDataTo(pixels);


            return pixels;
        }

        public Game(int width = 1280, int height = 768, string title = "Window") :
            base(
                GameWindowSettings.Default,
                new NativeWindowSettings()
                {
                    Title = title,
                    Size = new Vector2i(width, height),
                    APIVersion = new Version(4, 6),
                    Icon = new WindowIcon(new OpenTK.Windowing.Common.Input.Image(100, 100, ImageToByteArray(@"C:\Users\xenon\Downloads\BobbilyIcon.png")))
                })
        {
            this.CenterWindow();
        }

Sadly, I can't directly include images since I am a new user, so I've attached links to a couple useful images concerning my problem below:

  1. The picture of my cat which I am trying to set as the icon: https://i.sstatic.net/uEMLk.jpg
  2. The unexpected result: https://i.sstatic.net/nvpdz.jpg

Solution

  • Okay, so apparently I'm a bit dumb. Turns out that when I am setting the icon, using the WindowIcon function, the height and width of the icon must match that of the image being used, which makes a lot of sense now that I think about it. I thought it was some incompatibility between the ImageSharp library used to load the image to memory and copy the data to the byte array and OpenTK.