2009-02-05 94 views
8

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

我目前正在拍摄一张自定义图像和overylaying鼠标光标图像。如果我能做出这种半透明的,但不是必要的,那将是非常好的。销售人员喜欢闪亮。

目前国内做这样的事情:

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

回答

5

我试过下面的例子中,这是工作的罚款...

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

0

在我的头顶(我会尝试在前):

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

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

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

-2

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

我希望这是你的解决方案

+1

-1并不能使它半透明。 – quantum 2012-09-22 00:37:04

0

如果要设置“对飞”自定义鼠标光标位图的透明度,你会发现这个功能有帮助。它使用颜色矩阵来设置任何给定位图的透明度,并返回修改后的位图。要获得透明度,应该在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; 

}