c# delete Drawstring in picturebox

Asked

Viewed 159 times

1

I have a pictureBox where the user with each mouse click inserts a text on top of an image in a pictureBox. It inserts several texts at various points in the image, the inserted text is taken from checkboxes.

Using this code

 private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
    {
        var g = Graphics.FromImage(pictureBox2.Image);
        var mouseEventArgs = e as MouseEventArgs;
        if (mouseEventArgs != null) textBox1.Text = "X= " + mouseEventArgs.X + " Y= " + mouseEventArgs.Y;

        g.DrawString(texto, new Font("Verdana", 30F, FontStyle.Regular), Brushes.Red, new PointF(e.X * pictureBox2.Image.Width / pictureBox2.Width -30,
e.Y * pictureBox2.Image.Height / pictureBox2.Height -90));
        pictureBox2.Refresh();
        g.Dispose();
    }

What I’m not getting is to delete the texts. I wish he could delete anyone he inserted in any order. I thought of a kind of CTRL+Z but that would delete one by one, if he already inserted 5, and want to delete only the first, Ctrl+z would no longer serve.

I thought of a kind of eraser, that it was moving the mouse over the text that he inserted and was erasing, without erasing the background image. Or by the checkbox itself that he clicked to choose which text to write, if you click on the checkbox again the Drawstring that corresponds to that checkbox text delete.

Is it possible to delete these Drawstring? The easiest way possible, I’m beginner.

  • As in the other question, I have this form that I made a long time ago, see if it helps: https://pastebin.com/qCs9RF6v Obs. You have the designer file code and then the form code itself

1 answer

2


To make a rubber, I used the following command:

    private void Borracha(MouseEventArgs e)
    {
        int t = int.Parse(tam.ToString());
        var g = Graphics.FromImage(pic1.Image);
        g.FillEllipse(new SolidBrush(Color.Magenta), e.X - (t / 2), e.Y - (t / 2), t, t);
        Bitmap b = (Bitmap)pic1.Image;
        b.MakeTransparent(Color.Magenta);
        pic1.Image = b;
        g.Dispose();
    }
  • My friend, thank you again, it worked perfectly. The only thing I would like is that appeared the size of the shed, it erases just right but the user does not know the size of the rubber circle. Can you make a border appear or paint the circle that is rubber?? Thanks again.

  • I think to appear an edge, you would have to use the Graphics of the form, and a Drawellipse next to that code, or change the cursor, but I didn’t get to do that

Browser other questions tagged

You are not signed in. Login or sign up in order to post.