Question

I have an Image as shown below. Using MatrixTransform I can easily zoom in and out. How can I detect if zooming is too small/large? E.g. I would like to limit zooming to 10-200%.

<ScrollViewer x:Name="scrollViewer" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
   <Border x:Name="border" ClipToBounds="True" Width="Auto" Height="Auto">
     <Image x:Name="image" Source="test.png"/>
   </Border>
 </ScrollViewer>

The actual transformation:

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

Matrix m = image.RenderTransform.Value;
if (e.Delta > 0)
    m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
else
    m.ScaleAtPrepend(1.0 / 1.1, 1.0 / 1.1, p.X, p.Y);

// How do I know if the zoom level is lower than 10% or higher than 200%?
image.RenderTransform = new MatrixTransform(m);
Was it helpful?

Solution

Unless you also rotate, Matrix.M11 (or M22) gives you the scaling factor.

Matrix m = image.RenderTransform.Value;
...
if (m.M11 >= 0.1 && m.M11 <= 2.0)
{
    image.RenderTransform = new MatrixTransform(m);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top