title: Custom Cursor - Colour Issues
body: <p>I'm having a problem where a the colours in a custom cursor are sometimes not displaying correctly.</p>
  
  <p>I have a Windows Forms application and on one of the forms there is a PictureBox control. When an image is loaded into that PictureBox and the mouse is moved over that control I want to display a custom cursor. This cursor is a little unusual in that for each image that's loaded into the PictureBox there's another associated image, and as the user moves the mouse pointer over the PictureBox control I want the cursor to be a square and within that square I want it to display the corresponding pixels from the associated image. Within certain limits, the size of the cursor may be changed by the user.</p>
  
  <p>In principle I've nearly got it to work. Certainly at any given point the cursor is showing the right part of the associated image and it updates properly as I move the pointer around.</p>
  
  <p>The problem is that the colours being displayed in the cursor are not always correct. They're in the right ballpark: a shade of red will always display as a shade of red for instance, just not necessarily the right shade. Sometimes the cursor colours look slightly darker, other times slightly lighter. I've noticed that for any colours where all three of the R, G and B components are a combination of 0 or 255, the colours always look correct.</p>
  
  <p>In addition, this only happens if the cursor is a 64x64 pixel square or smaller. If the user increases the size of the cursor beyond that point the problem disappears, though it will reappear if the cursor size is later reduced back to 64x64 or smaller.</p>
  
  <p>This is the code which does the work. X and Y come from the MouseMove event. CursorSize is a form-wide int variable which holds the size of the cursor in pixels (always an even number) and imgAssoc is a form-wide Image variable into which the associated image was loaded. picMain is the PictureBox control.</p>
  
  <pre><code>    private void LoadCursorFromBitmap(int X, int Y)
      {
          int halfSize = CursorSize / 2;
  
          // The new cursor image is a [CursorSize] x [CursorSize] pixel square.
          using (Bitmap bmp = new Bitmap(CursorSize, CursorSize))
          {
              using (Graphics g = Graphics.FromImage(bmp))
              {
                  // Create a [CursorSize] x [CursorSize] pixel square, centered on the X and Y co-ordinates.
                  Rectangle square = new Rectangle(X - halfSize, Y - halfSize, CursorSize, CursorSize);
  
                  // Copy the square section of the image to the bitmap.
                  g.DrawImage(imgAssoc, 0, 0, square, GraphicsUnit.Pixel);
  
                  // Draw a line around the edge of the bitmap.
                  using (Pen pen = new Pen(Color.FromArgb(64, Color.Black), 1F))
                  {
                      g.DrawRectangle(pen, 0, 0, CursorSize - 1, CursorSize - 1);
                  }
              }
  
              picMain.Cursor = new Cursor(bmp.GetHicon());
          }
      }
  </code></pre>
  
  <p>Any ideas how to solve this?</p>