Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The icon isn't displayed correctly in the title bar #111

Closed
DonKadaj opened this issue Jul 19, 2020 · 10 comments
Closed

The icon isn't displayed correctly in the title bar #111

DonKadaj opened this issue Jul 19, 2020 · 10 comments
Labels
bug Something isn't working

Comments

@DonKadaj
Copy link

Describe the bug
I use an ico file (which embed several icon sizes) for my application logo. Usually, Windows chooses the best size to display. However, with AdonisUI.Controls.AdonisWindow the size isn't adjusted in the title bar.

Screenshots
The icon displayed from Windows:
image
The icon displayed from AdonisUI.Controls.AdonisWindow (it uses an oversized icon):
image

@DonKadaj DonKadaj added the bug Something isn't working label Jul 19, 2020
@benruehl
Copy link
Owner

Thank you for reporting this issue. And sorry for the late reply.

I tried to reproduce this with different .ico files but couldn't achieve the effect you had. For me, all icons I tried look the same in both window classes.

Would you mind uploading your file here so I can use it for reproduction?

@DonKadaj
Copy link
Author

I sent you an email with my icon. I hope this will be useful to you.

@irusanov
Copy link

irusanov commented Sep 7, 2020

@benruehl I have the same problem.
16x16 icon is picked for titlebar when main windows does not inherit AdonisWindow.
The icon I have includes common sizes and works correctly in Windows Forms and WPF without Adonis UI.
It's not horrible, but the largest size is picked for the titlebar, instead of the 16x16 version.

ICO file attached: ZenTimings.ico.zip

Current result
image

Expected:
image

As a workaround I've commented this line out, recompiled the DLL and defined a separate 16x16 icon for the MainWindow

IconSource = GetApplicationIcon();

That doesn't work, because now the taskbar (when "use small taskbar buttons" is witched off) is using the same 16x16 icon from the MainWindow.

@benruehl
Copy link
Owner

benruehl commented Sep 13, 2020

Ok guys, good and bad news.

Good news:
I can reproduce the issue with your files. Thanks to both of you @DonKadaj @irusanov.

Bad news:
I don't know how to fix it :(

At the moment, I use System.Drawing.Icon.ExtractAssociatedIcon(absolutePathToExecutable) to get the icon that has been specified for the application. Unfortunately, this retrieves the 32x32 version only [Source in comments]. There is no overload or anything to specify a size.

I found a different approach here which looked promising but unfortunately this throws a COMException for me. I couldn't get it to run. My guess is that this might only work with an URI in pack syntax like "pack://application:,,,". Using this syntax is not an option though because Adonis UI does not know the name and location of the icon file. And I don't like the idea that the developer has to specify an icon path for each window.

Last thing I found was some ugly interop magic here. This is a lot of code for such a simple requirement but I tested it anyway. It didn't work though. I cannot say why because there is no exception raised or anything. The rendered icon simply becomes a generic placeholder icon.

Possible workaround:
Even though I don't know how to fix it for the library you might be able to fix it for your cases individually.
Where you have your window class (probably MainWindow) deriving from AdonisWindow you could go to your code-behind file and look for the constructor. AdonisWindow displays its icon via the IconSource property which you can set on your own. The following code solved the issue for me. Make sure the build action of the icon file is set to Content.

public MainWindow()
{
    InitializeComponent();

    // Try to get your icon in the correct size here and assign it.
    IconSource = GetIcon("pack://application:,,,/AdonisUI.Demo;component/ZenTimings.ico", 16);
}

private ImageSource GetIcon(string iconSource, double width)
{
    var decoder = BitmapDecoder.Create(new Uri(iconSource),
        BitmapCreateOptions.DelayCreation,
        BitmapCacheOption.OnDemand);

    var result = decoder.Frames.SingleOrDefault(f => f.Width == width);
    if (result == default(BitmapFrame))
    {
        result = decoder.Frames.OrderBy(f => f.Width).First();
    }

    return result;
}

You can try different methods of loading an icon if you want. I linked some resources that might help.
I'm sorry that this is not as convenient as you might have wished.
Hope this helps anyway.

@irusanov
Copy link

irusanov commented Sep 14, 2020

Yes, that approach works for me. Thanks!
I've tried some of the linked solutions before, but they didn't work for me either.
Was not really keen on that interop code for just a simple icon, so I left it as it is, but will use that last solution you proposed.

PS: For other child windows I'm already using a separate 16x16 optimized PNG file.

@benruehl
Copy link
Owner

Good to hear that this works for you :)

I will close this issue then although the solution does not feel really satisfying to me.

@DonKadaj
Copy link
Author

Thanks @benruehl for all your research!

I found another workaround (if we can call it that), instead of setting the icon in the XAML file, just set it in the app properties: Project > Properties > Application tab.

image

@irusanov
Copy link

irusanov commented Sep 15, 2020

@DonKadaj This is my usual way of setting the application icon, but it did not work - it didn't matter if it is defined in the project properties or in the XAML file. GetApplicationIcon() gets this icon and extracts the 32x32 image from it.

@DonKadaj
Copy link
Author

@irusanov Make sure to remove the Icon="YourIconPath" in your XAML file, because it seems to override the icon set in the project properties. This is the case for me.

@irusanov
Copy link

@DonKadaj That's how I have it from the beginning and the higher resolution of the icon is loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants