Domanda

Vorrei utilizzare le immagini parzialmente trasparenti nelle operazioni di drag / drop. Tutto questo è configurare e funziona bene, ma la trasformazione reale per la trasparenza ha un effetto collaterale strano. Per qualche ragione, i pixel sembrano essere miscelato su uno sfondo nero.

L'immagine seguente descrive il problema:

problema Trasparenza

Figura a) è la bitmap originale.

Figura b) è quello che viene prodotto dopo alpha blending è stata eseguita. Ovviamente questo è molto più scuro del filtro alfa 50% destinato destinato.

Figura c) è l'effetto desiderato, un'immagine a) con il 50% di trasparenza (aggiunto alla composizione di un programma di disegno).

Il codice che uso per produrre l'immagine trasparente è la seguente:

Bitmap bmpNew = new Bitmap(bmpOriginal.Width, bmpOriginal.Height);
Graphics g = Graphics.FromImage(bmpNew);

// Making the bitmap 50% transparent:
float[][] ptsArray ={ 
    new float[] {1, 0, 0, 0, 0},        // Red
    new float[] {0, 1, 0, 0, 0},        // Green
    new float[] {0, 0, 1, 0, 0},        // Blue
    new float[] {0, 0, 0, 0.5f, 0},     // Alpha
    new float[] {0, 0, 0, 0, 1}         // Brightness
};
ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
ImageAttributes imgAttributes = new ImageAttributes();
imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(bmpOriginal, new Rectangle(0, 0, bmpOriginal.Width, bmpOriginal.Height), 0, 0, bmpOriginal.Width, bmpOriginal.Height, GraphicsUnit.Pixel, imgAttributes);
Cursors.Default.Draw(g, new Rectangle(bmpOriginal.Width / 2 - 8, bmpOriginal.Height / 2 - 8, 32, 32));
g.Dispose();
imgAttributes.Dispose();
return bmpNew;

Qualcuno sa il motivo per cui l'alpha blending non funziona?

Aggiornamento I:

Per chiarezza, il codice funziona se sto alphablending sulla cima di una superficie disegnato. Il problema è che voglio creare un'immagine completamente semitrasparente da un'immagine esistente e utilizzare questo come un cursore dinamico durante le operazioni di drag / drop. Anche saltando il sopra e solo per dipingere un rettangolo pieno di colore 88ffffff produce un colore grigio scuro. Qualcosa di strano sta succedendo con l'icona.

Aggiornamento II:

Da quando ho reseached un bel po 'e che questo ha qualcosa a che fare con la creazione del cursore, sarò comprendono che il codice qui sotto anche. Se io GetPixel-campione la bitmap appena prima della chiamata CreateIconIndirect, i quattro valori di colore sembrano essere intatto. Così Ho la sensazione i colpevoli potrebbe essere la hbmColor oi membri hbmMask della struttura ICONINFO.

Ecco la struttura ICONINFO:

public struct IconInfo {    // http://msdn.microsoft.com/en-us/library/ms648052(VS.85).aspx
    public bool fIcon;      // Icon or cursor. True = Icon, False = Cursor
    public int xHotspot;
    public int yHotspot;
    public IntPtr hbmMask;  // Specifies the icon bitmask bitmap. If this structure defines a black and white icon, 
                            // this bitmask is formatted so that the upper half is the icon AND bitmask and the lower 
                            // half is the icon XOR bitmask. Under this condition, the height should be an even multiple of two. 
                            // If this structure defines a color icon, this mask only defines the AND bitmask of the icon.
    public IntPtr hbmColor; // Handle to the icon color bitmap. This member can be optional if this structure defines a black 
                            // and white icon. The AND bitmask of hbmMask is applied with the SRCAND flag to the destination; 
                            // subsequently, the color bitmap is applied (using XOR) to the destination by using the SRCINVERT flag. 

}

E qui è il codice che crea effettivamente il cursore:

    public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot) {
        IconInfo iconInfo = new IconInfo();
        GetIconInfo(bmp.GetHicon(), ref iconInfo);
        iconInfo.hbmColor = (IntPtr)0;
        iconInfo.hbmMask = bmp.GetHbitmap();
        iconInfo.xHotspot = xHotSpot;
        iconInfo.yHotspot = yHotSpot;
        iconInfo.fIcon = false;

        return new Cursor(CreateIconIndirect(ref iconInfo));
    }

