Add a Label over Picturebox

30,371

Solution 1

While all these answers work, you should consider opting for a cleaner solution. You can instead use the picturebox's Paint event:

PB = new PictureBox();
PB.Paint += new PaintEventHandler((sender, e) =>
{
    e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    e.Graphics.DrawString("Text", Font, Brushes.Black, 0, 0);
});
//... rest of your code

Edit To draw the text centered:

PB.Paint += new PaintEventHandler((sender, e) =>
{
    e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

    string text = "Text";

    SizeF textSize = e.Graphics.MeasureString(text, Font);
    PointF locationToDraw = new PointF();
    locationToDraw.X = (PB.Width / 2) - (textSize.Width / 2);
    locationToDraw.Y = (PB.Height / 2) - (textSize.Height / 2);

    e.Graphics.DrawString(text, Font, Brushes.Black, locationToDraw);
});

Solution 2

Instead of

lblPB.Parent = PB;

do

PB.Controls.Add(lblPB);

Solution 3

I tried this. (no use picturebox)

  1. Use "Panel" control first
  2. Set panel's BackgroundImage & BackgroundImageLayout (Stretch)
  3. Add Label Inside panel

that's all

Solution 4

You have to add the control to the PictureBox. So:

PB.Controls.Add(lblPB):

EDIT:

I get blank page with no PictureBoxes.

You didn't see the picturebox because it has the same backcolor of the Form. So try to set BorderStyle and the BackColor. Another mistake is that probably you haven't set the location of the label. So:

PB.BorderStyle = BorderStyle.FixedSingle;
PB.BackColor = Color.White;
lblPB.Location = new Point(0,0);
Share:
30,371
Antonio Teh Sumtin
Author by

Antonio Teh Sumtin

Updated on July 05, 2022

Comments

  • Antonio Teh Sumtin
    Antonio Teh Sumtin almost 2 years

    I am trying to write some text over my picturebox so I thought the easiest and best thing to do is draw label over it. This is what I did:

    PB = new PictureBox();
    PB.Image = Properties.Resources.Image; 
    PB.BackColor = Color.Transparent;
    PB.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
    PB.Size = new System.Drawing.Size(120, 30);
    PB.Location = new System.Drawing.Point(100, 100);
    lblPB.Parent = PB;
    lblPB.BackColor = Color.Transparent;
    lblPB.Text = "Text";
    Controls.AddRange(new System.Windows.Forms.Control[] { this.PB });
    

    I get blank page with no PictureBoxes. What am I doing wrong?

    • Tergiver
      Tergiver about 12 years
      It's not the 'best' way, but it is the easiest. The 'best' way would be to handle the Paint event of the PictureBox and use e.Graphics.DrawText to paint the text.
    • ean5533
      ean5533 about 12 years
      Have you verified that the PictureBox shows up by itself if you completely remove the Label? Are you sure your code is even being called?
    • Antonio Teh Sumtin
      Antonio Teh Sumtin about 12 years
      Yes, I have verified and pb shows without label. Anyway I used paint event as suggested down and now am facing a problem of changing text's color, size, font and location
  • Kendall Frey
    Kendall Frey about 12 years
    It did, but not what you want. If the PictureBox doesn't appear on the Form, something is wrong with the code that you didn't post.
  • Antonio Teh Sumtin
    Antonio Teh Sumtin about 12 years
    Indeed, perfect, exactly what I needed, thank you man/woman :)
  • Antonio Teh Sumtin
    Antonio Teh Sumtin about 12 years
    although, I would love to know how to edit texts font, size and location using this
  • prestomanifesto
    prestomanifesto about 12 years
    Look into the DrawString method, font, size and location can all be set there. Just be careful about memory leaks (e.g. if you create a font, dispose it at some point)
  • Antonio Teh Sumtin
    Antonio Teh Sumtin about 12 years
    You've been most helpfull, still remains one question :D I want to center the text in my picture box... How do I do that? I've tried using width of the graphics but don't know how...