1

I have a user control which moves with my finger movement.The user control has its own manipulation delta set. My problem is , when My finger is on the user control and I am trying to move it up , I am having lag because it is not moving up with my finger. The user control appears when I long tap on my screen , the location of the user control is whereever my finger is pressed . It moves with my finger whereever I move my finger , but once my finger goes on the user control I am not having smooth movements .

Any help would be appreciated. The XAML is

<Page x:Name="page1" 
    x:Class="controlMagnifier.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:controlMagnifier"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="ParentGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" PointerReleased="ParentGrid_OnPointerReleased"  >
        <Canvas x:Name="InkPresenter" Height="auto" Width="auto">

            <Image Stretch="None" x:Name="image2"  >
                <Image.Source >
                    <BitmapImage UriSource="/Assets/wallpaper.jpg" />
                </Image.Source>
            </Image>
        </Canvas>

        <local:MagnifierUsercontrol  x:Name="MagnifyTip"  Visibility="Collapsed" ManipulationDelta="MagnifyTip_ManipulationDelta" ManipulationMode="All" >
            <local:MagnifierUsercontrol.RenderTransform>
                <CompositeTransform/>
            </local:MagnifierUsercontrol.RenderTransform>
        </local:MagnifierUsercontrol>

    </Grid>
</Page>

The CS Code Is

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Diagnostics;
using Windows.UI.Popups;
using Windows.UI.Input;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;



// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace controlMagnifier
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public const int XAxis = 105;
        public const int YAxis = 270;
        public Thickness margin;



        public WriteableBitmap CurrentBitmapObj, CurrentCroppedImage = null;
        public MainPage()
        {
            this.InitializeComponent();
            ParentGrid.Holding += Grid_Holding;
            image2.PointerMoved += InkCanvas_PointerMoved;
            image2.PointerReleased += ParentGrid_OnPointerReleased;
            image2.CacheMode=new BitmapCache();
            StoreCrrentImage();
        }



/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void Grid_Holding(object sender, HoldingRoutedEventArgs e)
        {
            //Point p;
            try
            {
                Point p = e.GetPosition(ParentGrid);

                double height = MagnifyTip.Height;
                double width = MagnifyTip.Width;

                margin = MagnifyTip.Margin;
                margin.Left = p.X - XAxis;
                margin.Right = p.Y - YAxis;
                MagnifyTip.Margin = margin;
               // MagnifyTip.Margin = new Thickness(p.X - XAxis, p.Y - YAxis, 0, 0);
                MagnifyTip.Visibility = Visibility.Visible;
            }
            catch (Exception)
            {

                throw;
            }


        }


        private  void InkCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
        {

            try
            {
                PointerPoint pt = e.GetCurrentPoint(image2);
                Point currentContactPt = pt.Position;


                double xValue = currentContactPt.X;
                double yValue = currentContactPt.Y;


                //  MagnifyTip.
                double height = MagnifyTip.Height;
                double width = MagnifyTip.Width;


                MagnifyTip.image1.ImageSource = CropBitmap(currentContactPt);

                MagnifyTip.Margin = new Thickness(xValue - XAxis, yValue - YAxis, 0, 0);

            }
            catch (Exception)
            {

                throw;
            }

            finally
            {e.Handled = true;}

        }

        private async void StoreCrrentImage()
        {

            try
            {
StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/wallpaper.jpg", UriKind.RelativeOrAbsolute));

                using (
                    Windows.Storage.Streams.IRandomAccessStream fileStream =
                        await storageFile.OpenAsync(FileAccessMode.Read))
                {
                    BitmapImage bitmapImage = new BitmapImage();

                    await bitmapImage.SetSourceAsync(fileStream);
                    WriteableBitmap writeableBitmap =
                        new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
                    fileStream.Seek(0);
                    await writeableBitmap.SetSourceAsync(fileStream);
                    CurrentBitmapObj = writeableBitmap;
                    writeableBitmap.Invalidate();
                }
            }
            catch (Exception )
            {

                throw;
            }


            finally
            {}

            }

        /// <summary>
        /// This method crops the image by accepting x and y as Arguments
        /// </summary>
        /// <param name="point"></param>
        /// <returns>Cropped Image</returns>
        private WriteableBitmap CropBitmap(Point point)
        {
            try
            {
                CurrentCroppedImage = CurrentBitmapObj.Crop(Convert.ToInt32(point.X), Convert.ToInt32(point.Y), 100, 100);
               // var resized = CurrentCroppedImage.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);

            }
            catch (Exception)
            {

                throw;
            }


            return CurrentCroppedImage;


        }

        private void MagnifyTip_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            Point p;
            p.X = e.Delta.Translation.X;
            p.Y = e.Delta.Translation.Y;
            var Dragme = (CompositeTransform)MagnifyTip.RenderTransform;
            Dragme.TranslateX += p.X;
            Dragme.TranslateY += p.Y;
            // MagnifyTip.controlCanvas.SetValue(Canvas.SetLeft, p.X);
        }




        private void ParentGrid_OnPointerReleased(object sender, PointerRoutedEventArgs e)
        {
            MagnifyTip.Visibility = Visibility.Collapsed;
           // throw new NotImplementedException();
        }
    }

}

The user control looks like the user control comes up just at the place of tapping and holding. Any movement will only happen while the finger is still tapped and moved around.

Apoorv
  • 2,023
  • 1
  • 19
  • 42

1 Answers1

0

If you want to avoid the lag which is intentionally implemented in ManipulationDelta to distinguish it from Tap, use PointerMove instead.

I have answered similar question here.

Community
  • 1
  • 1
Łukasz Rejman
  • 1,892
  • 1
  • 11
  • 18
  • Hi , Thanks for the reply. See , the whole control moves with my pointerpressed event that is on an image in the background. So would it be feasible for you to look into my XAML and C# and help me with that – Apoorv Mar 05 '15 at 11:33
  • I am stil unable to resolve it actually !! Its not working !! any help ? – Apoorv Mar 10 '15 at 07:57