From d5b421b811377646888a27a1cf34ce04e041bf25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=9F?= Date: Thu, 23 Apr 2026 16:52:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=83=8F=E7=B4=A0=E5=B0=BA?= =?UTF-8?q?=E5=AF=B8=EF=BC=8C=E9=81=BF=E5=85=8DDPI=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E5=AF=BC=E8=87=B4DIP=E5=B0=BA=E5=AF=B8=E4=B8=8E=E5=AE=9E?= =?UTF-8?q?=E9=99=85=E5=83=8F=E7=B4=A0=E4=B8=8D=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/PolygonRoiCanvas.xaml | 26 +++------ .../Controls/PolygonRoiCanvas.xaml.cs | 57 ++++++++++--------- 2 files changed, 40 insertions(+), 43 deletions(-) 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)