C# winform Label背景透明



如图所示,想把label背景透明,只看得到文字。
可是按照网上方法,为啥
//pictureBox1.Controls.Add(label1);
//pictureBox2.Controls.Add(label2);
整个label就看不到了呢。。。
...全文
1096 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
不好意思,才来结贴,我那个没正常显示,主要原因是因为没有设置坐标。
易2017 2017-11-16
  • 打赏
  • 举报
回复
引用 8 楼 xuzuning 的回复:
            label1.BackColor = Color.Transparent; //设置为透明色背景
            label1.Parent = pictureBox1; //指定父容器
            label1.ForeColor = Color.White;
            label1.Text = "要干嘛?";
正解
  • 打赏
  • 举报
回复
label直接把text文本值清空就自动隐藏了
sanyuni 2017-11-14
  • 打赏
  • 举报
回复
将label的BackColor设置为Transparent
巴士上的邂逅 2017-11-14
  • 打赏
  • 举报
回复
可以不用label,在picturebox画上去
xuzuning 2017-11-14
  • 打赏
  • 举报
回复

            label1.BackColor = Color.Transparent; //设置为透明色背景
label1.Parent = pictureBox1; //指定父容器
label1.ForeColor = Color.White;
label1.Text = "要干嘛?";
a1_b1_c1 2017-11-14
  • 打赏
  • 举报
回复
五楼正解,刚刚试验了一下
  • 打赏
  • 举报
回复
同意楼上说法,Label设成Transparent透明后,显示的是父窗体背景,要想显示图片,就是指定Picturebox为父窗休
xuzuning 2017-11-14
  • 打赏
  • 举报
回复
label1.BackColor = Color.Transparent; //设置为透明色背景 label1.Parent = pictureBox1; //指定父容器 调整 label1.Location 相对于父容器
desperaso 2017-11-14
  • 打赏
  • 举报
回复
接上面,挺有意思的一个问题,刚刚试了一试,richtextbox不能转,只能截屏。 将窗体全部组件转图片:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace test1
{
    public partial class Form1 : Form
    {
        public class control_Info
        {
            public Bitmap control_Img;
            public Point control_Location;
            public Size control_Size;
        }
        private List<control_Info> _controlList = new List<control_Info>();
        private void getControl_Info(Bitmap control_Img, Point control_Location, Size control_Size)
        {
            _controlList.Add(new control_Info
            {
                control_Img = control_Img,
                control_Location = control_Location,
                control_Size = control_Size
            });
        }

        public Form1()
        {
            InitializeComponent();
        }

        public static Bitmap RtbToBitmap(int x, int y, RichTextBox rtb)
        {
            rtb.Update(); 
            Bitmap bmp = new Bitmap(rtb.Width, rtb.Height);
            using (Graphics gr = Graphics.FromImage(bmp))
            {
                gr.CopyFromScreen( new Point (x,y), Point.Empty, rtb.Size);
            }
            return bmp;
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            foreach (System.Windows.Forms.Control control in this.Controls)
            {
                if (control is RichTextBox)
                {
                    getControl_Info(RtbToBitmap(Location.X +control.Location.X,Location.Y + control.Location.Y, (RichTextBox)control), control.Location, 
                        control.Size);
                } 
                else
                {
                    Bitmap controlBmp = new Bitmap(control.Width, control.Height);
                    control.DrawToBitmap(controlBmp, control.ClientRectangle);
                    if (control is Label)
                    {
                        controlBmp.MakeTransparent();
                    }

                    getControl_Info(controlBmp, control.Location, control.Size);
                }
            }
            Bitmap block = new Bitmap(400, 400);
            Graphics bmp_block = Graphics.FromImage(block);

            //全部图片放到一个PictureBox里
            for (int i = 0; i < _controlList.Count; i++)
            {
                bmp_block.DrawImage(_controlList[i].control_Img, _controlList[i].control_Location);
            }
            PictureBox bp = new PictureBox();
            //bp.BackColor = Color.SteelBlue;
            bp.BackgroundImage = Image.FromFile("2.jpg");
            bp.Size = new Size(300, 300);
            bp.Location = new Point(200, 10);
            bp.Image = block;
            this.Controls.Add(bp);
        }
    }
}
desperaso 2017-11-14
  • 打赏
  • 举报
回复
放组件看楼上, 把label转为背景透明图片 label.DrawToBitmap(controlBmp, label.ClientRectangle); controlBmp.MakeTransparent();

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