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?
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.
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
You may also find Laurent Bugnion's multitouch behaviour worth a look for zooming images.