C# Picturebox Transparent Background Doesn't Seem to Work

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:

Sample Image

Punching a hole through both the picture box and the form requires a bigger weapon, Form.TransparencyKey

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 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:

Sample Image

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:

Sample Image


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 {}

Transparent PNG in PictureBox

It is difficult to make a control that is partially transparent.

What you should do is handle the lower PictureBox's Paint event (the one that does not need to be transparent), and draw the overlay image using e.Graphics.DrawImage(image, x, y). This will draw transparent and semi-transparent images correctly.

EDIT: In response to your comment, there's nothing wrong with calling the Invalidate method in the MouseMove event. However, you will notice some flickering. To solve the flickering, make a control that inherits PictureBox, and call SetStyle(ControlStyles.DoubleBuffered, true) in the constructor.

Pictureboxes with transparent background

Try use GraphicsPath if you want to draw text on trasparent background:

private void InBitmapZeichnen()
{
Graphics g1 = Graphics.FromImage(bmp12);
g1.PageUnit = GraphicsUnit.Pixel;
g1.SmoothingMode = SmoothingMode.AntiAlias;
//g1.InterpolationMode = InterpolationMode.HighQualityBilinear;

Font f = new Font("Verdana", 8f);
Font f1 = new Font("Verdana", 8f);
Font f2 = new Font("Verdana", 10, System.Drawing.FontStyle.Bold);
Brush b = new SolidBrush(Color.YellowGreen);
Brush b1 = new SolidBrush(Color.YellowGreen);
Pen PenRaster = new Pen(Color.Black, 0.1f);

if (mnuRaster.Checked == true)
{
float j = Rohrdurchmesser / (float)(trk.Value + 2);
//g1.SmoothingMode = SmoothingMode.HighSpeed;
for (int i = pic1.Width / (trk.Value + 2); i <= pic1.Width - pic1.Width / (trk.Value + 2); i += pic1.Width / (trk.Value + 2))
{
PointF PRaster1 = new PointF(i, 0);
PointF PRaster2 = new PointF(i, pic1.Bottom);
PointF PRaster3 = new PointF(0, i + 4);
PointF PRaster4 = new PointF(pic1.Right, i + 4);

using (var path = new GraphicsPath())
{
path.AddString((j).ToString("0") + " mm", f.FontFamily, (int)f.Style, f.Size, new Point(i + 5, 5), null);
path.AddString((j).ToString("0") + " mm", f.FontFamily, (int)f.Style, f.Size, new Point(5, i + 5), null);
g1.FillPath(b, path);
}

//g1.DrawString((j).ToString("0") + " mm", f, b, new PointF(i + 5, 5));
//g1.DrawString((j).ToString("0") + " mm", f, b, new PointF(5, i + 5));
g1.DrawLine(PenRaster, PRaster1, PRaster2);
g1.DrawLine(PenRaster, PRaster3, PRaster4);
j += Rohrdurchmesser / (float)(trk.Value + 2);
}
}
}

As you can see instead of DrawString i used FillPath.



Related Topics



Leave a reply



Submit