WPF图像控件上的鼠标处理

时间:2018-06-20 04:19:10

标签: c# wpf emgucv

我在WPF中是个菜鸟。我正在创建一个WPF应用程序,并为EmguCV使用了一个image Processing库。我发现无法在ImageBox中使用WPF。因此,我使用NamedWindow显示image,然后决定使用Image Controlwindow上显示图像。我正在尝试在rectangle上绘制image,但在其他位置未绘制矩形。所以谁能告诉我代码中有什么问题。

基本上我想获取该图像的ROI。

编辑:- 我将Canvas放在grid内,并将Image Control放在Canvas

**我的XAML代码**

<Grid Margin="0,0,2,-1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="138*"/>
        <ColumnDefinition Width="139*"/>
    </Grid.ColumnDefinitions>
    <Button x:Name="button" Content="Convert" Margin="139.053,432.066,0,0" Click="button_Click" Height="27.934" VerticalAlignment="Top" HorizontalAlignment="Left" Width="113.947" Grid.Column="1"/>
    <Button x:Name="button1" Content="Load Palette" Margin="308,432.066,0,0" Click="button1_Click_1" Height="27.934" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75" Grid.ColumnSpan="2"/>
    <Button x:Name="button2" Content="Load Gray Image" Margin="48,432.066,0,0" Click="button2_Click" Height="27.934" VerticalAlignment="Top" HorizontalAlignment="Left" Width="104"/>

    <Canvas x:Name="MyCanvas" Margin="81,86.5,27.245,120.5" Grid.Column="1">
        <Image x:Name="image3" Height="263" Width="238"/>
    </Canvas>
    <Image x:Name="image1" HorizontalAlignment="Left" Height="263" Margin="10,86.5,0,0" VerticalAlignment="Top" Width="255.5"/>
    <Image x:Name="image2" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="393" Margin="308,10,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>

我的C#代码为

        private Boolean isdragging = false;
        private System.Windows.Point startPoint;
        private System.Windows.Point endPoint;
        private System.Windows.Shapes.Rectangle rect;  
   private void image3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        startPoint = e.GetPosition(MyCanvas);
        isdragging = true;
        if(rect != null)
            MyCanvas.Children.Remove(rect);
        rect = new System.Windows.Shapes.Rectangle
        {
            Stroke = System.Windows.Media.Brushes.LightBlue,
            StrokeThickness = 2
        };
        System.Windows.Controls.Canvas.SetLeft(rect, startPoint.X);
        System.Windows.Controls.Canvas.SetTop(rect, startPoint.Y);
        MyCanvas.Children.Add(rect);
    }

    private void image3_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        rect = null;
        isdragging = false;
        endPoint = e.GetPosition(MyCanvas);
    }
    private void image3_MouseMove(object sender, MouseEventArgs e)
    {
        if (isdragging == true)
        {
            var pos = e.GetPosition(MyCanvas);
            var x = Math.Min(pos.X, startPoint.X);
            var y = Math.Min(pos.Y, startPoint.Y);
            var w = Math.Max(pos.X, startPoint.X) - x;
            var h = Math.Max(pos.Y, startPoint.Y) - y;
            rect.Width = w;
            rect.Height = h;
            System.Windows.Controls.Canvas.SetLeft(rect, x);
            System.Windows.Controls.Canvas.SetTop(rect, y);
        }
    }

我在Event Handler上使用Canvas,但未显示rectangle

预先感谢

1 个答案:

答案 0 :(得分:1)

谢谢Clemens,我得到了答案,实际上我没有向image3 Image Control添加事件处理程序,这就是为什么它不显示输出。

<Canvas x:Name="MyCanvas" Margin="81,86.5,27.245,120.5" Grid.Column="1">
    <Image x:Name="image3" Height="263" Width="238" MouseLeftButtonDown="image3_MouseLeftButtonDown" MouseLeftButtonUp="image3_MouseLeftButtonUp" MouseMove="image3_MouseMove"/>
</Canvas>