void OnMouseDown(object sender, MouseButtonEventArgs mouseButtonEventArgs) { var position = mouseButtonEventArgs.GetPosition(ImageCanvas); if (CurrentClickPositionOnImageType == ClickPositionOnImageTypes.None) { return; } if (!AllowedRangeHelper.IsInAllowedRange(position, ImageCanvas.Width, ImageCanvas.Height, CurrentClickPositionOnImageType, ClickSequences)) { return; } var matchingInList = ClickSequences.FirstOrDefault(x => x.PositionType == CurrentClickPositionOnImageType); if (matchingInList == null) { throw new Exception("Click type could not be recognized."); } if (CurrentClickPositionOnImageType == ClickPositionOnImageTypes.RightEndOfRotatingDisc) { //get the left edge position var leftEdge = ClickSequences.FirstOrDefault(x => x.PositionType == ClickPositionOnImageTypes.LeftEndOfRotatingDisc); if (leftEdge == null) { throw new Exception("Left edge not found in the list."); } //get rotation angle to realign the left and right edges var rotationAngleToRealign = MainProcessor.GetRotationAngleToRealign(leftEdge, new ClickPositionOnImage { ClickXPos = position.X, ClickYPos = position.Y, PositionType = ClickPositionOnImageTypes.RightEndOfRotatingDisc, AllowedHeight = ImageCanvas.Height, AllowedWidth = ImageCanvas.Width }); //realign the x and y positions of the ends of the disc in line with the rotated image var newRightEdgePosition = RotationHelper.GetRotatedPosition(rotationAngleToRealign, position); if (newRightEdgePosition == null || !(0 <= newRightEdgePosition.Value.X && newRightEdgePosition.Value.X < ImageCanvas.Width) || !(0 <= newRightEdgePosition.Value.Y && newRightEdgePosition.Value.Y < ImageCanvas.Height)) { MessageBox.Show("Either the selection of the top left and right corners of the disc is wrong or the disc in the photo is in too much slanting."); return; } var leftEdgePositionBeforeRotation = new Point(leftEdge.ClickXPos, leftEdge.ClickYPos); var newLeftEdgePosition = RotationHelper.GetRotatedPosition(rotationAngleToRealign, leftEdgePositionBeforeRotation); if (newLeftEdgePosition == null || !(0 <= newLeftEdgePosition.Value.X && newLeftEdgePosition.Value.X < ImageCanvas.Width) || !(0 <= newLeftEdgePosition.Value.Y && newLeftEdgePosition.Value.Y < ImageCanvas.Height)) { MessageBox.Show("Either the selection of the top left and right corners of the disc is wrong or the disc in the photo is in too much slanting."); return; } _rotationAngleToRealign = rotationAngleToRealign; _leftEdgePositionBeforeRotation = leftEdgePositionBeforeRotation; //the dimensions of the _currentImage and the image canvas are the same so rotation will work right var rotatedImg = MainProcessor.RotateImg(_currentImage, (float)_rotationAngleToRealign, _bkColor); ImageCanvas.SetImage(rotatedImg); //set the realigned click positions for top left edge and top right edge SetClickPositions(newRightEdgePosition.Value, matchingInList); SetClickPositions(newLeftEdgePosition.Value, leftEdge); //update left edge display ImageCanvas.RemoveDisplayObject(DisplayMarkerNames.LeftEndOfRotatingDisc); MarkPositionOnCanvas(newLeftEdgePosition.Value, ClickPositionOnImageTypes.LeftEndOfRotatingDisc); //update right edge display MarkPositionOnCanvas(newRightEdgePosition.Value, CurrentClickPositionOnImageType); } else { SetClickPositions(position, matchingInList); MarkPositionOnCanvas(position, CurrentClickPositionOnImageType); } SetNextClickPositionType(); }