C# 如何使图片框透明?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19910172/
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
How to make picturebox transparent?
提问by Belal Khan
I am making an application in C# .NET. I have 8 picture boxes in it. I used PNG images with transparent background but in my form it is not transparent when it comes above another image.
我正在用 C# .NET 制作一个应用程序。我有 8 个图片框。我使用了具有透明背景的 PNG 图像,但在我的表单中,当它位于另一个图像之上时它不透明。
I am using Visual Studio 2012. This is a screenshot of my form:
我正在使用 Visual Studio 2012。这是我的表单的屏幕截图:
采纳答案by SuperPrograman
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:
一种方法是将重叠图片框的父级更改为它正在重叠的图片框。由于 Visual Studio 设计器不允许您将图片框添加到图片框,因此必须在您的代码 (Form1.cs) 和 Intializing 函数中完成此操作:
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:
只需将图片框名称更改为您需要的名称即可。这应该返回:
回答by Sudhakar Tillapudi
you can set the PictureBox
BackColor
proprty to Transparent
您可以将属性设置PictureBox
BackColor
为Transparent
回答by tttony
回答by Hamed Mirzaei
I've had a similar problem like this. You can not make Transparent picturebox easily such as picture that shown at top of this page, because .NET Framework and VS .NET objects are created by INHERITANCE! (Use Parent Property).
我遇到过类似的问题。您不能像本页顶部显示的图片那样轻松制作透明图片框,因为 .NET Framework 和 VS .NET 对象是由 INHERITANCE 创建的!(使用父属性)。
I solved this problem by RectangleShape
and with the below code I removed background,
if difference between PictureBox
and RectangleShape
is not important and doesn't matter, you can use RectangleShape
easily.
我通过RectangleShape
下面的代码解决了这个问题,我删除了背景,如果PictureBox
和之间的区别RectangleShape
不重要并且无关紧要,您可以RectangleShape
轻松使用。
private void CreateBox(int X, int Y, int ObjectType)
{
ShapeContainer canvas = new ShapeContainer();
RectangleShape box = new RectangleShape();
box.Parent = canvas;
box.Size = new System.Drawing.Size(100, 90);
box.Location = new System.Drawing.Point(X, Y);
box.Name = "Box" + ObjectType.ToString();
box.BackColor = Color.Transparent;
box.BorderColor = Color.Transparent;
box.BackgroundImage = img.Images[ObjectType];// Load from imageBox Or any resource
box.BackgroundImageLayout = ImageLayout.Stretch;
box.BorderWidth = 0;
canvas.Controls.Add(box); // For feature use
}
回答by i31nGo
One fast solution is set image property for image1 and set backgroundimage property to imag2, the only inconvenience is that you have the two images inside the picture box, but you can change background properties to tile, streched, etc. Make sure that backcolor be transparent. Hope this helps
一个快速的解决方案是为 image1 设置 image 属性并将 backgroundimage 属性设置为 imag2,唯一的不便是你在图片框内有两个图像,但你可以将背景属性更改为平铺、拉伸等。确保背景色是透明的. 希望这可以帮助
回答by Stav Bodik
GameBoard is control of type DataGridView; The image should be type of PNG with transparent alpha channel background;
GameBoard 是 DataGridView 类型的控件;图像应为具有透明 Alpha 通道背景的 PNG 类型;
Image test = Properties.Resources.checker_black;
PictureBox b = new PictureBox();
b.Parent = GameBoard;
b.Image = test;
b.Width = test.Width*2;
b.Height = test.Height*2;
b.Location = new Point(0, 90);
b.BackColor = Color.Transparent;
b.BringToFront();