从图像创建半透明光标

发布于 2024-07-13 01:27:47 字数 717 浏览 8 评论 0原文

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

我目前正在拍摄自定义图像并覆盖鼠标光标图像。 如果我可以将其设为半透明,那就太好了,但不是必需的。 销售人员喜欢闪亮的。

目前正在做这样的事情:

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

替代文本http://members.cox.net/dustinbrooks/drag。 jpg

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一抹微笑 2024-07-20 01:27:47

我尝试过以下示例,效果很好...

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

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.

別甾虛僞 2024-07-20 01:27:47

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

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

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

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

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!

路弥 2024-07-20 01:27:47

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

}

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;

}

难忘№最初的完美 2024-07-20 01:27:47

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

我希望这是你的解决方案

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文