Skip to content

Setting IconSource doesn't convert png to icon #208

@Giorgi

Description

@Giorgi

In my project I have two png icons that I want to use as an icon depending on the same. I use this code to set the Icon but it throws this error:

string iconPath = isDarkMode
    ? "pack://application:,,,/dark.png"
    : "pack://application:,,,/light.png";

TrayIcon.IconSource = new BitmapImage(new(iconPath));

System.ArgumentException: Argument 'picture' must be a picture that can be used as a Icon.
at System.Drawing.Icon.Initialize(Int32 width, Int32 height)
at System.Drawing.Icon..ctor(Stream stream, Int32 width, Int32 height)
at System.Drawing.Icon..ctor(Stream stream, Size size)
at H.NotifyIcon.StreamExtensions.ToSmallIcon(Stream stream) in //src/libs/H.NotifyIcon.Shared/Utilities/System.Drawing/StreamExtensions.cs:line 12
at H.NotifyIcon.ImageExtensions.ToIconAsync(ImageSource imageSource, CancellationToken cancellationToken) in /
/src/libs/H.NotifyIcon.Shared/Utilities/ImageExtensions.cs:line 54
at H.NotifyIcon.TaskbarIcon.d__178.MoveNext() in /_/src/libs/H.NotifyIcon.Shared/TaskbarIcon.IconSource.cs:line 19

As a workaround, I use the following code: TrayIcon.IconSource = new GeneratedIconSource {BackgroundSource = new BitmapImage(new (iconPath))};

I see that there is PngToIcoConverter class (but it is internal) that is used by the ImageExtensions.ToStream method but not by the async overload. Can you either make those classes public or should we use this workaround for setting png as an Icon?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions