C# 从图像创建半透明光标

C# 从图像创建半透明光标,c#,winforms,cursor,transparency,C#,Winforms,Cursor,Transparency,是否可以从图像创建一个半透明的光标 我目前正在拍摄一张自定义图像,并对鼠标光标图像进行叠加。如果我能把它做成半透明的,那就太好了,但不是必须的。销售人员喜欢闪亮 目前正在做类似的事情: Image cursorImage = customImage.GetThumbnailImage(300, 100, null, IntPtr.Zero); cursorImage.SetResolution(96.0F, 96.0F); int midPointX = cursorImage.Width /

是否可以从图像创建一个半透明的光标

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

目前正在做类似的事情:

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());
在我的头顶上(我会先试试):

  • 创建与原始位图大小相同但具有ARGB结构的新位图
  • drawimage:将现有位图转换为新位图
  • 访问原始位图数据,并将字节替换为128
  • 你应该有很好的半透明位图


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

    我试过下面的例子,效果很好

        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);
        }
    
    我已经把这个放在了按钮点击事件上(你可以在你喜欢的地方打电话):


    并且Up.png图像以75%的不透明度保存在AdobePhotoshop中。

    这非常简单,我不使用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.
    

    我希望这是您的解决方案

    如果您想“动态”设置自定义鼠标光标位图的透明度,您可能会发现此功能很有用。它使用颜色矩阵设置任何给定位图的透明度,并返回修改后的位图。要想稍微增加透明度,transfactor应该介于225和245之间,请尝试一下。(需要导入System.Drawing和System.Drawing.Imaging)

    {

    }

      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.
    
    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;