0

I have a C# WindowsForms program where the user clicks on a PictureBox control. I want a small red character every time the user clicks. I also don't want any of the previous character to go away.

  • Hello and welcome to StackOverflow. To maximize chances you will receive help you need please provide the code you already have, according to [Minimal, Complete and Verifiable example](https://stackoverflow.com/help/mcve). This will help others understanding what issue are you facing. – Asunez Oct 18 '18 at 06:42
  • You must choose: You can either draw the letters __on top__ of the pbox or __into the image__ the pbox displays. Do do the former persistently you must create a class to hold the data (a Point and a string, maybe a color etc..) and in the Paint event draw them all. Create a List and Add to it upon each mouseclick; then Invalidate the pbox! - For the latter you use a graphics object created from the image, drawstring and then re-assign the image.[Example code](https://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?s=3|20.7818#27341797) – TaW Oct 18 '18 at 09:13
  • Also note: Drawing on top of the control will work directly. But in order to draw into the Image you may need to calculate the image coordinates if the sizemode is not normal.. – TaW Oct 18 '18 at 09:20
  • @TaW i have car image and i want mark some image area with 'x' character by mouse click – meysam rahpeyma Oct 18 '18 at 11:05
  • Ok. If it shall always be an 'x' and always the same color, all you need to store is the location. So a `List` or maybe `List` will do. Did you study the link? – TaW Oct 18 '18 at 11:13
  • You could adapt this one: [How to draw small dot on every click of the mouse in WinForms PictureBox](https://stackoverflow.com/questions/52599453/how-to-draw-small-dot-on-every-click-of-the-mouse-in-winforms-picturebox?answertab=active#tab-top) – Jimi Oct 18 '18 at 11:30

1 Answers1

0

yeeeeeeeeeeeeeeeeeeaaaaaaaaaaaaaaaaaaaaah. i found it. before write this code you must set your BackgroundImage of pictureBox to your Image that you want to mark it by "x" or "z".

public static Graphics g;
public static Bitmap bmp;

DataTable dt = new DataTable();

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (dt.Columns.Count == 0)
    {
        dt.Columns.Add("xPoint", typeof(float));
        dt.Columns.Add("yPoint", typeof(float));
        dt.Columns.Add("character", typeof(string));
    }
    else
    {
        if (lblX.ForeColor == Color.Red)
        {
            dt.Rows.Add(e.Location.X - 5, e.Location.Y - 10, "x");
        }
        else if (lblZ.ForeColor == Color.Red)
        {
            dt.Rows.Add(e.Location.X - 5, e.Location.Y - 10, "z");
        }
        if (lblX.ForeColor == Color.Red || lblZ.ForeColor == Color.Red)
        {
            bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = Graphics.FromImage(bmp);
            DataRow lastRow = dt.Rows[dt.Rows.Count - 1];
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            foreach (DataRow row in dt.Rows)
            {
                g.DrawString(row[2].ToString(), new Font("Tahoma", 12), Brushes.Red, float.Parse(row[0].ToString()), float.Parse(row[1].ToString()));
            }
            g.Flush();
            pictureBox1.Image = bmp;
        }
    }
}