How to set a specific Bitmap as cursor ?

I am needing to set a specific bitmap as cursor during run-time. this bitmap is not static, I mean, this bitmap could be any image depending how the user will interact with my application.
i only need to use this custom cursor over my PictureBox.

Ho can I do that?
jr_barros_jrAsked:
Who is Participating?
 
nffvrxqgrcfqvvcConnect With a Mentor Commented:
Example using a bitmap that was added as an existing resource file. Set Build Action on the resource to Embedded Resource.
Dim b As Bitmap = My.Resources.cd
PictureBox1.Cursor = New Cursor(b.GetHicon)

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Be sure to store the IntPtr returned by GetHicon() and release it with the DestroyIcon() API before you go using GetHicon() again.  Otherwise you'll end up with an eventual GDI leak:

    <DllImport("user32", CallingConvention:=CallingConvention.Cdecl)> _
    Private Shared Function DestroyIcon(ByVal hIcon As IntPtr) As Boolean
    End Function

    ...
        Static hIcon As IntPtr
        If Not hIcon.Equals(IntPtr.Zero) Then
            DestroyIcon(hIcon)
        ENd If
        Dim b As Bitmap = ~some dynamic bitmap~
        hIcon = b.GetHicon()
        PictureBox1.Cursor = New Cursor(hIcon)
0
 
jr_barros_jrAuthor Commented:
Thank you so much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.