Search code examples
wpfimagecanvascursorwpf-controls

Change cursor to image in WPF.


I am trying to implement a "stamping" feature. I am selecting an image from a list, and using it to stamp on the clicked position on my canvas.

I have read several solutions on how to change the cursor, but they involved simply changing the ico texture.

I want to be able to preview in real time what I will be stamping. So if I change the rotation of the stamp, the cursor needs to rotate appropriately, if I scale the stamp the cursor needs to be scaled, and if I switch the stamp the cursor needs to switch.

I tried adding an image to an observablecollection, and binding it to the canvas. Then I tried updating the position, image, scale inside the MouseMove event of the canvas, but it doesnt work.

Here is the MouseMove function:

private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {


            if (currentTool == "staticBrush" && lvDataBinding.SelectedIndex != -1) 
            {
                canvasImages[0].Name = srcImages[lvDataBinding.SelectedIndex].Name;
                canvasImages[0].BmpImage = new BitmapImage(new Uri(canvasImages[0].Name, UriKind.Relative));
                scale(canvasImages[0]);
                canvasImages[0].OffsetX = e.GetPosition(canvasDataBinding).X;
                canvasImages[0].OffsetY = e.GetPosition(canvasDataBinding).Y;
            }
        }

Solution

  • You have two main options... you can either follow a fairly complex tutorial such as WPF Tutorial - How To Use Custom Cursors on TechPro, which should enable you to create a Cursor from just about any WPF UIElement, or you can simply hide the Cursor by setting Cursor = Cursors.None and replacing it with your own Image... of course, with this method, you'd also be responsible for moving the Image wherever the mouse cursor moves.