views:

323

answers:

2

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?

A: 

Do you need to be able to load the image dynamically, or will you be using the same icon all the time?

If the image isn't determined at runtime then you could always just convert the image manually to produce a .ico file using Microangelo or something similar. Obviously this doesn't help if you actually do need to create the icon on the fly.

TabbyCool
I suppose I'm asking how to create an icon resource in memory on the fly at runtime.
simmotech
A: 

Turns out that the BitmapFrame.Create was correct. Just needs to pass a Uri rather than an existing BitmapSource.

simmotech