Text on top/outside of picturebox

Joined
Jul 6, 2023
Messages
3
Reaction score
0
Hey all I am trying to figure out why my text is not showing past my picturebox:

2025-03-18 14_52_28-AgIji78J.png (208×203).png


The code I am using is this:

C#:
Image NewImage = Image.FromFile(imgSaveResized + newImgExt);
NewImage = NewImage.GetThumbnailImage((int)(NewImage.Width * 0.5), (int)(NewImage.Height * 0.5), null, IntPtr.Zero);
PictureBox P = new PictureBox();

P.SizeMode = PictureBoxSizeMode.AutoSize;
P.Image = NewImage;
P.Margin = new Padding(5, 5, 55, 35);   
P.Paint += new PaintEventHandler((sender, e) =>
{
    e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

    string text = "Text";

    Font myFont = new Font("Arial", 18, FontStyle.Italic, GraphicsUnit.Pixel);
    SizeF textSize = e.Graphics.MeasureString(text, Font);
    PointF locationToDraw = new PointF();
    locationToDraw.X = (P.Width / 2) - (textSize.Width / 2);
    locationToDraw.Y = (P.Height / 2) - (textSize.Height / 2)+85;

    e.Graphics.DrawString(text, myFont, Brushes.Red, locationToDraw);
});

flowLayoutStations.Controls.Add(P);

I know its because its being added to the picturebox (P) but i am not sure how to go about setting the picturebox and the behind so that the text can be dominant on top f it?
 

Attachments

  • 2025-03-18 11_12_10-Form1.png
    2025-03-18 11_12_10-Form1.png
    48 KB · Views: 5
Joined
Jul 4, 2023
Messages
589
Reaction score
78
Codes below are for illustration purposes (they may contain errors, it's just a concept, but it may be helpful)

Wrap PictureBox in a Panel and Draw on the Panel
C#:
Panel panel = new Panel();
panel.AutoSize = true;
panel.Margin = new Padding(5, 5, 55, 35);
panel.Paint += new PaintEventHandler((sender, e) =>
{
    e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

    string text = "Text";
    Font myFont = new Font("Arial", 18, FontStyle.Italic, GraphicsUnit.Pixel);
    SizeF textSize = e.Graphics.MeasureString(text, myFont);
 
    PointF locationToDraw = new PointF();
    locationToDraw.X = (panel.Width / 2) - (textSize.Width / 2);
    locationToDraw.Y = panel.Height - textSize.Height - 5; // Position at bottom

    e.Graphics.DrawString(text, myFont, Brushes.Red, locationToDraw);
});

PictureBox P = new PictureBox();
P.SizeMode = PictureBoxSizeMode.AutoSize;

Image NewImage = Image.FromFile(imgSaveResized + newImgExt);
NewImage = NewImage.GetThumbnailImage((int)(NewImage.Width * 0.5), (int)(NewImage.Height * 0.5), null, IntPtr.Zero);
P.Image = NewImage;

panel.Controls.Add(P);
flowLayoutStations.Controls.Add(panel);

Overlay a Label on the PictureBox
C#:
PictureBox P = new PictureBox();
P.SizeMode = PictureBoxSizeMode.AutoSize;

Image NewImage = Image.FromFile(imgSaveResized + newImgExt);
NewImage = NewImage.GetThumbnailImage((int)(NewImage.Width * 0.5), (int)(NewImage.Height * 0.5), null, IntPtr.Zero);
P.Image = NewImage;

Label lbl = new Label();
lbl.Text = "Text";
lbl.Font = new Font("Arial", 18, FontStyle.Italic);
lbl.ForeColor = Color.Red;
lbl.AutoSize = true;
lbl.BackColor = Color.Transparent;

// Positioning the label
lbl.Location = new Point(P.Width / 2 - lbl.Width / 2, P.Height - 30);

// Use a Panel to hold both PictureBox and Label
Panel panel = new Panel();
panel.AutoSize = true;
panel.Controls.Add(P);
panel.Controls.Add(lbl);

flowLayoutStations.Controls.Add(panel);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top