Convert WPF BitmapSource to Icon for window
- by simmotech
I have a 16x16 .png file which I have loaded as an ImageSource (BitmapSource) and it is working fine when I use it on an Image in a tabcontrol header.
I now want to use that same image in a floating window (inherited from the WPF Window class) when the user drags the document tab. (This is AvalonDock which I have tweaked to allow images in the tab header)
After many searches on the web, I understand that Window.Icon requires a BitmapFrame but all the sample code seems to assume that a .ico file is available which it isn't in my case.
I have tried the following code (plus variants including cloning, freezing etc):
var image = (Image) content.Icon;
var bitmapSource = (BitmapSource) image.Source;
Icon = BitmapFrame.Create(bitmapSource);
but when the Show() method is called, an exception is thrown: "Exception of type 'System.ExecutionEngineException' was thrown."
How can a I create a compatible bitmap on the fly to allow the Window to display the icon?