Transparent Usercontrols

  • Thread starter Thread starter Battal
  • Start date Start date
B

Battal

Hi,

I'm trying to develop a user control with transparent
background. In my application I'll have a picturebox
control with my background image on it, and on top of the
picturebox control I'll put my usercontrols with images on
it. And I want this images to be drawn transparent.

In my OnPaint event of the usercontrol, I get the first
pixel of my image, and create an image attributes object
and draw it. However it's not transparent. the image is
drawn as it is.

Another weird thing is, I edit my images(.bmp) in MS
Paint. And my transparency color is white, RGB(255, 255,
255). However when I read the image from within my
application and get the first pixel using Image.getPixel
(0, 0) the rgb value that I get is RGB(248, 248, 248).
Isn't that weird? Or, am I missing something?

Any samples or ideas?

Regards

Battal Abdurrahman
 
I spent a day trying to get a Label working with a Transparent background
and failed, so if you get this working let us know how you did it.
 
I just did that myself. I created "transparent picture buttons". I ran into
the same problem as you. I couldn't figure out how to make part of my
picture transparent. I believe it is because the underlying picture (drawn
on the form below my control) doesn't get painted underneath controls. So,
since the underlying picture never gets painted, my "transparent" areas just
showed white under them. I hope that made sense. Anyway, to get my controls
to appear transparent I had to do the following:

1) Set a property (backgroundImage) on my user control with the background
image that was used on the form below my control.

2) Set a property (buttonImage) on my user control of the image I want for
my button.

3) In the OnPaint event, do this:

// first we will draw the background image
// on the graphics and offset it so it lines up
// correctly with the background image on the form
e.Graphics.DrawImage(backgroundImage, -Left, -Top);

// create an imageattribute and set the color key for the transparency
// color (white in this example)
System.Drawing.Imaging.ImageAttributes att = new
System.Drawing.Imaging.ImageAttributes();
att.SetColorKey(Color.White, Color.White);

//now draw my button graphic with transparency
e.Graphics.DrawImage(buttonImage, new Rectangle(0, 0, buttonImage.Width,
buttonImage.Height), 0, 0, buttonImage.Width, buttonImage.Height,
GraphicsUnit.Pixel, att);

// then I go on to draw text and a border and stuff...


HTH,
Mark
 
Battal said:
Another weird thing is, I edit my images(.bmp) in MS
Paint. And my transparency color is white, RGB(255, 255,
255). However when I read the image from within my
application and get the first pixel using Image.getPixel
(0, 0) the rgb value that I get is RGB(248, 248, 248).
Isn't that weird? Or, am I missing something?

On most PPCs you have 16K colors, so what you are seeing is an RGB color
adjusted to actual device caps. Perhaps if you synchronize your transparency
color with the device caps (by setting it to RGB(248,248,248) it will
work...
 
Thanks Mark, It worked.

That's great.
-----Original Message-----
I just did that myself. I created "transparent picture buttons". I ran into
the same problem as you. I couldn't figure out how to make part of my
picture transparent. I believe it is because the underlying picture (drawn
on the form below my control) doesn't get painted underneath controls. So,
since the underlying picture never gets painted, my "transparent" areas just
showed white under them. I hope that made sense. Anyway, to get my controls
to appear transparent I had to do the following:

1) Set a property (backgroundImage) on my user control with the background
image that was used on the form below my control.

2) Set a property (buttonImage) on my user control of the image I want for
my button.

3) In the OnPaint event, do this:

// first we will draw the background image
// on the graphics and offset it so it lines up
// correctly with the background image on the form
e.Graphics.DrawImage(backgroundImage, -Left, -Top);

// create an imageattribute and set the color key for the transparency
// color (white in this example)
System.Drawing.Imaging.ImageAttributes att = new
System.Drawing.Imaging.ImageAttributes();
att.SetColorKey(Color.White, Color.White);

//now draw my button graphic with transparency
e.Graphics.DrawImage(buttonImage, new Rectangle(0, 0, buttonImage.Width,
buttonImage.Height), 0, 0, buttonImage.Width, buttonImage.Height,
GraphicsUnit.Pixel, att);

// then I go on to draw text and a border and stuff...


HTH,
Mark






.
 
Back
Top