Domanda

E 'possibile creare un cursore da un'immagine e farlo essere semi-trasparente?

Al momento sto prendendo un'immagine personalizzata e overylaying l'immagine cursore del mouse. Sarebbe grande se ho potuto fare questo semi-trasparente, ma non necessario. I ragazzi di vendita amore lucente.

Al momento di fare qualcosa di simile:

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

È stato utile?

Soluzione

Ho provato seguente, e stava funzionando benissimo ...

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

E ho messo questo su evento click del pulsante (è possibile chiamare da dove vi pare):

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

E l'immagine up.png viene salvata con il 75% di opacità in AdobePhotoshop.

Altri suggerimenti

Sulla parte superiore della mia testa (vorrei provare quella prima):

  1. Crea nuovo bitmap con stesse dimensioni dell'originale, ma con struttura ARGB
  2. DrawImage: bitmap esistente alla nuova bitmap
  3. accesso ai dati bitmap grezzi, e sostituire un byte con 128

Si dovrebbe avere bello bitmap semitrasparente lì.

Se le prestazioni consente, è possibile eseguire la scansione per pixel completamente trasparenti e impostare un a zero per loro!

Se si desidera impostare la trasparenza di un topo personalizzato cursore del bitmap 'al volo' si può trovare questa funzione utile. Esso utilizza una matrice colore per impostare la quantità di trasparenza a qualsiasi bitmap e tornerà quello modificato. Per avere solo un tocco di trasparenza del TranspFactor deve essere compresa tra 225 e 245, basta provare. (È necessario importare System.Drawing e 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;

}

che è molto facile, io non uso API.

Il codice è

  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.

Mi auguro che sia la soluzione

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top