C# PictureBox 在另一个 PictureBox 之上

C# PictureBox on top of another PictureBox

我希望 pbGrade 位于 pbItemtype 之上(pb = 图片框)

 pbItemtype.BackColor = Color.Transparent;

 // Change parent for overlay PictureBox...
 pbItemtype.Parent = pbGrade;

我已经试过了,但是 pbItemtype 甚至没有出现,而且 2 个图片框改变了图像(pbItemtype 和 pbGrade)

最好的方法是在 Bitmap 对象中构建带有叠加层的图像 off-line,然后将构建的图像分配给 PictureBox 进行显示。

例如像这样:

int width = 100;
int height = 100;
Image image = new Bitmap(width, height);

using (var graphics = Graphics.FromImage(image))
{
    graphics.DrawImage(MyApplication.Properties.Resources.ImageItemX, new Rectangle(0, 0, width, height));
    graphics.DrawImage(MyApplication.Properties.Resources.ImageGradeZ, new Rectangle(0, 0, width, height));
}

myPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
myPictureBox.Image = image;

这假设 ImageItemXImageGradeZ 是具有透明背景的图像(例如 png)作为项目资源以这些名称导入。


例如给定这些资源

代码将产生这个:

其实你可以很容易地做到这一点,事实上你已经做到了

代码有效但是您还需要更正Location,因为嵌套的 PB 将保留它之前的代码,因此可能会偏离右下角,可能会留下新 Parent 的可见大小... :

pbItemtype.BackColor = Color.Transparent;

// Change parent for overlay PictureBox...
pbItemtype.Parent = pbGrade;
// Move it to the top left of the parent:
pbItemtype.Location = Point.Empty;  // or some other position..

透明度在 嵌套 控件时效果很好。但是,当 重叠 它们时,它不起作用!

(当然我们看到的代码会不会交换图片!)