Windows forms with Transparent Backgrounds

  • Thread starter Thread starter Eric Renken
  • Start date Start date
E

Eric Renken

Well I seem to have an interesting problem. I have an application that
where I am setting the BackgroundImage and the TransparencyKey set in code.
These are both set after the InitializeComponent() in the constructor of the
form. This works great on my computer Windows XP Pro; however on some other
computer the TransparenecyKey isn't working and you see the horrible green
color that shouldn't be there. This doesn't happen on all machines just
some. I even went as far as pulling the TransparenacyColor from a single
pixel from the image at run time so I could make sure I was getting the
exact color.

Any thoughts?

Eric Renken
 
Eric Renken said:
Well I seem to have an interesting problem. I have an application that
where I am setting the BackgroundImage and the TransparencyKey set in
code. These are both set after the InitializeComponent() in the
constructor of the form. This works great on my computer Windows XP Pro;
however on some other computer the TransparenecyKey isn't working and you
see the horrible green color that shouldn't be there. This doesn't happen
on all machines just some. I even went as far as pulling the
TransparenacyColor from a single pixel from the image at run time so I
could make sure I was getting the exact color.

Probably, the computers where it is failing have the video adapter set to
32-bit color, while those where it is working are set to 24 bits or less. It
is a documented bug of the TransparencyKey (not working at more that 24 bits
of color depth).
 
My computer (Windows XP) is set to 32-bit and it is working, and it works on
Vista at 32-bit also, but on the other XP machines you are correct it seems
to work below 32-bit mode.

I wonder why it is working correctly on my dev box (Windows XP) at 32-bit.

The work around at
http://support.microsoft.com/default.aspx?scid=kb;en-us;822495 doesn't work.
When I do this I end up seeing the background color for the form, and you
can't set the Background Color of a form to transparent if you do you get a
nice runtime error.

Eric
 
Eric Renken said:
The work around at
http://support.microsoft.com/default.aspx?scid=kb;en-us;822495 doesn't
work. When I do this I end up seeing the background color for the form,
and you can't set the Background Color of a form to transparent if you do
you get a nice runtime error.

I'm going to say a silly thing, but the above-mentioned workaround takes
the transparencykey color from the pixel at (10,10) in the bitmap. Have you
verified that the color that you are taking is *really* the color that you
chose as transparent?

If you are only doing this to get a non-rectangular form, there's an
alternative aproach that works at all color depths:

1. Override the OnPaint event of the form.
2. In OnPaint, create a new instance of the GraphicsPath class.
3. Use the methods of the GraphicsPath object, such as AddEllipse, to
specify the shape and size required for the form.
4. Set the Region property of the form to a new Region, passing in the
constructor the GraphicsPath object that you have just created.
 
Alberto,

That isn't a silly question, but yes I change it to work with my stuff, but
I wouldn't put it past me either (darn copy and paste bugs). Well I was
doing more searching and if there would have 1 more lines in that sample it
would have worked. Here is the code I am using now:

System.Drawing.Bitmap Img = new System.Drawing.Bitmap(background);
Color mycolor = Img.GetPixel(transparencyColorLocation.X,
transparencyColorLocation.Y);
Img.MakeTransparent(mycolor);
this.BackgroundImage = Img;
this.BackColor = mycolor;
this.TransparencyKey = mycolor;

What was needed was this.BackColor. Once I added that and it seems to work
now. This is a generic form that is used for splash screens for our
application so the initial background image is passes as a paramater to the
constructor.

Thanks for the help,

Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top