c# Take text and insert on top of image

Asked

Viewed 964 times

0

I have a pictureBox, and I need the user to be able to make "markings" on the image. For example, the image is a human body, I need the user to click on the photo where the arm is, for example, a little arrow written "arm". When clicking on the leg, a little arrow appears where he clicked and a text on top written "leg" and so on.

I already have several checkboxes in the form, where each one already has all the texts that should appear in the image, such as arm, legs, head. I’d need a way to get this checkbox.Text when clicking the checkbox, and inserting this text into the photo, exactly where the user clicked.

And I need all this to be saved in the bank, that is, when he clicks save, these markings in the picture tmb have to be saved.

I was using the CreateGraphics() and DrawLine(), for the user to circulate the body parts, however, it does not save the risks drawn on top of the photo, and it is very limited just circular with the drawline, wanted a text in the photo.

Is it possible? I don’t even know how to research it better. Can anyone help?

  • still with Graphics, has Drawstring, and it is also with them that you will save the drawings, I will try to find a code I have here

  • But where is your problem? Can’t save the images in the database? Can’t get the mouse position? Make it clearer what you’re trying to do.

  • I want to insert a text (the text property of the checkbox) on top of an image in the place the user clicks. If he clicks the "arm" checkbox, then in the photo where the arm is, I want it to appear "arm" where he clicked. And at the end of it all, this image with these markings being saved in the bank, as if these texts were part of the image.

1 answer

0


You need to get the graphics from the image, not from the controls or form:

var g =  Graphics.FromImage(pictureBox1.Image);

This is a method I use to draw a circle on the image:

    private void Circulo(MouseEventArgs e)
    {
        var g = Graphics.FromImage(pic1.Image);
        g.Clear(Color.Transparent);
        SolidBrush br = new SolidBrush(panelColor.BackColor);
        Pen p = new Pen(br);
        p.Width = (float)tam;

        int W = e.Location.X - mouseDownPosition.X;
        int H = e.Location.Y - mouseDownPosition.Y;
        if (W > 0 && H > 0)
            g.DrawEllipse(p, mouseDownPosition.X, mouseDownPosition.Y, W, H);
        else if (W > 0 && H < 0)
        {
            H *= -1;
            g.DrawEllipse(p, mouseDownPosition.X, e.Location.Y, W, H);
        }
        else if (W < 0 && H > 0)
        {
            W *= -1;
            g.DrawEllipse(p, e.Location.X, mouseDownPosition.Y, W, H);
        }
        else
        {
            W *= -1;
            H *= -1;
            g.DrawEllipse(p, e.Location.X, e.Location.Y, W, H);
        }

        RefreshPictureBox();
        g.Dispose();
        p.Dispose();
        br.Dispose();
    }

and to call him:

        private void pictureBoxView_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
                Circulo(e);
        }

The Part of writing the text in the image:

g.DrawString("SEU TEXTO", new Font("Verdana", 10F, FontStyle.Regular), Brushes.Red, new PointF(0, 0));

where g is the object Graphics, of course

  • The text with Drawstring worked, but the ellipse was not transparent in the background, everything turned black, but anyway, half the problem was already, thank you my friend for the light q gave.

  • do not forget to mark as answer, but there must be something wrong, because the filled ellipse would be done with the Fillellipse and not Drawellipse, make sure it is like this

  • I had a question, if the user click for example on the leg instead of the arm, how to delete the text that Drawstring did? there is a eraser??

  • There are ways, but basically you create an image over each other for this, in several layers, then you can even do a Ctrl Z. This code I did a long time ago, and used only 3 layers. If you want I can give you the whole Form code, call in chat

  • Hi @Rovann Linhalis , I would like the code yes, but I do not know how you use the chat from here.

Browser other questions tagged

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