- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个图像,您在其上单击 2 个点,它会在每个点之间创建一条线。最后,该部门要求的是能够计算这些线的长度以及这些线出现的位置。他们目前正在用纸/笔/尺子手工完成这项工作。这是我正在处理的图像之一。
中间的那些裂缝被认为是“区域 7”。
所以我需要一种方法,除了保存我的 x 和 y 位置以便稍后测量线条外,还将它们出现的位置添加到我的列表中。以下是我知道的唯一方法,但事实证明它更乱。因为图片不是精确的正方形或长方形,所以有很多重叠区域和很多区域会有空隙,这些区域不会被很好地覆盖。
有更好的方法吗? (目前我只是使用一个消息框来显示我点击的位置,在我做对之前我还没有对数据做任何事情。)
if (e.Button.Equals(MouseButtons.Left))
{
Rectangle zone1 = new Rectangle(35, 30, 770, 30);
if (zone1.Contains(e.Location))
{
MessageBox.Show("Zone1");
}
Rectangle zone2 = new Rectangle(890, 40, 330, 300);
if (zone2.Contains(e.Location))
{
MessageBox.Show("Zone2");
}
Rectangle zone3 = new Rectangle(340, 340, 850, 60);
if (zone3.Contains(e.Location))
{
MessageBox.Show("Zone3");
}
Rectangle zone4 = new Rectangle(100, 25, 75, 300);
if (zone4.Contains(e.Location))
{
MessageBox.Show("Zone4");
}
//4-1 trying to cover areas missed in zone4
Rectangle zone41 = new Rectangle(255, 270, 120, 240);
if (zone41.Contains(e.Location))
{
MessageBox.Show("Zone4-1");
}
Rectangle zone5 = new Rectangle(310, 100, 180, 150);
if (zone5.Contains(e.Location))
{
MessageBox.Show("Zone5");
}
//5-1 trying to cover areas missed in zone5
Rectangle zone51 = new Rectangle(220, 80, 60, 45);
if (zone51.Contains(e.Location))
{
MessageBox.Show("Zone5-1");
}
Rectangle zone6 = new Rectangle(635, 35, 250, 210);
if (zone6.Contains(e.Location))
{
MessageBox.Show("Zone6");
}
}
最佳答案
我的建议是使用背景图像作为区域 map 。此图像根本不会显示给用户。您加载一次并将其保存在内存中,但继续显示原始图像并像往常一样在其上画线。然后,当用户点击您的图像时,您检查区域 map 的颜色以确定区域。
例如,假设我使用这两个图像作为我的显示和我的区域 map :
两个图像都加载到您的代码中,但只有显示 map 显示给用户:
class MyForm
{
Bitmap zoneDisplay;
Bitmap zoneMap;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
zoneDisplay = (Bitmap)Image.FromFile(@"c:\temp\zonedisp.png"); // replace with actual path to file
zoneMap = (Bitmap)Image.FromFile(@"c:\temp\zonemap.png");
// put the display image into the picturebox (or whatever control displays it)
pictureBox.Image = zoneDisplay;
}
然后,当用户点击您的图像时,只需检查区域 map 上的颜色:
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
var color = _zoneMap.GetPixel(e.X, e.Y);
if (color == Color.FromArgb(0, 0, 255))
MessageBox.Show("Zone 1");
else if (color == Color.FromArgb(255, 0, 0))
MessageBox.Show("Zone 2");
else if (color == Color.FromArgb(0, 255, 0))
MessageBox.Show("Zone 3");
// etc...
}
如果您的颜色略有偏差,那么您可能需要进行不太精确的比较。示例:
static int ColorDelta(Color c1, Color c2)
{
return Math.Abs(c1.R - c2.R) + Math.Abs(c1.G - c2.G) - Math.Abs(c1.B - c2.B);
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
var color = _zoneMap.GetPixel(e.X, e.Y);
if (90 > ColorDelta(color, Color.FromArgb(0, 0, 255)))
MessageBox.Show("Zone 1");
else if (90 > ColorDelta(color, Color.FromArgb(255, 0, 0)))
MessageBox.Show("Zone 2");
else if (90 > ColorDelta(color, Color.FromArgb(0, 255, 0)))
MessageBox.Show("Zone 3");
// etc...
}
关于c# - 有没有更好的方法来处理图像点击区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41575917/
据我所知,根本不为元素呈现 HTML,或添加 display:none,似乎具有完全相同的行为:两者都使元素消失并且不与 HTML 交互。 我正在尝试禁用和隐藏一个复选框。所以HTML的总量很小;我无
我刚刚读了Android Architecture Tutorial: Developing an App with a Background Service (using IPC) .基本上是 让服
我有两个查询具有相同的结果,现在我想知道哪个查询更优化? 在选择中: select t1.*, sum(t2.value) as total_votes from table1 t1 left joi
有人告诉我,对于 I/O 绑定(bind)的应用程序,非阻塞 I/O 会更好。对于 CPU 密集型应用程序,阻塞 I/O 会好得多。我找不到这种说法的原因。试过谷歌,但很少有文章只是触及这个话题而没有
我有一个算法可以在数字列表中寻找好的对。一个好的配对被认为是索引 i 小于 j 且 arr[i] 1: # Finding the mid of the array
我有一个算法可以在数字列表中寻找好的对。一个好的配对被认为是索引 i 小于 j 且 arr[i] 1: # Finding the mid of the array
我从 API 收到一个 json,我需要解析并修改一个属性值。问题是,我收到的 json 数据的嵌套结构不一致,我无法控制它。 这将禁止我指定在特定深度(如 parsedJson.children[0
我有 451 个城市的坐标。现在我想计算每个城市之间的距离,然后根据该距离对一些结果进行排序。现在我有两个选择: 我可以运行一个循环来计算每个可能的城市组合的距离并将它们存储到一个表中,这将产生大约
对于返回相同结果的不同查询,我有两个查询计划我想知道是否有人可以告诉我哪个“更好”,以及为什么。 SELECT * FROM bids order by (select ranking from us
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
我有一个二维数组。我需要尽可能快地对其执行一些操作(函数每秒将被调用十几次,所以让它变得高效会很好)。 现在,假设我想获取元素 A[i][j],简单地使用 A[i][j] 在速度上有什么不同吗和 *(
在声明或使用字符串的代码中,我通常会看到开发人员这样声明它: string randomString = @"C:\Random\RandomFolder\ThisFile.xml"; 代替: str
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why don't CSS resets use '*' to cover all elements? 我正
如果我有一个包含许多重复项的 python 列表,并且我想遍历每个项目,而不是重复项,最好使用一个集合(如 set(mylist),或者找到另一种方法来创建没有重复的列表?我想只是循环遍历列表并检查重
在阅读常量接口(interface)反模式时,我发现没有实例的最终常量类比常量接口(interface)更好。 请解释一下怎么做? public interface ConstIfc { publ
我正在查看我继承的一些旧代码,我真的不喜欢某些地方的风格。我真的不喜欢它的外观的一件事是: bool func() { bool ret = true; ret &= test1();
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
我经常发现自己试图使用 boost/QT 信号解耦对象。实现这一点的简单方法是针对我要通信的每个具体类型,创建一个新的信号和插槽签名并连接所有相关对象。这导致了访问者模式,理想情况下我想发出一个访问者
我正在 https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html 上阅读有关 lambda 的内容 在方法
public List getInts() { List xs = new ArrayList(); xs.add(1); // return Collections.unmo
我是一名优秀的程序员,十分优秀!