Le due funzioni esterne sono definite come segue:

    [DllImport("user32.dll", EntryPoint = "CreateIconIndirect")]
    public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
È stato utile?

Soluzione

GDI + ha un certo numero di problemi legati alla alpha blending quando facendo interoperabilità con GDI (e Win32). In questo caso, la chiamata a bmp.GetHbitmap () si fondono la vostra immagine con uno sfondo nero. Un su CodeProject fornisce maggiori dettagli sul problema, e una soluzione che è stata utilizzata per l'aggiunta di immagini per un elenco di immagini.

Si dovrebbe essere in grado di utilizzare codice simile per ottenere il HBITMAP da utilizzare per la maschera:

[DllImport("kernel32.dll")]
public static extern bool RtlMoveMemory(IntPtr dest, IntPtr source, int dwcount);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDIBSection(IntPtr hdc, [In, MarshalAs(UnmanagedType.LPStruct)]BITMAPINFO pbmi, uint iUsage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);

public static IntPtr GetBlendedHBitmap(Bitmap bitmap)
{
    BITMAPINFO bitmapInfo = new BITMAPINFO();
    bitmapInfo.biSize = 40;
    bitmapInfo.biBitCount = 32;
    bitmapInfo.biPlanes = 1;

    bitmapInfo.biWidth = bitmap.Width;
    bitmapInfo.biHeight = -bitmap.Height;

    IntPtr pixelData;
    IntPtr hBitmap = CreateDIBSection(
        IntPtr.Zero, bitmapInfo, 0, out pixelData, IntPtr.Zero, 0);

    Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
    BitmapData bitmapData = bitmap.LockBits(
        bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb );
    RtlMoveMemory(
        pixelData, bitmapData.Scan0, bitmap.Height * bitmapData.Stride);

    bitmap.UnlockBits(bitmapData);
    return hBitmap;
}

Altri suggerimenti

Qualche tempo fa, ho letto questo problema nasce da un requisito per i canali alfa pre-moltiplicato nella bitmap. Non sono sicuro se questo è stato un problema con i cursori di Windows o GDI, e per la vita di me, non riesco a trovare la documentazione per quanto riguarda questo. Così, mentre questa spiegazione può o non può essere corretto, il seguente codice in effetti fare quello che vuoi, utilizzando un canale alfa pre-moltiplicato nella bitmap del cursore.

