WPF のグリッドにイメージ コントロールを設定します。
<Window x:Class="Window.PhotoViewer"
Title="PhotoViewer" Height="768" Width="1024" BorderThickness="0,0,0,0">
<Grid Name="ViewerGrid">
<Image Name="imageBox" Width="980" Height="615" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,50,0,0" Stretch="None" />
</Grid>
関数TransformToAncestorを使用して imageBox の位置を取得します。
Point leftTop = imageBox.TransformToAncestor(this).Transform(new Point(-imageBox.ActualWidth/2, -imageBox.ActualHeight/2));
Point rightButtom = imageBox.TransformToAncestor(this).Transform(new Point(imageBox.ActualWidth/2, imageBox.ActualHeight/2));
次に、これらの 2 点を結ぶ線を引きます。
Line line = new Line();
line.Stroke = System.Windows.Media.Brushes.OrangeRed;
line.X1 = leftTop.X;
line.Y1 = leftTop.Y;
line.X2 = rightButtom.X;
line.Y2 = rightButtom.Y;
line.HorizontalAlignment = HorizontalAlignment.Center;
line.VerticalAlignment = VerticalAlignment.Center;
line.StrokeThickness = 5;
ViewerGrid.Children.Add(line);
結果が正しくありません。アライメントが中央のときにコントローラーの正しい位置を取得する方法は?