private void DrawRectFromPixelRect() { if (PixelRectangle.Width == 0 || PixelRectangle.Height == 0) { Rectangle.Width = 0; Rectangle.Height = 0; return; } //Get Size in relation to current image size double?pixelX = PixelRectangle.GetValue(Canvas.LeftProperty) as double?; double?pixelY = PixelRectangle.GetValue(Canvas.TopProperty) as double?; if (!pixelX.HasValue || !pixelY.HasValue) { return; } Point imagePoint = Image.TranslatePoint(new Point(0, 0), Grid); double pixelWidth = PixelRectangle.Width; double pixelHeight = PixelRectangle.Height; double screenX = ((pixelX.Value / ImagePixelWidth) * Image.ActualWidth) + imagePoint.X; double screenY = ((pixelY.Value / ImagePixelHeight) * Image.ActualHeight) + imagePoint.Y; double screenWidth = (pixelWidth / ImagePixelWidth) * Image.ActualWidth; double screenHeight = (pixelHeight / ImagePixelHeight) * Image.ActualHeight; Rectangle.SetValue(Canvas.LeftProperty, screenX); Rectangle.SetValue(Canvas.TopProperty, screenY); Rectangle.Width = screenWidth; Rectangle.Height = screenHeight; }
private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { PixelRectangle.SetValue(Canvas.LeftProperty, 0d); PixelRectangle.SetValue(Canvas.TopProperty, 0d); PixelRectangle.Width = ImagePixelWidth; PixelRectangle.Height = ImagePixelHeight; DrawRectFromPixelRect(); }
private void UpdatePixelRect() { Point imageStartPoint = Grid.TranslatePoint(new Point(StartX, StartY), Image); double pixelStartX = (imageStartPoint.X / Image.ActualWidth) * ImagePixelWidth; double pixelStartY = (imageStartPoint.Y / Image.ActualHeight) * ImagePixelHeight; double pixelWidth = (Rectangle.Width / Image.ActualWidth) * ImagePixelWidth; double pixelHeight = (Rectangle.Height / Image.ActualHeight) * ImagePixelHeight; PixelRectangle.SetValue(Canvas.LeftProperty, pixelStartX); PixelRectangle.SetValue(Canvas.TopProperty, pixelStartY); PixelRectangle.Width = pixelWidth; PixelRectangle.Height = pixelHeight; }