固定了可移动图像上的覆盖

时间:2018-09-25 20:05:18

标签: wpf

我正在尝试显示可以在WPF应用程序中平移/缩放的大图像。我想渲染一个框以突出显示图像的特定区域,并让这些框在平移和缩放时保持固定在图像上。

在大多数情况下,我正在工作。图像平移/缩放按预期方式工作,并且框随图像移动/调整大小。问题是我无法使框对齐基础图像上的正确点。

该应用程序的XAML如下。

    <Border x:Name="ImageBorder" ClipToBounds="True" Background="Gray">
        <Grid x:Name="ImageGrid" RenderTransformOrigin="0.5,0.5">
            <Grid.RenderTransform>
                <TransformGroup>
                    <TranslateTransform x:Name="ImageTranslate"/>
                    <ScaleTransform x:Name="ImageScale"/>
                </TransformGroup>
            </Grid.RenderTransform>
            <Image x:Name="Image" MouseWheel="Image_MouseWheel" MouseLeftButtonDown="Image_MouseLeftButtonDown" MouseLeftButtonUp="Image_MouseLeftButtonUp" MouseMove="Image_MouseMove"/>
            <Canvas x:Name="ImageOverlays"/>
        </Grid>
    </Border>

呈现框叠加层的代码如下,其中Area定义每个区域的行/列范围,以在源图像中突出显示。分辨率是要显示的源图像的大小,为正方形,其原点位于左下角。 c_size是超出范围的框的大小。

    private void ViewHighlight_Click(object sender, RoutedEventArgs e)
    {
        // If enabled, add a border around extents of each area
        if (ViewHighlight.IsChecked)
        {
            const int c_size = 10;
            foreach (Area a in Areas)
            {
                double left = ImageBorder.ActualWidth * ((a.ColMin - c_size) / (double)(Resolution - 1));
                double right = ImageBorder.ActualWidth * ((a.ColMax + c_size) / (double)(Resolution - 1));
                double bottom = ImageBorder.ActualHeight * ((Resolution - a.RowMin - c_size) / (double)(Resolution - 1));
                double top = ImageBorder.ActualHeight * ((Resolution - a.RowMax + c_size) / (double)(Resolution - 1));

                LineSegment line1 = new LineSegment();
                line1.Point = new Point(left, bottom);
                LineSegment line2 = new LineSegment();
                line2.Point = new Point(right, bottom);
                LineSegment line3 = new LineSegment();
                line3.Point = new Point(right, top);
                LineSegment line4 = new LineSegment();
                line4.Point = new Point(left, top);

                PathSegmentCollection segments = new PathSegmentCollection();
                segments.Add(line1);
                segments.Add(line2);
                segments.Add(line3);
                segments.Add(line4);

                PathFigure figure = new PathFigure();
                figure.StartPoint = new Point(left, top);
                figure.Segments = segments;

                PathFigureCollection figures = new PathFigureCollection();
                figures.Add(figure);

                PathGeometry geometry = new PathGeometry();
                geometry.Figures = figures;

                System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
                path.Stroke = Brushes.White;
                path.StrokeThickness = 0.25;
                path.Data = geometry;
                ImageOverlays.Children.Add(path);
            }
        }

        // Otherwise, remove the overlays
        else ImageOverlays.Children.Clear();
    }

任何帮助我弄清楚我做错了什么。

0 个答案:

没有答案