I have a .cur file path ("%SystemRoot%\cursors\aero_arrow.cur"
) witch I want to display in an image control. So I need to convert Cursor to ImageSource. I tried both CursorConverter and ImageSourceConverter but had no luck. I also tried creating Graphics from the cursor and then converting it to Bitmap, But that didn't work either. Then I found the accepted answer in this thread: http://social.msdn.microsoft.com/Forums/vstudio/en-US/87ee395c-9134-4196-bcd8-3d8e8791ff27/is-there-any-way-convert-cursor-to-icon
Now the funny thing is that I can't create a new instance of System.Windows.Form.Cursor with neither the file path nor the stream since It throws the following exception:
System.Runtime.InteropServices.COMException (0x800A01E1): Exception from HRESULT: 0x800A01E1 (CTL_E_INVALIDPICTURE) at System.Windows.Forms.UnsafeNativeMethods.IPersistStream.Load(IStream pstm) at System.Windows.Forms.Cursor.LoadPicture(IStream stream)
So can anybody tell me the best way to convert System.Windows.Input.Cursor
to ImageSource
?
And what about .ani cursors? If I remember correctly System.Windows.Input.Cursor does not support animated cursors, So how can I show them to the user? Converting them to gif then using the 3d party gif libraries?
I found the solution in this thread: How to Render a Transparent Cursor to Bitmap preserving alpha channel?
So here is the code:
It's written for Win Forms and draws an image. But can be used in wpf as well with referencing to System.Windows.Forms. and then you can convert that bitmap to bitmap source and show it in an image control...
The reason I'm using System.Windows.Forms.Cursor instead of System.Windows.Input.Cursor is that I can't get to create a new instance of cursor using the IntPtr handle...
Edit: The above method does NOT work with cursors having low color bits. An alternative is to use
Icon.ExtractAssociatedIcon
instead:Hope that helps someone ...