dotMorten / WinUIEx

WinUI Extensions
https://dotmorten.github.io/WinUIEx
MIT License
602 stars 38 forks source link

Using TrayIcon with ShellFile Thumbnails causes System.ExecutionEngineException #35

Closed kurmachu closed 2 years ago

kurmachu commented 2 years ago

When using a TrayIcon with the thumbnails gathered from https://github.com/contre/Windows-API-Code-Pack-1.1, closing a window causes System.ExecutionEngineException. Interestingly, loading the image itself is not enough, it only occurs when the image is displayed and then closed.

I'm setting up a tray icon like this:

Icon icon = Icon.FromFile("(Path to the file)");
tray = new TrayIcon(icon, "(Name)");
tray.TrayIconLeftMouseDown += Tray_TrayIconLeftMouseDown;

And loading the icons into an image via binding and:

public BitmapImage Image { //Using(?) https://stackoverflow.com/questions/26260654/wpf-converting-bitmap-to-imagesource
            get
            {
                if (_Image == null)
                {
                    ShellFile shellFile = ShellFile.FromFilePath(Path);
                    Bitmap bitmap = shellFile.Thumbnail.SmallBitmap;

                    //bitmap = (Bitmap)Bitmap.FromFile("(path to a file)"); //If you do this, the issue no longer occurs, even if you still load the bitmap from the line above.

                    MemoryStream ms = new MemoryStream();
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

                    bitmap.Dispose();
                    shellFile.Dispose();

                    ms.Seek(0, SeekOrigin.Begin);  //Magic line I randomly guessed.

                    _Image = new BitmapImage();
                    _Image.ImageFailed += (s, e) =>
                    {
                        Debug.WriteLine(e.ErrorMessage);
                    };
                    _Image.SetSourceAsync(ms.AsRandomAccessStream()).Completed += (s, e) =>
                    {
                        ms.Dispose();
                    };
                }
                return _Image;
            } 
        }

The issue only occurs if both the shell icons and the tray icon are used, there is no issue if only one is used.

I understand that this is probably not a fault of this project, but any help would be appreciated.

kurmachu commented 2 years ago

Update: the following code does cause a crash so I guess it has to do with saving?

public BitmapImage Image { //Using(?) https://stackoverflow.com/questions/26260654/wpf-converting-bitmap-to-imagesource
            get
            {
                if (_Image == null)
                {
                    ShellFile shellFile = ShellFile.FromFilePath(Path);
                    Bitmap bitmap = shellFile.Thumbnail.LargeBitmap;

                    MemoryStream ms = new MemoryStream();
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); //This causes the issue???

                    bitmap.Dispose();
                    ms.Dispose();

                    bitmap = (Bitmap)Bitmap.FromFile("Assets/ic_programnew.ico");

                    ms = new MemoryStream();
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

                    bitmap.Dispose();
                    shellFile.Dispose();

                    ms.Seek(0, SeekOrigin.Begin); 

                    _Image = new BitmapImage();
                    _Image.ImageFailed += (s, e) =>
                    {
                        Debug.WriteLine(e.ErrorMessage);
                    };
                    _Image.SetSourceAsync(ms.AsRandomAccessStream()).Completed += (s, e) =>
                    {
                        ms.Dispose();
                    };
                }
                return _Image;
            } 
        }

I am still creating the tray icon in the same method as the original comment.

dotMorten commented 2 years ago

I'm not sure what ShellFile is or why issues with saving a shellfile to a bitmap is logged here? (since none of those APIs are WinUIEx APIs). To set the trayicon you must use an .ico file, so don't really get the relevancy with the Bitmap format you're using either?