-4

I want to draw a editable TextBox on top of picture box and the user is allowed to enter text into this box.After entering text the text box should disappear and the text entered should be painted to the picture in the picture box.Please help me on this,I'am doing this in c#.

Bitmap myBitmap = new Bitmap("C:\\myImage.jpg");
Graphics g = Graphics.FromImage(myBitmap);
g.DrawString("My\nText", new Font("Tahoma", 20), Brushes.White, new PointF(0, 0));

Im stuck with this

rainbower
  • 141
  • 4
  • 14

3 Answers3

3

I think you are confusing "drawing" with the "editable" part.

It sounds like you just want to use a TextBox. A "basic" demonstration:

private Bitmap bmp = new Bitmap(256, 256);

private void Form1_Load(object sender, EventArgs e)
{
  pictureBox1.Image = bmp;
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
  TextBox txt = new TextBox();
  txt.Location = e.Location;
  txt.Width = 120;
  txt.Leave += new EventHandler(txt_Leave);
  pictureBox1.Controls.Add(txt);
}

void txt_Leave(object sender, EventArgs e)
{
  using (Graphics g = Graphics.FromImage(bmp))
  {
    g.DrawString(((TextBox)sender).Text, ((TextBox)sender).Font, Brushes.Black, ((TextBox)sender).Location);
  }
  ((TextBox)sender).Leave -= new EventHandler(txt_Leave);
  pictureBox1.Controls.Remove((TextBox)sender);
  ((TextBox)sender).Dispose();
  pictureBox1.Invalidate();
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
1

Sound like a TextBox control of MSPaint program, is it right?

Try this approach: http://bytes.com/topic/c-sharp/answers/230866-how-insert-text-bitmap-image-using-c

Hope this help.

Thinhbk
  • 2,194
  • 1
  • 23
  • 34
0

But in your code the only way to actually draw the text from textBox is to change focus on something else (e.g. by Tab key).

https://stackoverflow.com/a/7350238/2359840

Community
  • 1
  • 1
Pyramaze
  • 1
  • 2