Microsoft KB Archive/822495

= 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 =

Article ID: 822495

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





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:  Create a sample bitmap file (.bmp) or select a bitmap file that is already available on your computer.  Append the following code in the Form1 class constructor after the InitializeComponent statement.

Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 Code Dim Img As Bitmap = Bitmap.FromFile(&quot;c:\\Example.bmp&quot;) '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(&quot;c:\\Example.bmp&quot;); //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);  Rebuild the application.

Note In the sample code, modify C:\\ .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.



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.
 * 1) Under Templates, click Windows Application, and then click OK.

By default, Form1 is added to the project.
 * 1) Right-click Form1, and then click Properties.
 * 2) In the Properties window, set the BackgroundImage property to the background image that you use as the background for the form.
 * 3) Set the TransparencyKey property of Form1 to one of the colors of the background image.
 * 4) On the File menu, click Save All to save the project.
 * 5) 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.