public class CustomCursor
{
  // alphaLevel is a value between 0 and 255. For 50% transparency, use 128.
  public Cursor CreateCursorFromBitmap(Bitmap bitmap, byte alphaLevel, Point hotSpot)
  {
    Bitmap cursorBitmap = null;
    External.ICONINFO iconInfo = new External.ICONINFO();
    Rectangle rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

    try
    {
      // Here, the premultiplied alpha channel is specified
      cursorBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppPArgb);

      // I'm assuming the source bitmap can be locked in a 24 bits per pixel format
      BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
      BitmapData cursorBitmapData = cursorBitmap.LockBits(rectangle, ImageLockMode.WriteOnly, cursorBitmap.PixelFormat);

      // Use either SafeCopy() or UnsafeCopy() to set the bitmap contents
      SafeCopy(bitmapData, cursorBitmapData, alphaLevel);
      //UnsafeCopy(bitmapData, cursorBitmapData, alphaLevel);

      cursorBitmap.UnlockBits(cursorBitmapData);
      bitmap.UnlockBits(bitmapData);

      if (!External.GetIconInfo(cursorBitmap.GetHicon(), out iconInfo))
        throw new Exception("GetIconInfo() failed.");

      iconInfo.xHotspot = hotSpot.X;
      iconInfo.yHotspot = hotSpot.Y;
      iconInfo.IsIcon = false;

      IntPtr cursorPtr = External.CreateIconIndirect(ref iconInfo);
      if (cursorPtr == IntPtr.Zero)
        throw new Exception("CreateIconIndirect() failed.");

      return (new Cursor(cursorPtr));
    }
    finally
    {
      if (cursorBitmap != null)
        cursorBitmap.Dispose();
      if (iconInfo.ColorBitmap != IntPtr.Zero)
        External.DeleteObject(iconInfo.ColorBitmap);
      if (iconInfo.MaskBitmap != IntPtr.Zero)
        External.DeleteObject(iconInfo.MaskBitmap);
    }
  }

  private void SafeCopy(BitmapData srcData, BitmapData dstData, byte alphaLevel)
  {
    for (int y = 0; y < srcData.Height; y++)
      for (int x = 0; x < srcData.Width; x++)
      {
        byte b = Marshal.ReadByte(srcData.Scan0, y * srcData.Stride + x * 3);
        byte g = Marshal.ReadByte(srcData.Scan0, y * srcData.Stride + x * 3 + 1);
        byte r = Marshal.ReadByte(srcData.Scan0, y * srcData.Stride + x * 3 + 2);

        Marshal.WriteByte(dstData.Scan0, y * dstData.Stride + x * 4, b);
        Marshal.WriteByte(dstData.Scan0, y * dstData.Stride + x * 4 + 1, g);
        Marshal.WriteByte(dstData.Scan0, y * dstData.Stride + x * 4 + 2, r);
        Marshal.WriteByte(dstData.Scan0, y * dstData.Stride + x * 4 + 3, alphaLevel);
      }
  }

  private unsafe void UnsafeCopy(BitmapData srcData, BitmapData dstData, byte alphaLevel)
  {
    for (int y = 0; y < srcData.Height; y++)
    {
      byte* srcRow = (byte*)srcData.Scan0 + (y * srcData.Stride);
      byte* dstRow = (byte*)dstData.Scan0 + (y * dstData.Stride);

      for (int x = 0; x < srcData.Width; x++)
      {
        dstRow[x * 4] = srcRow[x * 3];
        dstRow[x * 4 + 1] = srcRow[x * 3 + 1];
        dstRow[x * 4 + 2] = srcRow[x * 3 + 2];
        dstRow[x * 4 + 3] = alphaLevel;
      }
    }
  }
}

Le dichiarazioni PInvoke si trovano nella classe esterno, illustrato di seguito:

public class External
{
  [StructLayout(LayoutKind.Sequential)]
  public struct ICONINFO
  {
    public bool IsIcon;
    public int xHotspot;
    public int yHotspot;
    public IntPtr MaskBitmap;
    public IntPtr ColorBitmap;
  };

  [DllImport("user32.dll")]
  public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);

  [DllImport("user32.dll")]
  public static extern IntPtr CreateIconIndirect([In] ref ICONINFO piconinfo);

  [DllImport("gdi32.dll")]
  public static extern bool DeleteObject(IntPtr hObject);

  [DllImport("gdi32.dll")]
  public static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint cPlanes, uint cBitsPerPel, IntPtr lpvBits);
}

Alcune note sul codice:

  1. Per utilizzare il metodo non sicuro, UnsafeCopy (), è necessario compilare con il flag / non sicuri.
  2. La bitmap metodi di copia sono brutti, in particolare il metodo sicuro, che utilizza Marshal.ReadByte () / Marshal.WriteByte () chiama. Ci deve essere un modo più veloce per copiare i dati bitmap, mentre anche l'inserimento di byte alfa.
  3. Io assumo che bitmap di origine è atto ad essere bloccato in una 24 bit per pixel formato. Questo non dovrebbe essere un problema, però.

provare a ridurre il valore di blu a 0,7 o 0,6 e vedere se questo è più vicino a ciò che si desidera.

Ecco un buon sito che spiega ColorMatrix :

Quando eseguo il codice per modificare un'immagine in una PictureBox un'immagine di sfondo griglia con, ottengo l'effetto desiderato senza modificare il codice. Forse l'immagine è in corso di elaborazione sopra la parte superiore di qualcosa che ha un colore scuro ...

Perdonami se il mio suggerimento è troppo semplicistica (io sono ancora una novità per C #), ma ho trovato questo sul sito MSDN e magari questo potrebbe puntare nella direzione giusta?

/ opaco

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