Hi jemediah, I had trouble following your terse notation, so I was very glad to see your VB.Net version of it. Unfortunately, I have been unable to get it to work, even after much trying of variations. I went on to make a further attempt at using Lenggries' approach, which I find easier to visualize. That too was unsuccessful despite much effort.
At a certain point, I realized that the WPF Mouse.GetPosition(UIElement) returns the image point regardless of the transformations applied. This prompted me to bite the bullet and start using WPF transforms as apparently intended. I ended up with the following code, which now includes the MouseWheel sub to make the usage clearer.
Does it work? Yes, it does - but only for a while. Zooming in and out to the mouse position starts off working perfectly but becomes increasingly unstable. Zooming out is particularly sensitive, and at a certain point the reduced image suddenly flits off to the nether regions of cyberspace, never to return. This is quite different from my earlier efforts, where the aberrant behaviour is immediately visible.
Private Sub _Image_MouseWheel(sender As Object, e As System.Windows.Input.MouseWheelEventArgs) Handles Canvas1.MouseWheel
Select Case _MouseWheelMode
Case WheelMode.ZoomToImageCentre, WheelMode.ZoomToMousePos
_ZoomFactor *= 1 + e.Delta / 4000
_Rotation += e.Delta / 1000
Private Sub ZoomRotateImage()
Dim centre As New Point(_Image.Width / 2, _Image.Height / 2)
Dim zoomFocus As Point
If _MouseWheelMode = WheelMode.ZoomToMousePos Then
zoomFocus = Mouse.GetPosition(_Image)
zoomFocus = centre
Dim tg As New TransformGroup
'zoom around zoom focus:
tg.Children.Add(New TranslateTransform(-zoomFocus.X, -zoomFocus.Y))
tg.Children.Add(New ScaleTransform(_ZoomFactor, _ZoomFactor))
tg.Children.Add(New TranslateTransform(zoomFocus.X, zoomFocus.Y))
'rotate around image centre:
tg.Children.Add(New RotateTransform(_Rotation, centre.X, centre.Y))
_Image.RenderTransform = tg
I'm certain that the problem is getting the focus from the transformed image (line 18). I guess it introduces a positive feedback resulting in chaotic behaviour. I have also tried clicking in the image to set the zoom focus. It's not what I want (the user should just point to the zoom focus without clicking) and it is shows has some wild behaviour (the image jumps to a wrong position after clicking or on zooming out). But I think it will be worth the effort of further debugging.