代码之家  ›  专栏  ›  技术社区  ›  Dustin Brooks

从图像创建半透明光标

  •  9
  • Dustin Brooks  · 技术社区  · 16 年前

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

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

    目前正在这样做:

    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

    4 回复  |  直到 8 年前
        1
  •  6
  •   godly-devotion    13 年前

    我试过用下面的例子,而且效果很好…

        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图像在adobephop中以75%的不透明度保存。

        2
  •  0
  •   Daniel MoÅ¡mondor    15 年前

    在我的头上(我先试试看):

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

    你应该有很好的半透明位图。

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

        3
  •  0
  •   pewehh    8 年前

    如果要“即时”设置自定义鼠标光标位图的透明度,您可能会发现此函数很有用。它使用颜色矩阵来设置任何给定位图的透明度,并返回修改后的位图。只有一点透明度 转置因子 应该在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
  •  -2
  •   Gonzalo.-    12 年前

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

    我希望这是你的解决办法