Recently I wanted to overlay a PictureBox over the top of a WebBrowser control in C# for Windows Forms. I added a transparent PNG to a PictureBox and set the Background to transparent.
However the problem with this approach is the Background of the PictureBox isn’t truely transparent, it simply inherits the background colour or background image of the control in the layer beneath it.
So in Visual Studio I have the following (note the scroll bar at the bottom right hand corner which can’t be seen or accessed due to the image overlaying it)
![]()
However I found a small function that will take the colour of the PictureBox at pixel position x=0, y=0 (which in my image happens to be transparent and apply that to all other transparent elements.
public static System.Drawing.Drawing2D.GraphicsPath Transparent(Image im)
{
int x;
int y;
Bitmap bmp = new Bitmap(im);
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
Color mask = bmp.GetPixel(0, 0);
for (x = 0; x <= bmp.Width - 1; x++)
{
for (y = 0; y <= bmp.Height - 1; y++)
{
if (!bmp.GetPixel(x, y).Equals(mask))
{
gp.AddRectangle(new Rectangle(x, y, 1, 1));
}
}
}
bmp.Dispose();
return gp;
}
Then in your form load event handler
System.Drawing.Drawing2D.GraphicsPath gp = Resources.Images.Transparent(pictureBox1.Image); pictureBox1.Region = new System.Drawing.Region(gp);
And voila, when you compile your application you'll get
![]()

