如何在 winforms picturebox 中每次单击鼠标时绘制小字符(x 或 z)

How to draw small character(x or z) on every click of the mouse in winforms picturebox

我有一个 C# WindowsForms 程序,用户可以在其中单击 PictureBox 控件。 每次用户点击时我都想要一个红色的小字符。 我也不希望任何以前的角色消失。

yeeeeeeeeeeeeeeeeeeeeeeaaaaaaaaaaaaaaaaaaaaaah。我找到了。在编写此代码之前,您必须将 pictureBox 的 BackgroundImage 设置为要用 "x" 或 "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;
        }
    }
}