如何让AutoSize为true的label在picturebox上面居中显示

2019-04-15 18:35发布

private void Form1_Load(object sender, EventArgs e) { PictureBox pic = new PictureBox(); Image img = Image.FromFile(@"C:UsersSaeWindDesktop1231.png"); pic.Image = (Image)img.Clone(); img.Dispose(); pic.Location = new Point(30,50); pic.Size = pic.Image.Size; Label lb = new Label(); lb.Text = "为哈佛撒谎发觉个"; Graphics g = Graphics.FromHwnd(lb.Handle); SizeF size=g.MeasureString(lb.Text, lb.Font); g.Dispose(); lb.Location = new Point(pic.Left + pic.Width / 2 - (int)size.Width / 2, pic.Top + pic.Height / 2 - (int)size.Height / 2); this.Controls.Add(lb); this.Controls.Add(pic); }