gpt4 book ai didi

c# - 如何将透明光标渲染到保留 alpha channel 的位图?

转载 作者:太空狗 更新时间:2023-10-29 17:47:16 26 4
gpt4 key购买 nike

我使用下面的代码来渲染一个透明图标:

    private void button1_Click(object sender, EventArgs e)
{
// using LoadCursorFromFile from user32.dll
var cursor = NativeMethods.LoadCustomCursor(@"d:\Temp\Cursors\Cursors\aero_busy.ani");

// cursor -> bitmap
Bitmap bitmap = new Bitmap(48, 48, PixelFormat.Format32bppArgb);
Graphics gBitmap = Graphics.FromImage(bitmap);
cursor.DrawStretched(gBitmap, new Rectangle(0, 0, 32, 32));

// 1. Draw bitmap on a form canvas
Graphics gForm = Graphics.FromHwnd(this.Handle);
gForm.DrawImage(bitmap, 50, 50);

// 2. Draw cursor directly to a form canvas
cursor.Draw(gForm, new Rectangle(100, 50, 32, 32));

cursor.Dispose();
}

不幸的是,我无法将透明的 Cursor 渲染到 Bitmap!当我直接将 Cursor 绘制到表单 Canvas 时它可以工作,但是当我将 Cursor 绘制到位图时出现问题。非常感谢任何建议。

alt text

最佳答案

您现在拥有的解决方案并不完全适用于托管代码。您自己的评论说您正在从 user32.dll P/Invoking LoadCursorFromFile。不管怎样,使用 Win32 API 真的没什么好怕的。

正如我在评论中提到的,您尝试做的事情通常与 GDI+ 绘图函数有关,就像 .NET Framework 提供的大多数函数一样。通过改用 GDI,该任务变得更加容易。 您可以使用以下代码从符合 alpha channel 的光标(或图标,它们基本上可以互换)创建位图:

[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);
}
}

如果此代码无法编译,请确保您已为 System.DrawingSystem.Drawing.Imaging 添加了 using 语句> 和 System.Runtime.InteropServices。还要记住将 Form1_Paint 方法连接为表单的 Paint 事件的处理程序。

经测试可以工作:

cursor, drawn from bitmap complete with alpha channel

关于c# - 如何将透明光标渲染到保留 alpha channel 的位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4451839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com