单击图像的一部分

时间:2011-08-18 15:48:45

标签: c# wpf image image-processing

如果之前有人询问我很抱歉,我试图搜索并找不到任何内容。

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

我有一张图片,例如一个男人。

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

我想将其划分为随机形状而不仅仅是矩形,因此鼠标点击必须精确。

非常感谢您的帮助

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

当然,没问题。

使用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%。

祝你好运!

答案 2 :(得分:1)

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

相关问题