Search code examples
c#windows-phone-7windows-phone-8windows-phonescale

How to make solution for scale image in one axis for Windows Phone?


I want that user can scale image in one axis and user can scale image in proportion.

I have xaml::

<Image Source="my_image.jpg" ManipulationDelta="UIElement_OnManipulationDelta" Width="400" Height="400">
 <Image.RenderTransform>
  <ScaleTransform x:Name="scaleImage" CenterX="200" CenterY="200"></ScaleTransform>
 </Image.RenderTransform>
</Image>

I have code:

 private void UIElement_OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            //scale image by only x
            if (e.DeltaManipulation.Scale.X != 0 && e.DeltaManipulation.Scale.Y == 1)
            {
                    scaleImage.ScaleX *= e.DeltaManipulation.Scale.X;
            }
            //scale image by only y
            else if (e.DeltaManipulation.Scale.X == 1 && e.DeltaManipulation.Scale.Y != 0)
            {
                    scaleImage.ScaleY *= e.DeltaManipulation.Scale.Y;
            }
             //proportion scale
            else if (e.DeltaManipulation.Scale.X != 0 && e.DeltaManipulation.Scale.Y != 0)
            {                   
                    scaleImage.ScaleX *= e.DeltaManipulation.Scale.X;

                    scaleImage.ScaleY *= e.DeltaManipulation.Scale.X;

            }

        }

but this code works is very unstable.

How to improve this solution?


Solution

  • I have wrote my own solution. WindowsPhone Toolkit is used in solution (toolkit:GestureService.GestureListener)

    Xaml:

    <Image Name="my_image" Width="450" Height="450" Source="my_image.jpg" CacheMode="BitmapCache">
     <Image.RenderTransform>
      <CompositeTransform CenterX="225" CenterY="225" x:Name="ImageTransformation" ScaleX="1" ScaleY="1" />
     </Image.RenderTransform>
     <toolkit:GestureListener PinchStarted="GestureListener_PinchStarted" PinchDelta="OnPinchDelta" />
     </toolkit:GestureService.GestureListener>
    </Image>
    

    c# code:

        private double InitialScale;
        private Point firstTouch; 
        private Point secondTouch;
    
    private void GestureListener_PinchStarted(object sender, PinchStartedGestureEventArgs e)
            {
                // Store the initial scaling
                InitialScale = ImageTransformation.ScaleX;
    
                firstTouch = e.GetPosition(photo, 0);
                secondTouch = e.GetPosition(photo, 1);
    
            }
    
            private void OnPinchDelta(object sender, PinchGestureEventArgs e)
            {
                double difX;
                double difY;
                if (firstTouch.Y >= secondTouch.Y)
                {
                    difY = firstTouch.Y - secondTouch.Y;
                }
                else
                {
                    difY = secondTouch.Y - firstTouch.Y;
                }
    
                if (firstTouch.X >= secondTouch.X)
                {
                    difX = firstTouch.X - secondTouch.X;
                }
                else
                {
                    difX = secondTouch.X - firstTouch.X;
                }
    
                if (difX <= difY)
                {
                    if (difX < 50)
                    {
                        ImageTransformation.ScaleY = InitialScale * e.DistanceRatio;
                    }
                    else
                    {
                        ImageTransformation.ScaleX = InitialScale * e.DistanceRatio;
                        ImageTransformation.ScaleY = InitialScale * e.DistanceRatio;
                    }
                }
                else
                {
                    if (difY < 50)
                    {
                        ImageTransformation.ScaleX = InitialScale * e.DistanceRatio;
                    }
                    else
                    {
                        ImageTransformation.ScaleX = InitialScale * e.DistanceRatio;
                        ImageTransformation.ScaleY = InitialScale * e.DistanceRatio;
                    }
                }
            }