Search code examples
c#user-controlsbackgroundparenttransparent

How to make the User's Control's BackGround transparent?


I want my UserControl's BackGround to look like it's not there. I tried to do it like that:

if ((Parent != null) && (Parent.BackgroundImage != null))
{
    Bitmap backGroundImage = new Bitmap(Width, Height);
    Graphics.FromImage(backGroundImage).DrawImage(owner.BackgroundImage, 0, 0, new Rectangle(Location.X, Location.Y, Width, Height), GraphicsUnit.Pixel);
     this.BackgroundImage = (Image)backGroundImage;
}

and it works, but I also have to calculate BackGroundImage of the Form which is UserControl's Parent every time its size changes and set Form's BackGroundImageLayout to none. My UserControl could take Parent's BackGroundImage and stretch it every time it needs to redraw, and then just take a piece of it and set it as its BackGroundImage, but Form draws its stretched background outside its visible rectangle. So I can see then the diffrence, it doesn't fit.

I made it working and it fits the Parent's BackGroundImage, but it's a lot of code and slowing down my program when resizing.

There must be a simplest way to do that. I don't mean using ControlStyles.SupportsTransparentBackColor. I want it to look just like there were no UserControl's background.


Solution

  • Make usercontrol backcolor as Transparent this.BackColor = Color.Transparent;