c# - 如何在图像框上添加标记?

标签 c#

public Form1()
{
    InitializeComponent();
    this.imgRoom.Click += new EventHandler(this.pictureBox1_Click);
}

private void pictureBox1_Click(object sender, EventArgs e)
{
    var label1 = new LabelControl();
    label1.Location = MousePosition;
    label1.BackColor = Color.Red;
    label1.Parent = imgRoom;
    label1.Text = "Point";
    imgRoom.Controls.Add(label1);
}

enter image description here

当我在所附屏幕上看到的地方单击时,点出现在另一个地方。如何解决这个问题呢?

最佳答案

MousePosition 在屏幕坐标中。您需要将其转换为客户端坐标。

private void pictureBox1_Click(object sender, EventArgs e)
    {
        var label1 = new LabelControl();
        label1.Location = imgRoom.PointToClient(MousePosition);  // changed here.
        label1.BackColor = Color.Red;
        label1.Parent = imgRoom;
        label1.Text = "Point";
        imgRoom.Controls.Add(label1);
    }

关于c# - 如何在图像框上添加标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45977013/

相关文章:

c# - 使用 WPFtoolkit 的多系列图表

c# - 如何为 DateTime 创建和使用自定义 IFormatProvider?

c# - ReferenceEquals 和 Nullable<T>

c# - 使用 MysqlConnector 从 C# 应用程序执行 MySql 存储过程很慢?

c# - 在虚拟模式下在 ListView 中分组

c# - 有没有办法在 C# 中使用 OpenXml 库来固定列的宽度?

c# - Xamarin - 如何淬火或过滤应用程序输出?

c# - 除了 xamarin 中的 Assets 之外,还可以使用代码加载关卡

C# 从嵌套数组中提取 JSON

c# - 修改 wix 制作的 bundle 二进制文件