views:

55

answers:

3

I would like to display a larger image on a windows phone 7 device. I need to be able to zoom in and out using multi-touch gesture. I was wondering if there is any control that can do this out of the box in the Windows Phone 7 SDK?

+2  A: 

Hi,

You might be interested in DeepZoom. Not sure how well it supports multi-touch gesture out the box, but you can learn about using gestures here and simulating multi-touch here, if multi-touch support isn't standard. Here's an example video of DeepZoom on WP7.

keyboardP
+1 Thanks for your comment. The GalaSoft link (http://www.galasoft.ch/Touch/) is very usefull. Now I understand it is possible to add a behaviour to the control and enable the multitouch support on the control this way.
gyurisc
+3  A: 

If you don't want to use DeepZoom, you could also use a ViewBox to contain the Image, and listen for the pinch touch gestures/events and zoom in and out of the ViewBox using a RenderTransform.

Below is some code that I used for a Silverlight app, which with some work could be changed to react to Pinch and touch gestures instead of mousewheel + click/drag events. It might be possible to also change the amount of zoom depending on the "strength" of the pinch gesture.

For a viewbox defined in XAML:

    <Border Name="viewboxBackground" Background="Black">
            <Viewbox Name="viewboxMain">
                <!--your content here -->
            </Viewbox>
    </Border>   

Codebehind:

    #region Pan and Zoom Events and Handlers

    void MouseClickHandler(object sender, MouseButtonEventArgs e)
    {
        _mouseClickPos = e.GetPosition(viewboxBackground);
        bMoving = true;
    }

    void MouseMoveHandler(object sender, MouseEventArgs e)
    {

        if (bMoving)
        {
            //get current transform
            CompositeTransform transform = viewboxMain.RenderTransform as CompositeTransform;

            Point currentPos = e.GetPosition(viewboxBackground);
            transform.TranslateX += (currentPos.X - _mouseClickPos.X);
            transform.TranslateY += (currentPos.Y - _mouseClickPos.Y);

            viewboxMain.RenderTransform = transform;

            _mouseClickPos = currentPos;
        }
    }

    void MouseReleaseHandler(object sender, MouseButtonEventArgs e)
    {
        bMoving = false;
    }

    void MouseWheelZoom(object sender, MouseWheelEventArgs e)
    {
        if (e.Delta > 0)
        {
            _zoomMultiplier += _zoomRate;
            ApplyZoomTransform(viewboxMain, _zoomMultiplier, new Point(viewboxMain.ActualWidth / 2, viewboxMain.ActualHeight / 2));
        }
        else if (e.Delta < 0 && _zoomMultiplier > 1)
        {
            _zoomMultiplier -= _zoomRate;
            ApplyZoomTransform(viewboxMain, _zoomMultiplier, new Point(viewboxMain.ActualWidth / 2, viewboxMain.ActualHeight / 2));
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="element"></param>
    /// <param name="iZoomFactor"></param>
    /// <param name="zoomCenter">If provided, the zoom will be centered around the given position.</param>
    void ApplyZoomTransform(UIElement element, double iZoomFactor, Point? zoomCenter)
    {
        //get current transform
        CompositeTransform transform = viewboxMain.RenderTransform as CompositeTransform;

        if (zoomCenter != null)
        {
            transform.CenterX = zoomCenter.GetValueOrDefault().X;
            transform.CenterY = zoomCenter.GetValueOrDefault().Y;
        }
        transform.ScaleX = iZoomFactor;
        transform.ScaleY = iZoomFactor;

        element.RenderTransform = transform;
    }

    #endregion
Blakomen
Please note that I haven't actually tested this on Windows Phone 7, but I'm pretty sure ViewBox and RenderTransform (which is what you really need to implement the zoom functionality) is available for use.
Blakomen
Thanks for the code sample and the suggestion of using the Viewbox control. Maybe the use of ViewBox and adding the multi-touch behaviour is the way I need to use this. I will report back after trying the combination of these techniques.
gyurisc
Someone pointed out to me that the code above is missing the constructor for the CompositeTransform, so the code above will complain at the line: "CompositeTransform transform = viewboxMain.RenderTransform as CompositeTransform;" - to fix this, you can just create a CompositeTransform() at some point (maybe in the page constructor) and assign it to the ViewBox :)
Blakomen
Sorry for the comment spam, but you might also find this example project useful - http://blogs.msdn.com/b/ptorr/archive/2010/08/31/sample-code-from-teched-new-zealand-talks.aspx (look for the Drag and Pinch gestures in part one). This link on using XNA on WP7 for touch gesture recognition might also be useful - http://blogs.msdn.com/b/nicgrave/archive/2010/07/12/touch-gestures-on-windows-phone-7.aspx
Blakomen
@Blakomen, thanks for the links
gyurisc
+1  A: 

You may also find Laurent Bugnion's multitouch behaviour worth a look for zooming images.

MultiTouch Behavior for Windows Phone 7

Mick N