How to make picturebox transparent?
One way to do this is by changing the parent of the overlapping picture box to the PictureBox over which it is lapping. Since the Visual Studio designer doesn't allow you to add a PictureBox to a PictureBox, this will have to be done in your code (Form1.cs) and within the Intializing function:
public Form1()
{
InitializeComponent();
pictureBox7.Controls.Add(pictureBox8);
pictureBox8.Location = new Point(0, 0);
pictureBox8.BackColor = Color.Transparent;
}
Just change the picture box names to what ever you need. This should return:
C# Picturebox transparent background doesn't seem to work
It probably works perfectly. You are seeing what's behind the picture box control. Which is the form. Whose BackColor is probably white. You can set the form's BackgroundImage property to be sure, you should see the image through the picture box. Like this:
Punching a hole through both the picture box and the form requires a bigger weapon, Form.TransparencyKey
PictureBox with transparent background
From this article, this is what's causing this behaviour :
Transparent controls in WinForms are transparent relative to their parent, not to other controls. Transparency in WinForms is more akin to camouflage than true transparency. A transparent control doesn’t actually let you see the control behind it through the form. It asks its parent to draw its own background on the "transparent" control. This is why a transparent control shows the form behind it, but covers up any other controls.
There is an accepted answer to your problem here (A PictureBox Problem)
Transparent control over PictureBox
The Label control supports transparency well. It is just that the designer won't let you place the label correctly. The PictureBox control is not a container control so the Form becomes the parent of the label. So you see the form's background.
It is easy to fix by adding a bit of code to the form constructor. You'll need to change the label's Parent property and recalculate it's Location since it is now relative to the picture box instead of the form. Like this:
public Form1() {
InitializeComponent();
var pos = this.PointToScreen(label1.Location);
pos = pictureBox1.PointToClient(pos);
label1.Parent = pictureBox1;
label1.Location = pos;
label1.BackColor = Color.Transparent;
}
Looks like this at runtime:
Another approach is to solve the design-time problem. That just takes an attribute. Add a reference to System.Design and add a class to your project, paste this code:
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design; // Add reference to System.Design
[Designer(typeof(ParentControlDesigner))]
class PictureContainer : PictureBox {}
Transparency of picture box
You are correct in your assumption.
Transparency in winforms does not mean that the object is actually transparent. Instead, it means that it will display it's parent object instead of it's background, including it's background, images and text, but not including any other controls on it, hence your problem.
Since the parent control of your top most picture box is not and can not be the other picture boxes, the fact that your top most picture box have a transparent background will not help.
Unfortunately, using the form's TransparencyKey
property will also not help for this. (It will make the selected color transparent, but will yield unexpected (and usually undesired) results.
In order to achieve your goal, you will have to follow OneFineDay's advice in the comments, and use Graphics
to draw the image yourself.
Fortunately, this is very easy to do:
Public Sub DrawImage(Image as Image, Location As Point)
Using(Dim g as Graphics = Me.CreateGraphics())
g.DrawImage(Image, Location)
EndUsing
End Sub
WinForms picturebox not transparent
It is quite easy all you have to do is make the canvas PictureBox of the same size and set its location also the same as your first picture box. Then set canvas PictureBox back colour to transparent. Now set your first PictureBox as the parent of canvas PictureBox.
You can write the below code on the form load event.
pictureBoxCanvas.Size = pictureBox1.Size;
pictureBoxCanvas.Location = pictureBox1.Location;
pictureBoxCanvas.BackColor = Color.Transparent;
pictureBoxCanvas.BringToFront();
pictureBoxCanvas.Parent = this.pictureBox1;
How make pictureboxes of a transparent Panel to appear?
Transparency, the way you are trying to use it, is not provided by the PictureBox control. I don't think you're going to have much luck if you continue with your current approach.
So here is a tip for an alternate approach. If you're having difficulty doing this with PictureBox controls and such, then you could do the rendering yourself. Create a UserControl that is DoubleBuffered, and in the Paint event, for each sprite, call Graphics.DrawImage to render each image to the correct spot. The nice thing about this technique is that you will get proper alpha-correct compositing when drawing the sprites this way. Just be sure to sort them so that you draw them from back to front and "paint over" the background sprites using the foreground sprites.
Make Picture boxes transparent, each overlapping the other with a corner?
Transparency in winforms is kind of misleading, since it's not really transparency.
Winforms controls mimic transparency by painting the part of their parent control they would hide instead of their own background.
However, they will not paint the other controls that might be partially covered by them.
This is the reason your top most picture boxes hides your big picture box.
You can overcome this by creating a custom control that inherits from PictureBox
and override its OnPaintBackground
method (taken, with slight adjustments, from this code project article):
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
Graphics g = e.Graphics;
if (this.Parent != null)
{
var index = Parent.Controls.GetChildIndex(this);
for (var i = Parent.Controls.Count - 1; i > index; i--)
{
var c = Parent.Controls[i];
if (c.Bounds.IntersectsWith(Bounds) && c.Visible)
{
using (var bmp = new Bitmap(c.Width, c.Height, g))
{
c.DrawToBitmap(bmp, c.ClientRectangle);
g.TranslateTransform(c.Left - Left, c.Top - Top);
g.DrawImageUnscaled(bmp, Point.Empty);
g.TranslateTransform(Left - c.Left, Top - c.Top);
}
}
}
}
}
Microsoft have published a Knowledge base article to solve this problem a long time ago, however it's a bit out-dated and it's code sample is in VB.Net.
Another option is to paint the images yourself, without picture boxes to hold them, by using Graphics.DrawImage
method.
The best place to do it is probably in the OnPaint
method of the form.
Related Topics
How to Read Single Excel Cell Value
Memory Efficiency and Performance of String.Replace .Net Framework
Parse and Modify a Query String in .Net Core
Keep Window on Top and Steal Focus in Winforms
Redirect Console.Writeline from Windows Application to a String
Launching a Process in User's Session from a Service
Get Error Message If Modelstate.Isvalid Fails
Does Mstest Have an Equivalent to Nunit's Testcase
Datacontractserializer Doesn't Call My Constructor
Linq Group by Multiple Fields -Syntax Help
List Sort Based on Another List
Reference Type Still Needs Pass by Ref
How to Support Listbox Selecteditems Binding with Mvvm in a Navigable Application
Click Through Transparency for Visual C# Window Forms
Loop Through All Controls of a Form, Even Those in Groupboxes