C# 是否可以有两个重叠的带有透明图像的 PictureBox 控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11412169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:45:54  来源:igfitidea点击:

Is it possible to have two overlapping PictureBox controls with transparent images?

c#.netwinformspictureboxalpha-transparency

提问by Uwe Keim

Having two overlapping PictureBoxcontrols, I'm trying to make the transparent areas of the picture box let the controls below (in the z-order) being visible.

有两个重叠的PictureBox控件,我试图让图片框的透明区域让下面的控件(按 z 顺序)可见。

Even after trying what Microsoft suggests, I cannot get the desired result.

即使在尝试了 Microsoft 的建议之后,我也无法得到想要的结果。

This is what I currently have:

这是我目前拥有的:

enter image description here

在此处输入图片说明

And this is what I want:

这就是我想要的:

enter image description here

在此处输入图片说明

So my question is:

所以我的问题是:

Any way to achieve my desired result with two PictureBoxcontrols (or with another way) that overlap each other and let the transparent areas shine through?

有什么方法可以通过两个PictureBox相互重叠的控件(或另一种方式)来实现我想要的结果并让透明区域透过?

Update:

更新:

Actually I solved it by using this answerto the the SO question "Transparent images with C# WinForms".

实际上,我通过使用这个对 SO 问题“ Transparent images with C# WinForms”的答案解决了这个问题。

回答by Samy Arous

As far as I know, the transparency of a control depends on its parent control (As noted in the link you've given), meaning that in order to have the effect you are looking for, you need to have one picture box nested into another picture box which is impossible given that a picture box is not a container.

据我所知,控件的透明度取决于其父控件(如您提供的链接中所述),这意味着为了获得您正在寻找的效果,您需要将一个图片框嵌套到另一个图片框是不可能的,因为图片框不是容器。

You can however, use a custom container control instead of a picture box for the parent image. The most basic control would be a panel. Just set the background image of the control and put the second picture box in it.

但是,您可以为父图像使用自定义容器控件而不是图片框。最基本的控件是面板。只需设置控件的背景图像并将第二个图片框放入其中即可。

Another solution, would be to use one single picture box and manage the rendering manually.

另一种解决方案是使用单个图片框并手动管理渲染。

This is by far the best solution as the pseudo-simulated transparency of the other method is quiet inefficient.

这是迄今为止最好的解决方案,因为另一种方法的伪模拟透明度非常低效。

回答by Alessio Koci

Try this

尝试这个

private void Form1_Load(object sender, EventArgs e)
{
  // Transparent background...  
  pictureBoxOverlay.BackColor = Color.Transparent;

  // Change parent for overlay PictureBox...
  pictureBoxOverlay.Parent    = pictureBoxMain;

 // Change overlay PictureBox position in new parent...
 // pictureBoxOverlay.Location  = new Point(0, 0);
}

Result

结果

enter image description here

在此处输入图片说明

llink

链接