How to get the zoomed image bounds

为君一笑 提交于 2020-02-07 05:15:32

问题


I am performing zooming on the image, after zooming I need to find the current visible bounds (i.e.) zoomed area bounds.

Since I need to add another element (Rectangle or any shape)on the center of the image, so even after panning, it should add at the center of the zoomed image.

Need to find the bounds marked in the image.

Zoomed Image

  <Grid>      
   <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <ScrollViewer HorizontalScrollBarVisibility="Disabled" 
                  VerticalScrollBarVisibility="Disabled" >
        <Grid x:Name="grid1" Background="AliceBlue" MouseLeftButtonDown="Grid1_MouseDown"
              MouseMove="Grid1_MouseMove" MouseLeftButtonUp="Grid1_MouseLeftButtonUp">
            <Image x:Name="image1" Source="2.bmp"></Image>
        </Grid>
    </ScrollViewer>

    <Grid Grid.Row="1" VerticalAlignment="Center">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="100"/>
        </Grid.ColumnDefinitions>

        <Slider  Width="500" x:Name="slider" ValueChanged="Slider_ValueChanged" Minimum="-50" Maximum="400" Value="100" TickFrequency="10"></Slider>
        <TextBlock Grid.Column="1" Text="{Binding ElementName=slider, Path=Value}"></TextBlock>
    </Grid>

 MatrixTransform zoomMatrixTransform;
    Point start;
    Point origin;
    Matrix matrix;
   private void Slider_ValueChanged(object sender, 
         RoutedPropertyChangedEventArgs<double> e)
    {
        var zoomLevel = e.NewValue;
        var factor = zoomLevel / 100;
        Matrix matrix = Matrix.Identity;
        matrix.Scale(factor, factor);
        image1.RenderTransformOrigin = new Point(0.5, 0.5);
        zoomMatrixTransform = new MatrixTransform(matrix);
        image1.RenderTransform = zoomMatrixTransform;
    }

   private void Grid1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        start = e.GetPosition(grid1);
        origin = new Point(zoomMatrixTransform.Matrix.OffsetX, zoomMatrixTransform.Matrix.OffsetY);

        image1.CaptureMouse();
    }

    private void Grid1_MouseMove(object sender, MouseEventArgs e)
    {
        if (!image1.IsMouseCaptured) return;

        Point p = e.MouseDevice.GetPosition(image1);

        var rect2 = new Rect(image1.RenderSize);
        var bounds = 
        image1.TransformToAncestor(grid1).TransformBounds(rect2);

        matrix = zoomMatrixTransform.Matrix;
        Vector v = start - e.GetPosition(grid1);
        matrix.OffsetX = origin.X - v.X;
        matrix.OffsetY = origin.Y - v.Y;
        zoomMatrixTransform.Matrix = matrix;

        image1.RenderTransformOrigin = new Point(0.5, 0.5);
        image1.RenderTransform = zoomMatrixTransform;
    }

    private void Grid1_MouseLeftButtonUp(object sender, 
    MouseButtonEventArgs e)
    {
        image1.ReleaseMouseCapture();
    }

来源:https://stackoverflow.com/questions/58334133/how-to-get-the-zoomed-image-bounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!