diff --git a/XP.ImageProcessing.RoiControl/Controls/PolygonRoiCanvas.xaml b/XP.ImageProcessing.RoiControl/Controls/PolygonRoiCanvas.xaml index d5c590f..be035cd 100644 --- a/XP.ImageProcessing.RoiControl/Controls/PolygonRoiCanvas.xaml +++ b/XP.ImageProcessing.RoiControl/Controls/PolygonRoiCanvas.xaml @@ -17,28 +17,20 @@ - + - - - - - - - + + + - - - - + 0 && CanvasHeight > 0) + if (imageDisplayGrid == null || CanvasWidth <= 0 || CanvasHeight <= 0) { - // 使用 Dispatcher 延迟执行,确保布局已完成 - Dispatcher.BeginInvoke(new Action(() => - { - // 获取图像显示区域的实际尺寸 - double viewportWidth = imageDisplayGrid.ActualWidth; - double viewportHeight = imageDisplayGrid.ActualHeight; - - if (viewportWidth > 0 && viewportHeight > 0) - { - // 计算宽度和高度的缩放比例 - double scaleX = viewportWidth / CanvasWidth; - double scaleY = viewportHeight / CanvasHeight; - - // 选择较小的缩放比例,确保图像完全显示在窗口内(保持宽高比) - ZoomScale = Math.Min(scaleX, scaleY); - - // 居中显示由 Grid 的 HorizontalAlignment 和 VerticalAlignment 自动处理 - PanOffsetX = 0; - PanOffsetY = 0; - } - }), System.Windows.Threading.DispatcherPriority.Loaded); + ZoomScale = 1.0; + return; } + + // 延迟到布局完成后计算,确保 ActualWidth/Height 准确 + Dispatcher.BeginInvoke(new Action(() => + { + double viewW = imageDisplayGrid.ActualWidth; + double viewH = imageDisplayGrid.ActualHeight; + + if (viewW > 0 && viewH > 0) + { + ZoomScale = Math.Min(viewW / CanvasWidth, viewH / CanvasHeight); + } + else + { + ZoomScale = 1.0; + } + + PanOffsetX = 0; + PanOffsetY = 0; + }), System.Windows.Threading.DispatcherPriority.Render); } private void BtnZoomIn_Click(object sender, RoutedEventArgs e)