/// <span class="code-SummaryComment"><summary></span> /// Raised when the user has drawn a selection area /// <span class="code-SummaryComment"></summary></span> #endregion #region Ctor /// <span class="code-SummaryComment"><summary></span> /// Constructs a new SelectionCanvas, and registers the /// CropImage event /// <span class="code-SummaryComment"></summary></span> public SelectionCanvas() { // Создаем элемент просмотра изображения //imgSource = new BitmapImage(new Uri(ImgUrl)); img = new System.Windows.Controls.Image(); //img.Source = imgSource; //img.RenderTransform = new ScaleTransform(1.0, 1.0, 0, 0); //this.Width = imgSource.Width; //this.Height = imgSource.Height; this.Children.Add(img); // Создаем элемент управления прямоугольной рамкой выделения rubber = new RubberBandManager(); rubber.GuiBind(this); // Создаём элемент управления якорями рамки anchors = new AnchorManager(); anchors.GuiBind(this); // Инциализируем остальные поля m_IsOpened = false; // Переводим поле вывода в неактивный режим SetMode(DisplayCanvasModeID.Disabled); }
// Метод обновляет состояние якорей по заданным координатам // рамки выделения rubber и параметрам мышки e. Следует вызывать // из обработчика события мыши OnMouseMove(). public void Update(RubberBandManager rubber, Point mousePoint, bool IsMouseCaptured) { // Определяем радиус якорей System.Drawing.Rectangle clip = rubber.GetClientRect(); if (clip.Width < 15 || clip.Height < 15) { m_radius = 2.0; } else { m_radius = 3.0; } // Определяем координаты всех якорей Point topleft = rubber.currentTopLeftCorner; Point bottomright = rubber.currentBottomRightCorner; double widthStep = 0.5 * (bottomright.X - topleft.X); double heightStep = 0.5 * (bottomright.Y - topleft.Y); Point center = topleft; SetAnchorCenter(AnchorID.TopLeft, center); center.X = topleft.X + widthStep; center.Y = topleft.Y; SetAnchorCenter(AnchorID.TopCenter, center); center.X = topleft.X + 2 * widthStep; center.Y = topleft.Y; SetAnchorCenter(AnchorID.TopRight, center); center.X = topleft.X; center.Y = topleft.Y + heightStep; SetAnchorCenter(AnchorID.MiddleLeft, center); center.X = topleft.X + widthStep; center.Y = topleft.Y + heightStep; SetAnchorCenter(AnchorID.MiddleCenter, center); center.X = topleft.X + 2 * widthStep; center.Y = topleft.Y + heightStep; SetAnchorCenter(AnchorID.MiddleRight, center); center.X = topleft.X; center.Y = topleft.Y + 2 * heightStep; SetAnchorCenter(AnchorID.BottomLeft, center); center.X = topleft.X + widthStep; center.Y = topleft.Y + 2 * heightStep; SetAnchorCenter(AnchorID.BottomCenter, center); center = bottomright; SetAnchorCenter(AnchorID.BottomRight, center); // При необходимости обновляем индекс активного якоря if (!IsMouseCaptured) { // Отыскиваем ближайший к указателю мыши якорь AnchorID nearestID = AnchorID.None; double nearestDist = 1e6; foreach (KeyValuePair <AnchorID, Shape> record in m_anchors) { center = GetAnchorCenter(record.Key); double distX = Math.Abs(center.X - mousePoint.X); double distY = Math.Abs(center.Y - mousePoint.Y); double dist = Math.Sqrt(distX * distX + distY * distY); if (dist < nearestDist) { nearestDist = dist; nearestID = record.Key; } } // Сравниваем расстояние до ближайшего якоря с порогом if (nearestDist < 10) { m_activeAnchorID = nearestID; } else { m_activeAnchorID = AnchorID.None; } } // Обновляем цвета якорей в зависимости от их состояния foreach (KeyValuePair <AnchorID, Shape> record in m_anchors) { Color color; if (record.Key == m_activeAnchorID) { // Активный якорь color = IsMouseCaptured ? Colors.Blue : Colors.Red; } else { // Неактивный якорь color = m_normalColor; } record.Value.Stroke = new SolidColorBrush(color); } }