I've got an IntPtr marshaled across an unmanaged/managed boundary that corresponds to an Icon Handle. Converting it to an Icon is trivial via the FromHandle() method, and this was satisfactory until recently.
Basically, I've got enough thread weirdness going on now that the MTA/STA dance I've been playing to keep a hosted WinForm from breaking the primary (WPF-tastic) UI of the application is too brittle to stick with. So the WinForm has got to go.
So, how can I get an ImageSource version of an Icon?
Note, I've tried ImageSourceConverter to no avail.
As an aside, I can get the underlying resource for some but not all of the icons involved and they generally exist outside of my application's assembly (in fact, they often exist in unmanaged dll's).
Simple conversion method without creating any extra objects:
When using disposable streams it is almost always recommended to use 'using' blocks to force correct releasing of resources.
Where
icon
is the source System.Drawing.Icon, andthis.TargetWindow
is the target System.Windows.Window.Somehow similar example, only tuned from developer's use cases...
Taking from some above this has created the highest quality of icons for my self. Loading the icons from a byte array. I use cache onload because if you don't you will get a disposed exception when you dispose the memory stream.
There is a really simple solution to this problem.
Steps:
(1) add image to resources in solution explorer -> resources.resx (2) edit image properties inside "Resources" directory in solution explorer and change "Build action" to "Resource"
In xaml, add the following...
Icon="resources/name of image" (where "name of image" is the name of the image you added to resources - see point (1).