internal Cursor CreateCursor(BitmapSource renderBitmap, int width, int height) { // unfortunately, this byte array will get placed on the large object heap more than likely ... byte[] pixels = GetBitmapPixels(renderBitmap, width, height); // -height specifies top-down bitmap BITMAPV5HEADER bInfo = new BITMAPV5HEADER(width, -height, 32); IntPtr ppvBits = IntPtr.Zero; BitmapHandle dibSectionHandle = null; try { dibSectionHandle = new BitmapHandle(CreateDIBSection(IntPtr.Zero, ref bInfo, DIB_RGB_COLORS, out ppvBits, IntPtr.Zero, 0)); // copy the pixels into the DIB section now ... Marshal.Copy(pixels, 0, ppvBits, pixels.Length); if (!dibSectionHandle.IsInvalid && ppvBits != IntPtr.Zero) { return CreateCursor(width, height, dibSectionHandle); } } finally { if (dibSectionHandle != null) { dibSectionHandle.Dispose(); } } return null; }
private Cursor CreateCursor(int width, int height, BitmapHandle dibSectionHandle) { BitmapHandle monoBitmapHandle = null; try { monoBitmapHandle = new BitmapHandle(CreateBitmap(width, height, 1, 1, IntPtr.Zero)); ICONINFO icon = new ICONINFO(); icon.IsIcon = false; icon.xHotspot = 0; icon.yHotspot = 0; icon.ColorBitmap = dibSectionHandle; icon.MaskBitmap = monoBitmapHandle; _iconHandle = CreateIconIndirect(ref icon); if (!_iconHandle.IsInvalid) { return CursorInteropHelper.Create(_iconHandle); } } finally { // destroy the temporary mono bitmap now ... if (monoBitmapHandle != null) { monoBitmapHandle.Dispose(); } } return null; }