在gif顶部的Winforms标签



我想在winforms的PictureBox内的gif顶部放置一个标签。问题在于标签的背景是白色的。我希望它是透明的。

我的代码如下:

this.pictureBox = new PictureBox();
this.pictureBox.Image = Image.FromFile("my_background.gif");
this.pictureBox.Dock = DockStyle.Fill;
this.pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
this.label = new Label();
this.label.Text = "Hallo";
this.label.BackColor = Color.Transparent;
this.label.Location = new System.Drawing.Point(100, 100);

this.Controls.Add(this.label);
this.Controls.Add(this.pictureBox);

我的问题是标签有一个白色的背景,即使背景设置为透明。其他遇到类似问题的解决方案是将标签的父元素设置为图片框,如下所示:

this.label.parent = this.pictureBox;

但这并没有解决我的问题。还有其他方法可以做到这一点吗?

谢谢你的回答。

谢谢你的提示。

当在gif前只放置一个视图控件时它工作得很好。当添加到许多时,它变得非常错误,只有一些矩形部分会更新。

我通过将我的gif分割成单个图像并在窗体

的OnPaint事件中显示它们来解决这个问题
this.Paint += new PaintEventHandler(this.Paint_Background);

Paint Method是这样的

private void Paint_Background(object sender, PaintEventArgs e)
{
Graphics background_graphics = e.Graphics;
Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
int image_num = ((int)((DateTime.Now - start_time).TotalMilliseconds) / gif_speed) % num_images;
Image background= Image.FromFile("Filename_"+image_num+".jpg");
background_graphics.DrawImage(background, 0, 0, ClientSize.Width, ClientSize.Height);
background.Dispose();
}

我所有的控件都直接添加到视图中,而不再是imagebox,因为imagebox完全被删除了。

也许这也能帮助到别人

最新更新