Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a user control . The user control has an image control inside. Presently I am using WriteableBitmapEx and cropping the background image and displaying inside the user control .
The actual requirement is to zoom in the pixels where ever the user control moves without compromising on the preformance of the application.

The application is a Windows 8.1 store app. find the complete code below.

C#
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;
            }

           
        }
///<Summary>
This is the event where I have to keep on dispalying the zoomed image on mouse move. The image should keep on changing with mouse move. ie it has to crop the background based on the mouse x and y coordinates.
       
        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();
        }
    }

}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900