Create a semi-transparent cursor from an image

2019-03-26 07:25发布

Is it possible to create a cursor from an image and have it be semi-transparent?

I'm currently taking a custom image and overylaying the mouse cursor image. It would be great if I could make this semi-transparent, but not necessary. The sales guys love shiny.

Currently doing something like this:

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条回答
别忘想泡老子
2楼-- · 2019-03-26 08:01

that is very easy, I don't use API.

the code is

  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.

I hope that is your solution

查看更多
forever°为你锁心
3楼-- · 2019-03-26 08:03

If you want to set transparency of a custom mouse cursor bitmap 'on the fly' you may find this function helpful. It uses a color matrix to set the amount of transparency to any given bitmap and will return the modified one. To have just a touch of transparency the TranspFactor should be between 225 and 245, just try it out. (You need to import System.Drawing and 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楼-- · 2019-03-26 08:04

On the top of my head (I would try that first):

  1. create new bitmap with same size as original, but with ARGB structure
  2. drawimage: existing bitmap to the new bitmap
  3. access raw bitmap data, and replace A bytes with 128

You should have nice semitransparent bitmap there.

If performance allows, you can scan for fully transparent pixels and set A to zero for them!

查看更多
小情绪 Triste *
5楼-- · 2019-03-26 08:23

I've tried following example, and it was working fine...

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

And i've put this on button click event (you can call from where you like):

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

And the Up.png image is saved with 75% opacity in AdobePhotoshop.

查看更多
登录 后发表回答