9

是否可以从图像创建光标并使其半透明?

我目前正在拍摄自定义图像并覆盖鼠标光标图像。如果我能把它做成半透明的就好了,但不是必须的。销售人员喜欢闪亮的。

目前正在做这样的事情:

Image cursorImage = customImage.GetThumbnailImage(300, 100, null, IntPtr.Zero);
cursorImage.SetResolution(96.0F, 96.0F);
int midPointX = cursorImage.Width / 2;
int midPointY = cursorImage.Height / 2;
Bitmap cursorMouse = GetCursorImage(cursorOverlay);
Graphics cursorGfx = Graphics.FromImage(cursorImageCopy);
cursorGfx.DrawImageUnscaled(cursorMouse, midPointX, midPointY);

Cursor tmp = new Cursor(cursorImage.GetHicon());

替代文字 http://members.cox.net/dustinbrooks/drag.jpg

4

4 回答 4

6

我试过下面的例子,它工作正常......

    public struct IconInfo
    {
        public bool fIcon;
        public int xHotspot;
        public int yHotspot;
        public IntPtr hbmMask;
        public IntPtr hbmColor;
    }


    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

    [DllImport("user32.dll")]
    public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

    public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
    {
        IntPtr ptr = bmp.GetHicon();
        IconInfo tmp = new IconInfo();
        GetIconInfo(ptr, ref tmp);
        tmp.xHotspot = xHotSpot;
        tmp.yHotspot = yHotSpot;
        tmp.fIcon = false;
        ptr = CreateIconIndirect(ref tmp);
        return new Cursor(ptr);
    }

我已经把它放在按钮点击事件上(你可以从你喜欢的地方打电话):

Bitmap b = new Bitmap("D:/Up.png");
this.Cursor = CreateCursor(b, 5, 5);

Up.png 图像在 Adob​​ePhotoshop 中以 75% 的不透明度保存。

于 2009-09-11T06:52:19.273 回答
0

在我的头顶上(我会先尝试):

  1. 创建与原始大小相同但具有 ARGB 结构的新位图
  2. drawimage:现有位图到新位图
  3. 访问原始位图数据,并将 A 字节替换为 128

你应该在那里有漂亮的半透明位图。

如果性能允许,您可以扫描完全透明的像素并将它们的 A 设置为零!

于 2009-09-04T21:39:35.643 回答
0

如果您想“即时”设置自定义鼠标光标位图的透明度,您可能会发现此功能很有帮助。它使用颜色矩阵来设置任何给定位图的透明度,并将返回修改后的位图。要获得一点透明度,TranspFactor应该在 225 到 245 之间,试试吧。(需要导入 System.Drawing 和 System.Drawing.Imaging)

public static Bitmap GetBMPTransparent(Bitmap bmp, int TranspFactor)

{

Bitmap transpBmp = new Bitmap(bmp.Width, bmp.Height);
using (ImageAttributes attr = new ImageAttributes()) {
    ColorMatrix matrix = new ColorMatrix { Matrix33 = Convert.ToSingle(TranspFactor / 255) };
    attr.SetColorMatrix(matrix);
    using (Graphics g = Graphics.FromImage(transpBmp)) {
        g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attr);
    }
}
return transpBmp;

}

于 2016-08-09T13:42:23.267 回答
-2

这很容易,我不使用 API。

代码是

  Bitmap img = new Bitmap(new Bitmap(@"image.png"), 30, 30); //this is the size of cursor

  Icon icono = Icon.FromHandle(img.GetHicon()); //create the Icon object 

  Cursor = new Cursor(icono.Handle); //the icon Object has the stream to create a Cursor.

我希望这是你的解决方案

于 2012-09-21T17:23:47.837 回答