Create a semi-transparent cursor from an image

后端 未结 4 2234
深忆病人
深忆病人 2021-02-14 23:12

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 b

4条回答
  •  不思量自难忘°
    2021-02-15 00:16

    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;
    

    }

提交回复
热议问题