1

我有一个"%SystemRoot%\cursors\aero_arrow.cur"想要在图像控件中显示的 .cur 文件路径 ()。所以我需要将 Cursor 转换为 ImageSource。我尝试了 CursorConverter 和 ImageSourceConverter 但没有运气。我还尝试从光标创建图形,然后将其转换为位图,但这也不起作用。

这个线程说:

将 Cursor 直接转换为 Icon 很复杂,因为 Cursor 不会暴露它使用的图像源。

如果您真的想将图像绑定到光标,您可能想尝试一种方法。

由于 WindowForm 能够绘制光标,我们可以使用 WindowForm 在位图上绘制光标。之后,我们可以找到一种方法将该位图复制到 WPF 支持的东西上。

现在有趣的是,我无法创建一个System.Windows.Forms.Cursor既没有文件路径也没有流的新实例,因为它引发了以下异常:

System.Runtime.InteropServices.COMException (0x800A01E1): 
Exception from HRESULT: 0x800A01E1 (CTL_E_INVALIDPICTURE)    
at System.Windows.Forms.UnsafeNativeMethods.IPersistStream.Load(IStream pstm)   
at System.Windows.Forms.Cursor.LoadPicture(IStream stream)

那么有人可以告诉我转换System.Windows.Input.Cursor为的最佳方法ImageSource吗?

那么.ani 游标呢?如果我没记错的话System.Windows.Input.Cursor不支持动画光标,那么我怎样才能将它们展示给用户呢?将它们转换为 gif,然后使用 3d 派对 gif 库?

4

1 回答 1

4

我在这个线程中找到了解决方案:如何将透明光标渲染到保留 alpha 通道的位图?

所以这里是代码:

[StructLayout(LayoutKind.Sequential)]    
private struct ICONINFO
{
    public bool fIcon;
    public int xHotspot;
    public int yHotspot;
    public IntPtr hbmMask;
    public IntPtr hbmColor;
}

[DllImport("user32")]
private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO pIconInfo);

[DllImport("user32.dll")]
private static extern IntPtr LoadCursorFromFile(string lpFileName);

[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);

private Bitmap BitmapFromCursor(Cursor cur)
{
    ICONINFO ii;
    GetIconInfo(cur.Handle, out ii);

    Bitmap bmp = Bitmap.FromHbitmap(ii.hbmColor);
    DeleteObject(ii.hbmColor);
    DeleteObject(ii.hbmMask);

    BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
    Bitmap dstBitmap = new Bitmap(bmData.Width, bmData.Height, bmData.Stride, PixelFormat.Format32bppArgb, bmData.Scan0);
    bmp.UnlockBits(bmData);

    return new Bitmap(dstBitmap);
}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    //Using LoadCursorFromFile from user32.dll, get a handle to the icon
    IntPtr hCursor = LoadCursorFromFile("C:\\Windows\\Cursors\\Windows Aero\\aero_busy.ani");

    //Create a Cursor object from that handle
    Cursor cursor = new Cursor(hCursor);

    //Convert that cursor into a bitmap
    using (Bitmap cursorBitmap = BitmapFromCursor(cursor))
    {
        //Draw that cursor bitmap directly to the form canvas
        e.Graphics.DrawImage(cursorBitmap, 50, 50);
    }
}

它是为 Win Forms 编写的并绘制图像。但也可以在 wpf 中使用,并引用 System.Windows.Forms。然后您可以将该位图转换为位图源并将其显示在图像控件中...

我使用 System.Windows.Forms.Cursor 而不是 System.Windows.Input.Cursor 的原因是我无法使用 IntPtr 句柄创建光标的新实例......

编辑:上述方法不适用于具有低颜色位的光标。另一种方法是使用Icon.ExtractAssociatedIcon

System.Drawing.Icon i = System.Drawing.Icon.ExtractAssociatedIcon(@"C:\Windows\Cursors\arrow_rl.cur");
System.Drawing.Bitmap b = i.ToBitmap();

希望对某人有所帮助...

于 2013-08-23T08:35:20.097 回答