【问题标题】:Clicking on part of an image单击图像的一部分
【发布时间】:2011-10-29 22:21:22
【问题描述】:

很抱歉,如果之前有人问过这个问题,我尝试搜索并没有找到任何东西。

我正在创建一个 C# WPF 应用程序。

我有一个图像,例如一个男人。

有没有办法检测鼠标点击了男人身体的哪个部位?例如,如果鼠标点击他的手,我们会得到一个消息框:“你点击了手”?

我想把它分成随机的形状而不仅仅是矩形,所以鼠标点击必须是精确的。

非常感谢您的帮助

【问题讨论】:

  • 图片是静态的还是会经常变化?

标签: c# wpf image image-processing


【解决方案1】:

如果我是你,我会在图像顶部创建多个多边形并将其放置到正确的位置/形状,然后使它们透明并处理每个多边形的点击事件。

【讨论】:

  • 你能告诉我怎么做吗?我不是在寻找完整的代码,只是一个简单的例子会很棒。谢谢
【解决方案2】:

您可以将图像裁剪成单独的部分,然后将它们加载到窗口中,就像它是一个完整的图像一样。这不是最有效的方法,它只适用于静态图像(flash 视频非常适合动画“图像”),但它可以快速轻松地实现。如果您要使用 HTML,则该窗口必须使用 Web 浏览器工具箱项(除非您以某种方式编写自己的代码)。

【讨论】:

    【解决方案3】:

    当然,没问题。

    像这样使用 XAML:

    <Border BorderBrush="Gray" 
            BorderThickness="1" 
            Height="200" Width="200" 
            MouseMove="Border_MouseMove">
        <TextBlock Name="Jerry" />
    </Border>
    

    做这样的事情:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Border_MouseMove(object sender, MouseEventArgs e)
        {
            var _Control = (sender as System.Windows.Controls.Border);
            var _ControlLocation = _Control.PointToScreen(new Point(0, 0));
            var _MousePosition = MouseInfo.GetMousePosition();
            var _RelativeLocation = _MousePosition - _ControlLocation;
            this.Jerry.Text = _RelativeLocation.ToString();
        }
    }
    
    internal class MouseInfo
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        [return: System.Runtime.InteropServices.MarshalAs(
            System.Runtime.InteropServices.UnmanagedType.Bool)]
        internal static extern bool GetCursorPos(ref Win32Point pt);
    
        [System.Runtime.InteropServices.StructLayout(
            System.Runtime.InteropServices.LayoutKind.Sequential)]
    
        internal struct Win32Point
        {
            public Int32 X;
            public Int32 Y;
        };
    
        public static Point GetMousePosition()
        {
            Win32Point w32Mouse = new Win32Point();
            GetCursorPos(ref w32Mouse);
            return new Point(w32Mouse.X, w32Mouse.Y);
        }
    }
    

    我从Location of WPF control in window? 中提取了一些代码,显然您想要使用与边框不同的东西——您想要一个图像。你想要 MouseDown 而不是 MouseMove。但这是你需要的逻辑!

    要完成这项工作,您需要知道自定义图像上的 X 和 Y(例如,“头部”或“手臂”在哪里)。如果部分不是矩形,您将需要计算多边形。如果是,那么这应该可以帮助您完成 90% 的工作。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      相关资源
      最近更新 更多