Add image to Label : Label « GUI Windows Forms « C# / CSharp Tutorial






Add image to Label
using System;        
using System.Drawing;
using System.Windows.Forms;

public class LabelImageAdding : Form
{
  Label lblEcho;
  TextBox txt;

  public LabelImageAdding()
  {
    Size = new Size(300,250);

    lblEcho = new Label();
    lblEcho.Parent = this;
    lblEcho.Text = "test";
    lblEcho.Location = new Point(0,0);
    lblEcho.AutoSize = true;
    lblEcho.BorderStyle = BorderStyle.Fixed3D;
    int yDelta = lblEcho.Height + 10;
    
    Image img = Image.FromFile("YourFile.bmp");
    Label lblImage = new Label();
    lblImage.Parent = this;
    lblImage.Location = new Point(250, 0);
    lblImage.Image = img;
    lblImage.Anchor = AnchorStyles.Top | AnchorStyles.Right;
    lblImage.Size = new Size(img.Width, img.Height);

    Label lblCaption = new Label();
    lblCaption.Parent = this;
    lblCaption.Text = "&Enter Text Here:";
    lblCaption.Size = new Size(lblCaption.PreferredWidth, lblCaption.PreferredHeight);
    lblCaption.Location = new Point(0, yDelta);
    lblCaption.BorderStyle = BorderStyle.FixedSingle;

    txt = new TextBox();
    txt.Parent = this;
    txt.Size = new Size(100,23);
    txt.Location = new Point(lblCaption.Width + 5, yDelta);
    txt.TextChanged += new System.EventHandler(txt_TextChanged);

  }

  static void Main() 
  {
    Application.Run(new LabelImageAdding());
  }

  private void txt_TextChanged(object sender, EventArgs e)
  {
    lblEcho.Text = txt.Text;
  }
}








23.7.Label
23.7.1.Label Text ChangeLabel Text Change
23.7.2.Add image to LabelAdd image to Label
23.7.3.Add Image in an ImageList to a LabelAdd Image in an ImageList to a Label
23.7.4.Generic event for a LabelGeneric event for a Label