<p>I want to use an image or icon as a custom cursor in WPF app. How can I do that?</p>

[{'answer_id': 46817, 'author': 'palehorse', 'author_id': 312, 'author_profile': 'https://Stackoverflow.com/users/312', 'pm_score': 2, 'selected': False, 'text': '<p>You could try this</p>\n\n<pre><code>&lt;Window Cursor=""C:\\WINDOWS\\Cursors\\dinosaur.ani"" /&gt;\n</code></pre>\n'}, {'answer_id': 49341, 'author': 'rudigrobler', 'author_id': 5147, 'author_profile': 'https://Stackoverflow.com/users/5147', 'pm_score': 1, 'selected': False, 'text': '<p>Also check out Scott Hanselman\'s BabySmash (www.codeplex.com/babysmash). He used a more "brute force" method of hiding the windows cursor and showing his new cursor on a canvas and then moving the cursor to were the "real" cursor would have been </p>\n\n<p>Read more here:\n<a href="http://www.hanselman.com/blog/DeveloperDesigner.aspx" rel="nofollow noreferrer">http://www.hanselman.com/blog/DeveloperDesigner.aspx</a></p>\n'}, {'answer_id': 265989, 'author': 'PeterAllenWebb', 'author_id': 21365, 'author_profile': 'https://Stackoverflow.com/users/21365', 'pm_score': 6, 'selected': True, 'text': '<p>You have two basic options:</p>\n<ol>\n<li>When the mouse cursor is over your control, hide the system cursor by setting <code>this.Cursor = Cursors.None;</code> and draw your own cursor using whatever technique you like. Then, update the position and appearance of your cursor by responding to mouse events. Here are two examples:</li>\n</ol>\n<ul>\n<li><p><a href="https://web.archive.org/web/20080124002441/http://www.xamlog.com/2006/07/17/creating-a-custom-cursor/" rel="nofollow noreferrer">http://www.xamlog.com/2006/07/17/creating-a-custom-cursor/</a></p>\n</li>\n<li><p><a href="https://www.hanselman.com/blog/developer-designer" rel="nofollow noreferrer">http://www.hanselman.com/blog/DeveloperDesigner.aspx</a><br/><br/>\nAdditional examples can be found here:</p>\n</li>\n<li><p><a href="https://web.archive.org/web/20150504171203/http://tech.pro:80/tutorial/751/wpf-tutorial-how-to-use-custom-cursors" rel="nofollow noreferrer">WPF Tutorial - How To Use Custom Cursors</a></p>\n</li>\n<li><p><a href="https://web.archive.org/web/20210417173605/https://wpf.2000things.com/2012/12/18/714-setting-the-cursor-to-render-some-text-while-dragging/" rel="nofollow noreferrer">Setting the Cursor to Render Some Text While Dragging</a></p>\n</li>\n<li><p><a href="https://web.archive.org/web/20151112155029/http://blogs.msdn.com/b/jaimer/archive/2007/07/12/drag-drop-in-wpf-explained-end-to-end.aspx" rel="nofollow noreferrer">Getting fancy and using the Visual we are dragging for feedback [instead of a cursor]</a></p>\n</li>\n<li><p><a href="https://github.com/bstollnitz/old-wpf-blog/tree/master/46-DragDropListBox" rel="nofollow noreferrer">How can I drag and drop items between data bound ItemsControls?</a><br/><br/></p>\n</li>\n</ul>\n<ol start="2">\n<li>Create a new Cursor object by loading an image from a .cur or .ani file. You can create and edit these kinds of files in Visual Studio. There are also some free utilites floating around for dealing with them. Basically they\'re images (or animated images) which specify a &quot;hot spot&quot; indicating what point in the image the cursor is positioned at.</li>\n</ol>\n<p>If you choose to load from a file, note that you need an absolute file-system path to use the <code>Cursor(string fileName)</code> constructor. Lamely, <em>a relative path or Pack URI will not work.</em> If you need to load the cursor from a relative path or from a resource packed with your assembly, you will need to get a stream from the file and pass it in to the <code>Cursor(Stream cursorStream)</code> constructor. Annoying but true.</p>\n<p>On the other hand, specifying a cursor as a relative path when loading it using a XAML attribute <em>does</em> work, a fact you could use to get your cursor loaded onto a hidden control and then copy the reference to use on another control. I haven\'t tried it, but it should work.</p>\n'}, {'answer_id': 410517, 'author': 'Ben McIntosh', 'author_id': 50859, 'author_profile': 'https://Stackoverflow.com/users/50859', 'pm_score': 5, 'selected': False, 'text': '<p>Like <a href="https://stackoverflow.com/a/265989/3195477">Peter mentioned</a>, if you already have a .cur file, you can use it as an embedded resource by creating a dummy element in the resource section, and then referencing the dummy\'s cursor when you need it.</p>\n<p>For example, say you wanted to display non-standard cursors depending on the selected tool.</p>\n<p>Add to resources:</p>\n<pre><code>&lt;Window.Resources&gt;\n    &lt;ResourceDictionary&gt;\n        &lt;TextBlock x:Key=&quot;CursorGrab&quot; Cursor=&quot;Resources/Cursors/grab.cur&quot;/&gt;\n        &lt;TextBlock x:Key=&quot;CursorMagnify&quot; Cursor=&quot;Resources/Cursors/magnify.cur&quot;/&gt;\n    &lt;/ResourceDictionary&gt;\n&lt;/Window.Resources&gt;\n</code></pre>\n<p>Example of embedded cursor referenced in code:</p>\n<pre><code>if (selectedTool == &quot;Hand&quot;)\n    myCanvas.Cursor = ((TextBlock)this.Resources[&quot;CursorGrab&quot;]).Cursor;\nelse if (selectedTool == &quot;Magnify&quot;)\n    myCanvas.Cursor = ((TextBlock)this.Resources[&quot;CursorMagnify&quot;]).Cursor;\nelse\n    myCanvas.Cursor = Cursor.Arrow;\n</code></pre>\n'}, {'answer_id': 2837158, 'author': 'Ray Burns', 'author_id': 199245, 'author_profile': 'https://Stackoverflow.com/users/199245', 'pm_score': 4, 'selected': False, 'text': '<p>There is an easier way than managing the cursor display yourself or using Visual Studio to construct lots of custom cursors.</p>\n\n<p>If you have a FrameworkElement you can construct a Cursor from it using the following code:</p>\n\n<pre><code>public Cursor ConvertToCursor(FrameworkElement visual, Point hotSpot)\n{\n  int width = (int)visual.Width;\n  int height = (int)visual.Height;\n\n  // Render to a bitmap\n  var bitmapSource = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);\n  bitmapSource.Render(visual);\n\n  // Convert to System.Drawing.Bitmap\n  var pixels = new int[width*height];\n  bitmapSource.CopyPixels(pixels, width, 0);\n  var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);\n  for(int y=0; y&lt;height; y++)\n    for(int x=0; x&lt;width; x++)\n      bitmap.SetPixel(x, y, Color.FromArgb(pixels[y*width+x]));\n\n  // Save to .ico format\n  var stream = new MemoryStream();\n  System.Drawing.Icon.FromHandle(resultBitmap.GetHicon()).Save(stream);\n\n  // Convert saved file into .cur format\n  stream.Seek(2, SeekOrigin.Begin);\n  stream.WriteByte(2);\n  stream.Seek(10, SeekOrigin.Begin);\n  stream.WriteByte((byte)(int)(hotSpot.X * width));\n  stream.WriteByte((byte)(int)(hotSpot.Y * height));\n  stream.Seek(0, SeekOrigin.Begin);\n\n  // Construct Cursor\n  return new Cursor(stream);\n}\n</code></pre>\n\n<p>Note that your FrameworkElement \'s size must be a standard cursor size (eg 16x16 or 32x32), for example:</p>\n\n<pre><code>&lt;Grid x:Name="customCursor" Width="32" Height="32"&gt;\n  ...\n&lt;/Grid&gt;\n</code></pre>\n\n<p>It would be used like this:</p>\n\n<pre><code>someControl.Cursor = ConvertToCursor(customCursor, new Point(0.5, 0.5));\n</code></pre>\n\n<p>Obviously your FrameworkElement could be an <code>&lt;Image&gt;</code> control if you have an existing image, or you can draw anything you like using WPF\'s built-in drawing tools.</p>\n\n<p>Note that details on the .cur file format can be found at <a href="http://en.wikipedia.org/wiki/ICO_(icon_image_file_format)" rel="noreferrer">ICO (file format)</a>.</p>\n'}, {'answer_id': 8346911, 'author': 'paulmcguinness', 'author_id': 1076136, 'author_profile': 'https://Stackoverflow.com/users/1076136', 'pm_score': 3, 'selected': False, 'text': "<p>I wanted to load a custom cursor file from the project resources and ran into similar problems. I searched the internet for a solution and didn't find what I needed: to set the <code>this.Cursor</code> to a custom cursor stored in my resources folder in my project at runtime.\nI've tried Ben's xaml solution, but didn't find it elegant enough.\nPeterAllen stated:</p>\n<blockquote>\n<p>Lamely, a relative path or Pack URI will not work. If you need to load the cursor from a relative path or from a resource packed with your assembly, you will need to get a stream from the file and pass it in to the Cursor(Stream cursorStream) constructor. Annoying but true.</p>\n</blockquote>\n<p>I stumbled on a nice way to do this and resolves my problem:</p>\n<pre><code>    System.Windows.Resources.StreamResourceInfo info = \n        Application.GetResourceStream(new \n        Uri(&quot;/MainApp;component/Resources/HandDown.cur&quot;, UriKind.Relative));\n\n    this.Cursor = new System.Windows.Input.Cursor(info.Stream); \n</code></pre>\n<p><code>MainApp</code> should be replaced with the name of your application. <code>Resources</code> should be replaced with the relative folder path to your *.cur files inside your project.</p>\n"}, {'answer_id': 8715560, 'author': 'Greg', 'author_id': 555993, 'author_profile': 'https://Stackoverflow.com/users/555993', 'pm_score': 3, 'selected': False, 'text': '<p>A very easy way is to create the cursor within Visual Studio as a .cur file, and then add that to the projects Resources.</p>\n\n<p>Then just add the following code when you want to assign the cursor:</p>\n\n<pre><code>myCanvas.Cursor = new Cursor(new System.IO.MemoryStream(myNamespace.Properties.Resources.Cursor1));\n</code></pre>\n'}, {'answer_id': 13395359, 'author': 'Matthias', 'author_id': 1739297, 'author_profile': 'https://Stackoverflow.com/users/1739297', 'pm_score': 1, 'selected': False, 'text': "<p>Make sure, that any GDI resource (for example bmp.GetHIcon) gets disposed. Otherwise you end up with a memory leak. The following code (extension method for icon) works perfectly for WPF. It creates the arrow cursor with a small icon on it's lower right.</p>\n<p>Remark: This code uses an icon to create the cursor. It does not use a current UI control.</p>\n<pre><code>    public static Cursor CreateCursor(this Icon icon, bool includeCrossHair, System.Drawing.Color crossHairColor)\n    {\n        if (icon == null)\n            return Cursors.Arrow;\n\n        // create an empty image\n        int width = icon.Width;\n        int height = icon.Height;\n\n        using (var cursor = new Bitmap(width * 2, height * 2))\n        {\n            // create a graphics context, so that we can draw our own cursor\n            using (var gr = System.Drawing.Graphics.FromImage(cursor))\n            {\n                // a cursor is usually 32x32 pixel so we need our icon in the lower right part of it\n                gr.DrawIcon(icon, new Rectangle(width, height, width, height));\n\n                if (includeCrossHair)\n                {\n                    using (var pen = new System.Drawing.Pen(crossHairColor))\n                    {\n                        // draw the cross-hair\n                        gr.DrawLine(pen, width - 3, height, width + 3, height);\n                        gr.DrawLine(pen, width, height - 3, width, height + 3);\n                    }\n                }\n            }\n\n            try\n            {\n                using (var stream = new MemoryStream())\n                {\n                    // Save to .ico format\n                    var ptr = cursor.GetHicon();\n                    var tempIcon = Icon.FromHandle(ptr);\n                    tempIcon.Save(stream);\n\n                    int x = cursor.Width/2;\n                    int y = cursor.Height/2;\n\n                    #region Convert saved stream into .cur format\n\n                    // set as .cur file format\n                    stream.Seek(2, SeekOrigin.Begin);\n                    stream.WriteByte(2);\n\n                    // write the hotspot information\n                    stream.Seek(10, SeekOrigin.Begin);\n                    stream.WriteByte((byte)(width));\n                    stream.Seek(12, SeekOrigin.Begin);\n                    stream.WriteByte((byte)(height));\n                    \n                    // reset to initial position\n                    stream.Seek(0, SeekOrigin.Begin);\n\n                    #endregion\n\n\n                    DestroyIcon(tempIcon.Handle);  // destroy GDI resource\n\n                    return new Cursor(stream);\n                }\n            }\n            catch (Exception)\n            {\n                return Cursors.Arrow;\n            }\n        }\n    }\n\n    /// &lt;summary&gt;\n    /// Destroys the icon.\n    /// &lt;/summary&gt;\n    /// &lt;param name=&quot;handle&quot;&gt;The handle.&lt;/param&gt;\n    /// &lt;returns&gt;&lt;/returns&gt;\n    [DllImport(&quot;user32.dll&quot;, CharSet = CharSet.Auto)]\n    public extern static Boolean DestroyIcon(IntPtr handle);\n</code></pre>\n"}, {'answer_id': 16560633, 'author': 'kkCosmo', 'author_id': 1374375, 'author_profile': 'https://Stackoverflow.com/users/1374375', 'pm_score': 4, 'selected': False, 'text': '<p>To use a custom cursor in XAML I altered the <a href="https://stackoverflow.com/a/410517">code Ben McIntosh provided</a> slightly:</p>\n<pre><code>&lt;Window.Resources&gt;    \n &lt;Cursor x:Key=&quot;OpenHandCursor&quot;&gt;Resources/openhand.cur&lt;/Cursor&gt;\n&lt;/Window.Resources&gt;\n</code></pre>\n<p>To use the cursor just reference to the resource:</p>\n<pre><code>&lt;StackPanel Cursor=&quot;{StaticResource OpenHandCursor}&quot; /&gt;\n</code></pre>\n'}, {'answer_id': 18268280, 'author': 'Alex Cube', 'author_id': 1764208, 'author_profile': 'https://Stackoverflow.com/users/1764208', 'pm_score': 1, 'selected': False, 'text': '<p>If you are using visual studio, you can </p>\n\n<ol>\n<li>New a cursor file</li>\n<li>Copy/Paste the image</li>\n<li>Save it to .cur file.</li>\n</ol>\n'}, {'answer_id': 24246960, 'author': 'Gábor', 'author_id': 2076286, 'author_profile': 'https://Stackoverflow.com/users/2076286', 'pm_score': 3, 'selected': False, 'text': '<p>One more solution somewhat similar to Ray\'s but instead of slow and cumbersome pixel copying, this uses some Windows internals:</p>\n\n<pre><code>private struct IconInfo {\n  public bool fIcon;\n  public int xHotspot;\n  public int yHotspot;\n  public IntPtr hbmMask;\n  public IntPtr hbmColor;\n}\n\n[DllImport("user32.dll")]\nprivate static extern IntPtr CreateIconIndirect(ref IconInfo icon);\n\n[DllImport("user32.dll")]\n[return: MarshalAs(UnmanagedType.Bool)]\nprivate static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);\n\npublic Cursor ConvertToCursor(FrameworkElement cursor, Point HotSpot) {\n  cursor.Arrange(new Rect(new Size(cursor.Width, cursor.Height)));\n  var bitmap = new RenderTargetBitmap((int)cursor.Width, (int)cursor.Height, 96, 96, PixelFormats.Pbgra32);\n  bitmap.Render(cursor);\n\n  var info = new IconInfo();\n  GetIconInfo(bitmap.ToBitmap().GetHicon(), ref info);\n  info.fIcon = false;\n  info.xHotspot = (byte)(HotSpot.X * cursor.Width);\n  info.yHotspot = (byte)(HotSpot.Y * cursor.Height);\n\n  return CursorInteropHelper.Create(new SafeFileHandle(CreateIconIndirect(ref info), true));\n}\n</code></pre>\n\n<p>There is an extension method in the middle that I prefer to have in an extension class for such cases:</p>\n\n<pre><code>using DW = System.Drawing;\n\npublic static DW.Bitmap ToBitmap(this BitmapSource bitmapSource) {\n  var bitmap = new DW.Bitmap(bitmapSource.PixelWidth, bitmapSource.PixelHeight, DW.Imaging.PixelFormat.Format32bppPArgb);\n  var data = bitmap.LockBits(new DW.Rectangle(DW.Point.Empty, bitmap.Size), DW.Imaging.ImageLockMode.WriteOnly, DW.Imaging.PixelFormat.Format32bppPArgb);\n  bitmapSource.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);\n  bitmap.UnlockBits(data);\n  return bitmap;\n}\n</code></pre>\n\n<p>With all this, it\'s rather simple and straightforward.</p>\n\n<p>And, if you happen not to need to specify your own hotspot, you can even cut this shorter (you don\'t need the struct or the P/Invokes, either):</p>\n\n<pre><code>public Cursor ConvertToCursor(FrameworkElement cursor, Point HotSpot) {\n  cursor.Arrange(new Rect(new Size(cursor.Width, cursor.Height)));\n  var bitmap = new RenderTargetBitmap((int)cursor.Width, (int)cursor.Height, 96, 96, PixelFormats.Pbgra32);\n  bitmap.Render(cursor);\n  var icon = System.Drawing.Icon.FromHandle(bitmap.ToBitmap().GetHicon());\n  return CursorInteropHelper.Create(new SafeFileHandle(icon.Handle, true));\n}\n</code></pre>\n'}, {'answer_id': 27077188, 'author': 'gjacobs', 'author_id': 3560034, 'author_profile': 'https://Stackoverflow.com/users/3560034', 'pm_score': 4, 'selected': False, 'text': '<p>In case anyone is looking for a UIElement itself as a cursor, I combined the solutions of <a href="https://stackoverflow.com/questions/46805/custom-cursor-in-wpf#2837158">Ray</a> and <a href="https://stackoverflow.com/questions/11630071/convert-an-xaml-file-to-a-bitmapimage#11631235">Arcturus</a>:</p>\n\n<pre class="lang-cs prettyprint-override"><code>    public Cursor ConvertToCursor(UIElement control, Point hotSpot)\n    {\n        // convert FrameworkElement to PNG stream\n        var pngStream = new MemoryStream();\n        control.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));\n        Rect rect = new Rect(0, 0, control.DesiredSize.Width, control.DesiredSize.Height);\n        RenderTargetBitmap rtb = new RenderTargetBitmap((int)control.DesiredSize.Width, (int)control.DesiredSize.Height, 96, 96, PixelFormats.Pbgra32);\n\n        control.Arrange(rect);\n        rtb.Render(control);\n\n        PngBitmapEncoder png = new PngBitmapEncoder();\n        png.Frames.Add(BitmapFrame.Create(rtb));\n        png.Save(pngStream);\n\n        // write cursor header info\n        var cursorStream = new MemoryStream();\n        cursorStream.Write(new byte[2] { 0x00, 0x00 }, 0, 2);                               // ICONDIR: Reserved. Must always be 0.\n        cursorStream.Write(new byte[2] { 0x02, 0x00 }, 0, 2);                               // ICONDIR: Specifies image type: 1 for icon (.ICO) image, 2 for cursor (.CUR) image. Other values are invalid\n        cursorStream.Write(new byte[2] { 0x01, 0x00 }, 0, 2);                               // ICONDIR: Specifies number of images in the file.\n        cursorStream.Write(new byte[1] { (byte)control.DesiredSize.Width }, 0, 1);          // ICONDIRENTRY: Specifies image width in pixels. Can be any number between 0 and 255. Value 0 means image width is 256 pixels.\n        cursorStream.Write(new byte[1] { (byte)control.DesiredSize.Height }, 0, 1);         // ICONDIRENTRY: Specifies image height in pixels. Can be any number between 0 and 255. Value 0 means image height is 256 pixels.\n        cursorStream.Write(new byte[1] { 0x00 }, 0, 1);                                     // ICONDIRENTRY: Specifies number of colors in the color palette. Should be 0 if the image does not use a color palette.\n        cursorStream.Write(new byte[1] { 0x00 }, 0, 1);                                     // ICONDIRENTRY: Reserved. Should be 0.\n        cursorStream.Write(new byte[2] { (byte)hotSpot.X, 0x00 }, 0, 2);                    // ICONDIRENTRY: Specifies the horizontal coordinates of the hotspot in number of pixels from the left.\n        cursorStream.Write(new byte[2] { (byte)hotSpot.Y, 0x00 }, 0, 2);                    // ICONDIRENTRY: Specifies the vertical coordinates of the hotspot in number of pixels from the top.\n        cursorStream.Write(new byte[4] {                                                    // ICONDIRENTRY: Specifies the size of the image\'s data in bytes\n                                          (byte)((pngStream.Length &amp; 0x000000FF)),\n                                          (byte)((pngStream.Length &amp; 0x0000FF00) &gt;&gt; 0x08),\n                                          (byte)((pngStream.Length &amp; 0x00FF0000) &gt;&gt; 0x10),\n                                          (byte)((pngStream.Length &amp; 0xFF000000) &gt;&gt; 0x18)\n                                       }, 0, 4);\n        cursorStream.Write(new byte[4] {                                                    // ICONDIRENTRY: Specifies the offset of BMP or PNG data from the beginning of the ICO/CUR file\n                                          (byte)0x16,\n                                          (byte)0x00,\n                                          (byte)0x00,\n                                          (byte)0x00,\n                                       }, 0, 4);\n\n        // copy PNG stream to cursor stream\n        pngStream.Seek(0, SeekOrigin.Begin);\n        pngStream.CopyTo(cursorStream);\n\n        // return cursor stream\n        cursorStream.Seek(0, SeekOrigin.Begin);\n        return new Cursor(cursorStream);\n    }\n</code></pre>\n'}, {'answer_id': 43604577, 'author': 'Ahmad', 'author_id': 6631692, 'author_profile': 'https://Stackoverflow.com/users/6631692', 'pm_score': 2, 'selected': False, 'text': '<p>you can do this by Code like </p>\n\n<pre><code>this.Cursor = new Cursor(@"&lt;your address of icon&gt;");\n</code></pre>\n'}, {'answer_id': 44100957, 'author': 'Steve Miller', 'author_id': 2079118, 'author_profile': 'https://Stackoverflow.com/users/2079118', 'pm_score': 1, 'selected': False, 'text': '<p>It may have changed with Visual Studio 2017 but I was able to reference a .cur file as an embedded resource:</p>\n\n<pre><code>&lt;Setter\n    Property="Cursor"\n    Value="/assembly-name;component/location-name/curser-name.cur" /&gt;\n</code></pre>\n'}, {'answer_id': 69804048, 'author': 'Imagin', 'author_id': 17283045, 'author_profile': 'https://Stackoverflow.com/users/17283045', 'pm_score': 0, 'selected': False, 'text': "<p>This will convert any image stored in your project to a cursor using an attached property. The image must be compiled as a resource!</p>\n<p><strong>Example</strong></p>\n<pre><code>&lt;Button MyLibrary:FrameworkElementExtensions.Cursor=&quot;&quot;{MyLibrary:Uri MyAssembly, MyImageFolder/MyImage.png}&quot;&quot;/&gt;\n</code></pre>\n<p><em>FrameworkElementExtensions</em></p>\n<pre><code>using System;\nusing System.Windows;\nusing System.Windows.Media;\n\npublic static class FrameworkElementExtensions\n{\n    #region Cursor\n\n    public static readonly DependencyProperty CursorProperty = DependencyProperty.RegisterAttached(&quot;Cursor&quot;, typeof(Uri), typeof(FrameworkElementExtensions), new UIPropertyMetadata(default(Uri), OnCursorChanged));\n    public static Uri GetCursor(FrameworkElement i) =&gt; (Uri)i.GetValue(CursorProperty);\n    public static void SetCursor(FrameworkElement i, Uri input) =&gt; i.SetValue(CursorProperty, input);\n    static void OnCursorChanged(object sender, DependencyPropertyChangedEventArgs e)\n    {\n        if (sender is FrameworkElement frameworkElement)\n        {\n            if (GetCursor(frameworkElement) != null)\n                frameworkElement.Cursor = new ImageSourceConverter().ConvertFromString(((Uri)e.NewValue).OriginalString).As&lt;ImageSource&gt;().Bitmap().Cursor(0, 0).Convert();\n        }\n    }\n\n    #endregion\n}\n</code></pre>\n<p><em>ImageSourceExtensions</em></p>\n<pre><code>using System.Drawing;\nusing System.Windows.Media;\nusing System.Windows.Media.Imaging;\n\npublic static class ImageSourceExtensions\n{\n    public static Bitmap Bitmap(this ImageSource input) =&gt; input.As&lt;BitmapSource&gt;().Bitmap();\n}\n</code></pre>\n<p><em>BitmapSourceExtensions</em></p>\n<pre><code>using System.IO;\nusing System.Windows.Media.Imaging;\n\npublic static class BitmapSourceExtensions\n{\n    public static System.Drawing.Bitmap Bitmap(this BitmapSource input)\n    {\n        if (input == null)\n            return null;\n\n        System.Drawing.Bitmap result;\n        using (var outStream = new MemoryStream())\n        {\n            var encoder = new PngBitmapEncoder();\n            encoder.Frames.Add(BitmapFrame.Create(input));\n            encoder.Save(outStream);\n            result = new System.Drawing.Bitmap(outStream);\n        }\n        return result;\n    }\n}\n</code></pre>\n<p><em>BitmapExtensions</em></p>\n<pre><code>using System;\nusing System.Drawing;\nusing System.Runtime.InteropServices;\n\npublic static class BitmapExtensions\n{\n\n    [StructLayout(LayoutKind.Sequential)]\n    public struct ICONINFO\n    {\n        /// &lt;summary&gt;\n        /// Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies an icon; FALSE specifies a cursor. \n        /// &lt;/summary&gt;\n        public bool fIcon;\n\n        /// &lt;summary&gt;\n        /// Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot spot is always in the center of the icon, and this member is ignored.\n        /// &lt;/summary&gt;\n        public Int32 xHotspot;\n\n        /// &lt;summary&gt;\n        /// Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot spot is always in the center of the icon, and this member is ignored. \n        /// &lt;/summary&gt;\n        public Int32 yHotspot;\n\n        /// &lt;summary&gt;\n        /// (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon, this bitmask is formatted so that the upper half is the icon AND bitmask and the lower half is the icon XOR bitmask. Under this condition, the height should be an even multiple of two. If this structure defines a color icon, this mask only defines the AND bitmask of the icon. \n        /// &lt;/summary&gt;\n        public IntPtr hbmMask;\n\n        /// &lt;summary&gt;\n        /// (HBITMAP) Handle to the icon color bitmap. This member can be optional if this structure defines a black and white icon. The AND bitmask of hbmMask is applied with the SRCAND flag to the destination; subsequently, the color bitmap is applied (using XOR) to the destination by using the SRCINVERT flag. \n        /// &lt;/summary&gt;\n        public IntPtr hbmColor;\n    }\n\n    [DllImport(&quot;user32.dll&quot;)]\n    static extern IntPtr CreateIconIndirect([In] ref ICONINFO piconinfo);\n\n    [DllImport(&quot;user32.dll&quot;)]\n    static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);\n\n    [DllImport(&quot;user32.dll&quot;, SetLastError = true)]\n    public static extern bool DestroyIcon(IntPtr hIcon);\n\n    public static System.Windows.Forms.Cursor Cursor(this Bitmap input, int hotX, int hotY)\n    {\n        ICONINFO Info = new ICONINFO();\n        IntPtr Handle = input.GetHicon();\n        GetIconInfo(Handle, out Info);\n\n        Info.xHotspot = hotX;\n        Info.yHotspot = hotY;\n        Info.fIcon = false;\n\n        IntPtr h = CreateIconIndirect(ref Info);\n        return new System.Windows.Forms.Cursor(h);\n    }\n}\n</code></pre>\n<p><em>CursorExtensions</em></p>\n<pre><code>using Microsoft.Win32.SafeHandles;\n\npublic static class CursorExtensions\n{\n    public static System.Windows.Input.Cursor Convert(this System.Windows.Forms.Cursor Cursor)\n    {\n        SafeFileHandle h = new SafeFileHandle(Cursor.Handle, false);\n        return System.Windows.Interop.CursorInteropHelper.Create(h);\n    }\n}\n</code></pre>\n<p><em>As</em></p>\n<pre><code>public static Type As&lt;Type&gt;(this object input) =&gt; input is Type ? (Type)input : default;\n</code></pre>\n<p><em>Uri</em></p>\n<pre><code>using System;\nusing System.Windows.Markup;\n\npublic class Uri : MarkupExtension\n{\n    public string Assembly { get; set; } = null;\n\n    public string RelativePath { get; set; }\n\n    public Uri(string relativePath) : base()\n    {\n        RelativePath = relativePath;\n    }\n\n    public Uri(string assembly, string relativePath) : this(relativePath)\n    {\n        Assembly = assembly;\n    }\n\n    static Uri Get(string assemblyName, string relativePath) =&gt; new Uri($&quot;pack://application:,,,/{assemblyName};component/{relativePath}&quot;, UriKind.Absolute);\n\n    public override object ProvideValue(IServiceProvider serviceProvider)\n    {\n        if (Assembly == null)\n            return new System.Uri(RelativePath, UriKind.Relative);\n\n        return Get(Assembly, RelativePath);\n    }\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/46805', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1133/']