2009-02-05 95 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; 

}