Share via


Geek Speak: BUG: TransparencyKey Property

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 Form1 class constructor after the InitializeComponent statement. Microsoft Visual Basic .NET 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 = ImgMe.TransparencyKey = Img.GetPixel(10, 10)
Microsoft Visual C# .NET 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.

 

Link to the rest of the document:

https://support.microsoft.com/default.aspx?scid=kb;en-us;822495

Comments

  • Anonymous
    January 05, 2005
    Thanks David,

    This did fix the problem.

    Brian
  • Anonymous
    January 05, 2005

    i was looking for a workaround on this one; fire up google and found your weblog instead of landing to ms kb articles.

    cool!
  • Anonymous
    January 08, 2005
    Nice post...!!!