Convert WPF BitmapSource to Icon for window

Posted by simmotech on Stack Overflow See other posts from Stack Overflow or by simmotech
Published on 2010-03-22T08:26:13Z Indexed on 2010/03/22 8:31 UTC
Read the original article Hit count: 975

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about window