Convert a bitmap into a cursor in C#

Converting a bitmap into a cursor isn't easy, but you can do it by using the CreateIconIndirect API function. This example uses the following BitmapToCursor method to create a cursor from a bitmap.

// Create a cursor from a bitmap.
private Cursor BitmapToCursor(Bitmap bmp,
  int hot_x, int hot_y) { // Initialize the cursor information. ICONINFO icon_info = new ICONINFO(); IntPtr h_icon = bmp.GetHicon(); GetIconInfo(h_icon, out icon_info); icon_info.xHotspot = hot_x; icon_info.yHotspot = hot_y; icon_info.fIcon = false; // Cursor, not icon. // Create the cursor. IntPtr h_cursor = CreateIconIndirect(ref icon_info); return new Cursor(h_cursor); }

(See the code for the API declarations.) The code creates an ICONINFO structure to describe the cursor to create. It calls the bitmap's GetHicon to get a handle to an icon that has the same image as the bitmap. It passes that handle to the GetIconInfo API function to get ICONINFO data describing the icon.

Next the code sets the X and Y coordinates of the cursor's hot spot, the position inside the cursor that represents the mouse's position. For example, an arrow cursor would normally use the arrow's tip as the hot spot. The code sets the ICONINFO's fIcon property to False to indicate that this should be a cursor not an icon and it calls the CreateIconIndirect method to create the icon and get a new handle to it. Finally the code passes the handle to the Cursor class's constructor and returns the resulting Cursor object.

Note that these API functions allocate unmanaged memory that is never freed by the program. If the program only creates a few cursors, that's not a problem. If the program could create hundreds of cursors on the fly, this will cause a memory leak that may be a problem. in that case, use the DestroyIcon API function to release cursors that are no longer needed.

The program uses the following code to create the cursor it uses.

private void Form1_Load(object sender, EventArgs e)
{
    // Make pixels that match the one in the upper left corner transparent.
    Bitmap bm = Properties.Resources.Diamond;
    bm.MakeTransparent(bm.GetPixel(0, 0));
    this.Cursor = BitmapToCursor(bm, 7, 7);
}

The program gets the bitmap resource named Diamond and makes the pixels that match the one in its upper left corner transparent. It then calls the BitmapToCursor method and uses the result as the form's cursor.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.