1

拡大縮小された方法でソースを示す画像があるとしましょう。MouseMove イベントを使用して、カーソルがあるピクセル位置をラベルまたはテキストブロックに表示するにはどうすればよいでしょうか?

(サイズに対する画像の座標ではなく、ピクセル座標が必要です)

前もって感謝します。

4

2 に答える 2

7

ImageSource から実際のピクセルの高さと幅を確認できます。

    ImageSource imageSource = image.Source;
    BitmapImage bitmapImage = (BitmapImage) imageSource ;

これで、イメージ コントロールにイメージが表示されました。マウスの位置をピクセル スケールに簡単にマッピングできます。

pixelMousePositionX = e.GetPosition(image).X * bitmapImage.PixelWidth/image.Width;
pixelMousePositionY = e.GetPosition(image).Y * bitmapImage.PixelHeight/image.Height;

楽しむ

ジョビ・ジョイ

于 2009-10-21T05:42:57.340 に答える
3

イメージの XAML が次のようになっている場合:

 <Border Grid.Row="1" Grid.Column="0" 
            BorderThickness="3" 
            BorderBrush="BlueViolet">
        <Image x:Name="Image_Box" 
               VerticalAlignment="Stretch"
               HorizontalAlignment="Stretch"
               Source="8.jpg"
               Stretch="Uniform"
               MouseMove="ImageBox_OnMouseMove"
               />
    </Border>

おそらくImageコントロールの幅は double.Nan なので、ActualWidthプロパティを使用する必要があります。したがって、コードは次のとおりです。

private void ImageBox_OnMouseMove(object sender, MouseEventArgs e)
    {
        ImageSource imageSource = Image_Box.Source;
        BitmapSource bitmapImage = (BitmapSource)imageSource;
        TextBoxCursor_X.Text =( e.GetPosition(Image_Box).X * bitmapImage.PixelWidth / Image_Box.ActualWidth).ToString();
        TextBoxCursor_Y.Text = (e.GetPosition(Image_Box).Y * bitmapImage.PixelHeight / Image_Box.ActualHeight).ToString();
    }
于 2015-10-09T05:01:25.097 に答える