C# 透明背景的图片框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12138892/
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 22:13:04  来源:igfitidea点击:

PictureBox with transparent background

c#winformstransparencypicturebox

提问by Saeid Yazdani

Possible Duplicate:
A PictureBox Problem

可能的重复:
图片框问题

I have a TabPagewhich hosts some custom PictureBoxcontrols. What I do here is drawing some IC packages which user can click on theire pins to get some info. User can add multiple of this pictureboxes to the tabpage and move them around in the tabpage.

我有一个TabPage托管一些自定义控件PictureBox。我在这里做的是绘制一些 IC 封装,用户可以点击它们的引脚来获取一些信息。用户可以将多个此图片框添加到标签页并在标签页中移动它们。

My problem is that this pictureboxes are transparent, only when they are not over each other. In the image below you see two of this pictureboxes added to tabpage. The tabpage has a blue background, in fact, it doesnt matter what color it has, the pictureboxes will have theire undrawn areas transparent to the tabpage:

我的问题是这个图片框是透明的,只有当它们不相互重叠时。在下图中,您会看到两个添加到标签页的图片框。tabpage 有一个蓝色的背景,事实上,不管它是什么颜色,pictureboxes 都会有它们的未绘制区域对 tabpage 透明:

enter image description here

在此处输入图片说明

But as soon as I move any of thise pictureboxes above another, it will not be transparent anymore:

但是一旦我将这些图片框中的任何一个移到另一个上方,它就不再透明了:

enter image description here

在此处输入图片说明

This is the code that produces raw Imagethat I will draw on it:

这是Image我将在其上绘制的生成原始数据的代码:

    //Setting up image area
    Image = new Bitmap(requiredImageWidth, requiredImageHeight, PixelFormat.Format32bppArgb);
    Image.MakeTransparent();

The rest are default settings I didnt change anything else. What do you think causing this behaviour?

其余的是默认设置,我没有更改任何其他内容。你认为是什么导致了这种行为?

采纳答案by Nasreddine

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.

WinForms 中的透明控件相对于它们的父控件是透明的,而不是其他控件。WinForms 中的透明度更像是伪装而不是真正的透明度。透明控件实际上并不能让您通过窗体看到它背后的控件。它要求其父级在“透明”控件上绘制自己的背景。这就是为什么透明控件显示其背后的表单,但掩盖了任何其他控件的原因。

There is an accepted answer to your problem here (A PictureBox Problem)

您的问题在这里有一个公认的答案(图片框问题)