2009-02-05 7 views
8

Est-il possible de créer un curseur à partir d'une image et de le rendre semi-transparent?Créer un curseur semi-transparent à partir d'une image

Je suis en train de prendre une image personnalisée et de surligner l'image du curseur de la souris. Ce serait génial si je pouvais rendre cela semi-transparent, mais pas nécessaire. Les vendeurs aiment l'éclat.

faire actuellement quelque chose comme ceci:

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

Répondre

5

J'ai essayé exemple suivant, et il fonctionnait très bien ...

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

Et j'ai mis cela sur l'événement bouton de clic (vous pouvez appeler où vous le souhaitez):

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

Et l'image Up.png est enregistrée avec 75% d'opacité dans AdobePhotoshop.

0

Sur le dessus de ma tête (j'essayer en premier):

  1. créer une nouvelle image bitmap avec la même taille que l'original , mais avec la structure ARGB
  2. drawimage: bitmap existant vers le nouveau bitmap
  3. accéder aux données bitmap brutes, et remplacer A octets avec 128

Vous devriez avoir un bitmap semi-transparent ici. Si les performances le permettent, vous pouvez rechercher des pixels totalement transparents et régler A sur zéro pour eux!

-2

c'est très facile, je n'utilise pas API.

le code est

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. 

J'espère que est votre solution

+1

-1 ne fait pas semi-transparent. – quantum

+0

-1 En outre, ne permet pas de définir l'emplacement du point d'accès. – takrl

0

Si vous souhaitez définir la transparence d'une image du curseur de souris personnalisé « à la volée » entre vous trouverez cette fonction utile. Il utilise une matrice de couleurs pour définir la quantité de transparence pour un bitmap donné et retournera celui modifié. Pour avoir juste une touche de transparence, le TranspFactor devrait être entre 225 et 245, il suffit de l'essayer. (Vous devez importer System.Drawing et 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; 

}