ComponentOne Imaging for UWP
Step 3 of 4: Adding Code for Image Cropping
Imaging for UWP > Bitmap > Bitmap for UWP Quick Start > Step 3 of 4: Adding Code for Image Cropping

The code in this step will load the default image and allow the user to crop it.
 Follow these steps:

  1. Open the MainPage.xaml.cs file and add the following using (Imports in Visual Basic) statements.
    C#
    Copy Code
    using C1.Xaml;
    using C1.Xaml.Imaging;
    using System.IO;
    
  2. Add the following code to load a default image and define cropping:
    C#
    Copy Code
    public partial class MainPage : UserControl
        {
            C1Bitmap bitmap = new C1Bitmap();
            Rect selection;
    
            public MainPage()
            {
                InitializeComponent();
                LoadDefaultImage();
                image.Source = bitmap.ImageSource;
                var mouseHelper = new C1DragHelper(imageGrid);
                mouseHelper.DragStarted += OnDragStarted;
                mouseHelper.DragDelta += OnDragDelta;
            }
            void OnDragDelta(object sender, C1DragDeltaEventArgs e)
            {
                var transform = Window.Current.Content.TransformToVisual(image);
                var start = transform.TransformPoint(_startPosition);
                var end = transform.TransformPoint(e.GetPosition(null));
                start.X = Math.Min((double)Math.Max(start.X, 0), bitmap.Width);
                end.X = Math.Min((double)Math.Max(end.X, 0), bitmap.Width);
                start.Y = Math.Min((double)Math.Max(start.Y, 0), bitmap.Height);
                end.Y = Math.Min((double)Math.Max(end.Y, 0), bitmap.Height);
                selection = new Rect(new Point(
                    Math.Round(Convert.ToDouble(Math.Min(start.X, end.X))),
                    Math.Round(Convert.ToDouble(Math.Min(start.Y, end.Y)))),
                    new Size(Convert.ToDouble(Math.Round(Math.Abs(start.X - end.X))),
                        Convert.ToDouble(Math.Round(Math.Abs(start.Y - end.Y)))));
                UpdateMask();       
    }
            void UpdateMask()
            {
                topMask.Height = selection.Top;
                bottomMask.Height = bitmap.Height - selection.Bottom;
                leftMask.Width = selection.Left;
                rightMask.Width = bitmap.Width - selection.Right;
            }
           
            void LoadDefaultImage()
            {
                Assembly asm = typeof(Crop).GetTypeInfo().Assembly;
                Stream stream = asm.GetManifestResourceStream("ImageSamplesLib2012.Resources.Lenna.jpg");
                LoadImageStream(stream);
            }
           void LoadImageStream(Stream stream)
            {
                bitmap.SetStream(stream);
               
                imageGrid.Width = bitmap.Width;
                imageGrid.Height = bitmap.Height;
                selection = new Rect(0, 0, bitmap.Width, bitmap.Height);
                UpdateMask();
            }
        }
    

In the next step you will run the application.

See Also