【问题标题】:How to draw small character(x or z) on every click of the mouse in winforms picturebox如何在winforms图片框中每次单击鼠标时绘制小字符(x或z)
【发布时间】:2019-03-22 22:10:21
【问题描述】:

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

【问题讨论】:

  • 您好,欢迎来到 StackOverflow。为了最大限度地提高您获得所需帮助的机会,请根据Minimal, Complete and Verifiable example 提供您已有的代码。这将有助于其他人了解您面临的问题。
  • 您必须选择:您可以在 pbox 的 顶部在 pbox 显示的图像中绘制字母。坚持做前者,你必须创建一个类来保存数据(一个点和一个字符串,也许是一种颜色等),并在 Paint 事件中将它们全部绘制出来。创建一个 List 并在每次鼠标单击时添加到它;然后使 pbox 无效! - 对于后者,您使用从图像创建的图形对象,拉绳然后重新分配图像。Example code
  • 另请注意:在控件顶部绘图将直接起作用。但是为了绘制到图像中,如果 sizemode 不正常,您可能需要计算图像坐标..
  • @TaW 我有汽车图像,我想通过鼠标单击用“x”字符标记一些图像区域
  • 好的。如果它始终是“x”并且始终是相同的颜色,那么您只需要存储位置即可。所以List<Point> 或者List<PointF> 就可以了。你研究过链接吗?

标签: c# winforms drawing picturebox


【解决方案1】:

是啊啊啊啊啊啊啊啊啊啊啊。我找到了。在编写此代码之前,您必须将 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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-08
    • 2013-01-18
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多