Skip to content
New issue

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

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

Already on GitHub? # to your account

Setting NotifyIcon.Icon can cause System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+. #1313

Open
soleon opened this issue Jan 13, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@soleon
Copy link

soleon commented Jan 13, 2025

Describe the bug

I have an app that sits in the background and updates the notify icon periodically. This app runs into the following GDI+ exception after running an arbitrary amount of time:

System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
   at Windows.Win32.Graphics.GdiPlus.StatusExtensions.ThrowIfFailed(Status status)
   at System.Drawing.Bitmap.GetHicon()
   at Wpf.Ui.Tray.Hicon.FromSource(ImageSource source)
   at Wpf.Ui.Tray.TrayManager.ReloadHicon(INotifyIcon notifyIcon)
   at Wpf.Ui.Tray.TrayManager.ModifyIcon(INotifyIcon notifyIcon)
   at Wpf.Ui.Tray.Internal.InternalNotifyIconManager.ModifyIcon()
   at Wpf.Ui.Tray.Controls.NotifyIcon.OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Wpf.Ui.Tray.Controls.NotifyIcon.set_Icon(ImageSource value)

To Reproduce

Sample code to reproduce the issue:

XAML

<Window x:Class="Percentage.App.NotifyIconWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tray="http://schemas.lepo.co/wpfui/2022/xaml/tray">
    <tray:NotifyIcon x:Name="NotifyIcon" />
</Window>

C#

for (var i = 0; i < 10000; i++)
{
    var bitmap = new WriteableBitmap(1, 1, 96, 96, PixelFormats.Pbgra32, null);
    bitmap.WritePixels(new Int32Rect(0, 0, 0, 0), new byte[]{0}, 0, 0);
    bitmap.Freeze();

    NotifyIcon.Icon = bitmap;
}

The error would occur when the loop is running up to around 4000.

Expected behavior

Changing the notify icon shouldn't cause the GDI+ exception over time.

Screenshots

No response

OS version

Windows 11 24H2 26100.2454

.NET version

.NET 9.0

WPF-UI NuGet version

WPF-UI.Tray 3.0.5

Additional context

No response

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

No branches or pull requests

1 participant