alexa
alexa

Reputation: 41

How to change the cursor with a local image in wpf?

I'm trying to load a local image and use it for the cursor. In order to do so I have to send a stream from the image to the Cursor(Stream cursorStream) constructor.

    var res = Application.GetResourceStream(new Uri("pack://application:,,,/MyProj;component/Images/delete-icon.png"));

    this.Cursor = new Cursor(res.Stream);

The problem is that I always get this exception

ReadTimeout = (res.Stream).ReadTimeout threw an exception of type 'System.InvalidOperationException'
WriteTimeout = (res.Stream).WriteTimeout threw an exception of type 'System.InvalidOperationException'

How should I do it?

Upvotes: 3

Views: 5094

Answers (1)

Ira
Ira

Reputation: 764

Cursor expects .cur file type. Also make sure the file in resource folder has its build action set to resource

Example: if following folder inside my project has .cur cursor file --> component/Resource/Images/BusyCursor.cur

I use below code and cursor changes for this control.

StreamResourceInfo sri = System.Windows.Application.GetResourceStream(
    new Uri("/<projectname>;component/Resource/Images/BusyCursor.cur", UriKind.RelativeOrAbsolute));

this.Cursor = new Cursor(sri.Stream);

Upvotes: 3

Related Questions