/// <summary> /// 手动设定ROI区域 /// </summary> /// <param name="idx"></param> /// <param name="loca"></param> /// <param name="color"></param> public void SetRoi(string idx, RegLocation loca, Color color = default(Color)) { var localClient = new RegLocation(); if (ConvertToScaleImageClient(loca, ref localClient)) { Rectangle rect = null; if (_rectList.ContainsKey(idx)) { rect = _rectList[idx]; } if (_parent.Children.Contains(rect)) { _parent.Children.Remove(rect); rect = null; } InitRoiRect(color, ref rect); var tmp1 = new Rect(localClient.X, localClient.Y, localClient.Width, localClient.Height); Console.WriteLine($"NImg: loc -> {loca}"); Console.WriteLine($"NImg: -> {tmp1}"); var p1 = AssociatedObject.TranslatePoint(new Point(tmp1.X, tmp1.Y), _parent); var p2 = AssociatedObject.TranslatePoint(new Point(tmp1.X + tmp1.Width, tmp1.Y + tmp1.Height), _parent); SetRectangleOnParent(ref rect, new Rect(p1, p2)); _rectList[idx] = rect; _rectOri[idx] = loca; } }
private bool ConvertToScaleImageClient(RegLocation loca, ref RegLocation localClient) { bool res = false; var image = AssociatedObject.Source as BitmapSource; if (image != null) { var imagePixWidth = image.PixelWidth; var imagePixHeight = image.PixelHeight; var x = AssociatedObject.RenderSize.Width * loca.X / imagePixWidth; var y = AssociatedObject.RenderSize.Height * loca.Y / imagePixHeight; var w = AssociatedObject.RenderSize.Width * loca.Width / imagePixWidth; var h = AssociatedObject.RenderSize.Height * loca.Height / imagePixHeight; localClient.X = (int)x; localClient.Y = (int)y; localClient.Width = (int)w; localClient.Height = (int)h; res = true; } return(res); }
private void AssociatedObject_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (Mouse.LeftButton != MouseButtonState.Released) { return; } if (OnRectReleased != null) { var loc = new RegLocation(); if (GetRect(ref loc)) { OnRectReleased?.Invoke(loc); if (_rect != null) { _parent.Children.Remove(_rect); _rect = null; _startlocation = null; } } } _startlocation = null; AssociatedObject.Cursor = _cursor; }
public bool GetRect(ref RegLocation location) { if (!ChkRect()) { return(false); } var image = AssociatedObject.Source as BitmapSource; if (image != null) { var po = new Point(Canvas.GetLeft(_rect), Canvas.GetTop(_rect)); var po2 = _parent.TranslatePoint(po, AssociatedObject); var tmpRect = new Rect { X = po2.X, Y = po2.Y, Width = _rect.Width, Height = _rect.Height }; var imagePixWidth = image.PixelWidth; var imagePixHeight = image.PixelHeight; var roiX = (int)(tmpRect.X * imagePixWidth / AssociatedObject.RenderSize.Width); var roiY = (int)(tmpRect.Y * imagePixHeight / AssociatedObject.RenderSize.Height); var roiWidth = (int)(tmpRect.Width * imagePixWidth / AssociatedObject.RenderSize.Width); var roiHeight = (int)(tmpRect.Height * imagePixHeight / AssociatedObject.RenderSize.Height); location.X = roiX; location.Y = roiY; location.Width = roiWidth; location.Height = roiHeight; Console.WriteLine($"OImg: -> {location}"); Console.WriteLine($"OImg: Po2-> {tmpRect}"); return(true); } return(false); }
private void RenderRectangles() { foreach (var pair in _rectList) { var pElement = pair.Value; if (_rectOri.ContainsKey(pair.Key)) { var ori = _rectOri[pair.Key]; RegLocation loca = new RegLocation(); if (ConvertToScaleImageClient(ori, ref loca)) { var loc = AssociatedObject.TranslatePoint(new Point(loca.X, loca.Y), _parent); Canvas.SetLeft(pElement, loc.X); Canvas.SetTop(pElement, loc.Y); } } } }