Gewusst wie: Zeichnen von Bildern mit Transparenz

Aktualisiert: November 2007

.NET Compact Framework unterstützt Transparenz, allerdings nur mit einer Transparenzfarbe. In der SetColorKey(Color, Color)-Methode muss für den niedrigen und den hohen Farbbereich die gleiche Farbe angegeben sein.

Beispiel

In diesem Beispiel werden eine Bitmap eines Rechtecks mit rotem und schwarzem Design erstellt und zwei Techniken für das Festlegen der Transparenz veranschaulicht:

  • Verwenden Sie die SetColorKey(Color, Color)-Methode auf Grundlage eines Pixels im Bild. In diesem Beispiel wird die Transparenz anhand des linken oberen Pixels des Bildes festgelegt. Da dieses Pixel schwarz ist, werden alle ursprünglich schwarzen Pixel transparent.

  • Verwenden Sie die SetColorKey(Color, Color)-Methode mit einer expliziten Farbeinstellung. In diesem Beispiel wird die Farbe Rot festgelegt, sodass alle ursprünglich roten Pixel transparent werden.

Führen Sie zur Veranschaulichung die Anwendung zunächst mit den auskommentierten Transparenztechniken aus, um festzustellen, wie das Bild ohne Transparenz aussieht. Heben Sie dann die Auskommentierung des Codes für eine der beiden Transparenztechniken auf.

' The .NET Compact Framework supports transparency,
' but with only one transparency color.
' The SetColorKey method must have the same color 
' specified for the low color and high color range.

' Note that you must uncomment lines of code
' as indicated for the desired transparency effect.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

' Create a red and black bitmap to demonstrate transparency.
    Dim bmp As New Bitmap(75, 75)
    Dim g As Graphics = Graphics.FromImage(bmp)

    g.FillEllipse(New SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width)
    g.DrawLine(New Pen(Color.Black), 0, 0, bmp.Width, bmp.Width)
    g.DrawLine(New Pen(Color.Black), bmp.Width, 0, 0, bmp.Width)
    g.Dispose()


Dim attr As New ImageAttributes

' Set the transparency color key based on the upper-left pixel 
' of the image.
' Uncomment the following line to make all black pixels transparent:
' attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0))

' Set the transparency color key based on a specified value.
' Uncomment the following line to make all red pixels transparent:
' attr.SetColorKey(Color.Red, Color.Red)

' Draw the image using the image attributes.
Dim dstRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height, _
    GraphicsUnit.Pixel, attr)

End Sub
// The .NET Compact Framework supports transparency,
// but with only one transparency color.
// The SetColorKey method must have the same color 
// specified for the low color and high color range.

// Note that you must uncomment lines of code
// as indicated for the desired transparency effect.

protected override void OnPaint(PaintEventArgs e)
{
    // Create a red and black bitmap to demonstrate transparency.
    Bitmap bmp = new Bitmap(75,75);
    Graphics g = Graphics.FromImage(bmp);

    g.FillEllipse(new SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width);
    g.DrawLine(new Pen(Color.Black), 0, 0, bmp.Width, bmp.Width);
    g.DrawLine(new Pen(Color.Black), bmp.Width, 0, 0, bmp.Width);
    g.Dispose();

    ImageAttributes attr = new ImageAttributes();

    // Set the transparency color key based on the upper-left pixel 
    // of the image.
    // Uncomment the following line to make all black pixels transparent:
    // attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));

    // Set the transparency color key based on a specified value.
    // Uncomment the following line to make all red pixels transparent:
    // attr.SetColorKey(Color.Red, Color.Red);

    // Draw the image using the image attributes.
    Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
        GraphicsUnit.Pixel, attr);
}

Kompilieren des Codes

Für dieses Beispiel sind Verweise auf die folgenden Namespaces erforderlich:

Robuste Programmierung

Beachten Sie, dass das zur Erstellung der Bitmap verwendete Graphics-Objekt explizit verworfen wird. Das Graphics-Objekt, das von der Graphics-Eigenschaft des PaintEventArgs-Objekts zurückgegeben wird, wird vom Garbage Collector zerstört und muss nicht explizit verworfen werden.

Siehe auch

Weitere Ressourcen

Grafik und Zeichnen in .NET Compact Framework