BUG: The TransparencyKey property is not effective for Windows forms if the color depth of the monitor is set to a value that is greater than 24-bit

Symptoms

When you run a Microsoft Windows application with the TransparencyKey property of the form set to one of the colors of the background image, areas of the form that match the color that you set the TransparencyKey property to are not rendered as transparent.

Cause

This bug occurs when the color depth of the monitor is set to a value that is greater than 24-bit. When the color depth of the monitor is set to a value that is greater than 24-bit, the TransparencyKey property is not effective. Therefore, the areas on the form that match the color you set the TransparencyKey property to are not rendered as transparent.

Workaround

To work around this bug, programmatically set the BackgroundImage property and the TransparencyKey property. To do this, follow these steps:
  1. Create a sample bitmap file (.bmp) or select a bitmap file that is already available on your computer.
  2. Append the following code in the Form1class constructor after the InitializeComponentstatement.

    Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 Code
    Dim Img As Bitmap = Bitmap.FromFile("c:\\Example.bmp")
    'The color at Pixel(10,10) is rendered as transparent for the complete background.
    Img.MakeTransparent(Img.GetPixel(10, 10))
    Me.BackgroundImage = Img
    Me.TransparencyKey = Img.GetPixel(10, 10)
    Microsoft Visual C# .NET or Microsoft Visual C# 2005 Code
    System.Drawing.Bitmap Img = new System.Drawing.Bitmap("c:\\Example.bmp");
    //The color at Pixel(10,10) is rendered as transparent for the complete background.
    Img.MakeTransparent(Img.GetPixel(10,10));
    this.BackgroundImage = Img;
    this.TransparencyKey = Img.GetPixel(10,10);
  3. Rebuild the application.
Note In the sample code, modify C:\\Example.bmp based on the background image of your computer.

Status

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

More Information

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to
    New, and then click Project.
  3. Under Project Types, click Visual Basic Projects or click Visual C# Projects.

    Note In Visual Studio 2005, click Visual Basic or Visual C# under Project Types.
  4. Under Templates, click Windows Application, and then click OK.

    By default, Form1 is added to the project.
  5. Right-click Form1, and then click
    Properties.
  6. In the Properties window, set the
    BackgroundImage property to the background image that you use as the background for the form.
  7. Set the TransparencyKey property of Form1 to one of the colors of the background image.
  8. On the File menu, click Save All
    to save the project.
  9. On the Debug menu, click
    Start to run the application.

    By default, Form1 appears on the screen. The areas of the form that match the color you set the TransparencyKey property to are not rendered as transparent.

References

For more information about the TransparencyKey property, visit the following Microsoft Web site:
Propriedades

ID do Artigo: 822495 - Última Revisão: 11 de jul de 2008 - Revisão: 1

Comentários