【问题标题】:Create a semi-transparent cursor from an image从图像创建半透明光标
【发布时间】:2009-02-05 15:54:27
【问题描述】:

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

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

目前正在做这样的事情:

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());

alt text http://members.cox.net/dustinbrooks/drag.jpg

【问题讨论】:

    标签: c# winforms transparency mouse-cursor


    【解决方案1】:

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

        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% 的不透明度保存。

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        【解决方案3】:

        如果您想“即时”设置自定义鼠标光标位图的透明度,您可能会发现此功能很有帮助。它使用颜色矩阵来设置任何给定位图的透明度,并将返回修改后的位图。 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;
        

        }

        【讨论】:

          【解决方案4】:

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

          我希望这是你的解决方案

          【讨论】:

            猜你喜欢
            • 2015-04-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-19
            • 2011-10-19
            • 1970-01-01
            • 2013-03-26
            相关资源
            最近更新 更多