0

I want to add option to draw rectangle on image. But that is not problem. In order to get it working I need to use MouseDown, MouseUp and MouseLeave events of Image Control in WPF.

    <DockPanel Name="dockPanel1" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="15">
        <GroupBox Header="WebCam" Name="groupBox1" VerticalAlignment="Top" HorizontalAlignment="Left">
            <Grid>
                <Image HorizontalAlignment="Left" Margin="0" Name="pictureBoxMain" Stretch="Fill" VerticalAlignment="Top" MinHeight="240" MinWidth="320" StretchDirection="DownOnly" MaxWidth="960" MaxHeight="1200" MouseDown="pictureBoxMain_MouseDown" MouseUp="pictureBoxMain_MouseUp" MouseLeave="pictureBoxMain_MouseLeave" Panel.ZIndex="50" />
            </Grid>
        </GroupBox>
        <StackPanel HorizontalAlignment="Right" Name="stackPanel2" VerticalAlignment="Top" DockPanel.Dock="Right" Margin="15,0,0,0">
            <GroupBox Header="Controls" Name="groupBox2">
                <StackPanel Name="stackPanel1">
                    <Button Content="Start" Height="23" Name="buttonStartStop" Width="120" Margin="0" Click="buttonStartStop_Click" />
                    <Button Content="Video Properties" Height="23" Name="buttonVideoProperties" Width="120" Margin="0" Click="buttonVideoProperties_Click" />
                    <Button Content="WebCam Settings" Height="23" Name="buttonWebCamSettings" Width="120" Margin="0" Click="buttonWebCamSettings_Click" />
                </StackPanel>
            </GroupBox>
            <GroupBox Header="Motion Data" Name="groupBox3">
                <StackPanel>
                    <Label Content="Strength: 0" x:Name="labelMotionStrength" />
                    <Label Content="Count: 0" x:Name="timesWasMotionCounter" />
                </StackPanel>
            </GroupBox>
        </StackPanel>
    </DockPanel>

I'm talking about events of "pictureBoxMain". Why those events never fire? I'm clicking on my image like crazy and still nothing. What should I do to make those event working?

4

3 回答 3

1

Have you tried catching PreviewMouseDown?

To capture the same as MouseDown you need to use PreviewMouseDown event which is a tunneling event.
于 2012-09-13T20:24:40.657 回答
1

I found the error. For some reason my picture box size was 0,0. I stretched it and now it works.

于 2012-09-13T22:17:55.927 回答
0

You need to set the Source attribute of the Image to an image file or binding before the event handlers will work:

<Image HorizontalAlignment="Left" Margin="0" Name="pictureBoxMain" Stretch="Fill" VerticalAlignment="Top" MinHeight="240" MinWidth="320" StretchDirection="DownOnly" MaxWidth="960" MaxHeight="1200" MouseDown="pictureBoxMain_MouseDown" MouseUp="pictureBoxMain_MouseUp" MouseLeave="pictureBoxMain_MouseLeave" Source="test_image.png" Panel.ZIndex="50" />
于 2012-09-13T20:26:21.577 回答