using AI.Common; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Threading; using Rect = AI.Common.Rect; namespace CameraImageCaptureTest { public class RoiInfo { public Rect RoiRect { get; set; } public System.Windows.Media.Brush Color { get; set; } public RoiInfo(Rect roi, System.Windows.Media.Brush color) { RoiRect = roi; Color = color; } } public class OrganInfo { public string OrganName { get; set; } public Rect BoundingBox { get; set; } public Point2D[][] Contours { get; set; } public System.Windows.Media.Brush Color => System.Windows.Media.Brushes.Green; public OrganInfo(string organName, Rect boundingBox, Point2D[][] contours) { OrganName = organName; BoundingBox = boundingBox; Contours = contours; } } public class MyTransform { public double Scale { get; set; } public double OffsetX { get; set; } public double OffsetY { get; set; } public MyTransform(double scale, double offsetX, double offsetY) { Scale = scale; OffsetX = offsetX; OffsetY = offsetY; } public System.Windows.Point Transform(System.Windows.Point point) { double x = OffsetX + Scale * point.X; double y = OffsetY + Scale * point.Y; return new System.Windows.Point(x, y); } public System.Windows.Rect Transform(System.Windows.Rect rect) { double left = OffsetX + Scale * rect.Left; double top = OffsetY + Scale * rect.Top; double width = Scale * rect.Width; double height = Scale * rect.Height; return new System.Windows.Rect(left, top, width, height); } } /// /// ImageCanvas.xaml 的交互逻辑 /// public partial class ImageCanvas : UserControl { #region private variables private Bitmap _imageRGB; private MyTransform _transformRGB; #endregion #region 用户界面响应 private void CanvasSizeChanged(object sender, System.Windows.SizeChangedEventArgs e) { UpdateRGBTransforms(); } #endregion #region public funcs public ImageCanvas() { InitializeComponent(); } public void UpdateRGBImage(Bitmap image) { if (_imageRGB != null) { _imageRGB.Dispose(); } _imageRGB = image.Clone(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), image.PixelFormat); // 更新缩放比例 UpdateRGBTransforms(); // 更新图像显示 var bitmapImage = BitmapToBitmapImage(image); Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { RGBImageShow.Source = bitmapImage; })); } #endregion #region private funcs private BitmapImage BitmapToBitmapImage(Bitmap img) { BitmapImage bmpimg = new BitmapImage(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { img.Save(ms, ImageFormat.Png); bmpimg.BeginInit(); bmpimg.StreamSource = ms; bmpimg.CacheOption = BitmapCacheOption.OnLoad; bmpimg.EndInit(); bmpimg.Freeze(); ms.Dispose(); } return bmpimg; } private void UpdateRGBTransforms() { if (_imageRGB != null) { // 使图片保持长宽比例,居中显示,需要平移和缩放 var imgWidth = _imageRGB.Width; var imgHeight = _imageRGB.Height; var canvasWidth = RGBImageCanvas.ActualWidth; var canvasHeight = RGBImageCanvas.ActualHeight; var scaleX = canvasWidth / imgWidth; var scaleY = canvasHeight / imgHeight; var scale = scaleX < scaleY ? scaleX : scaleY; double offsetX, offsetY; if (Math.Abs(scale - scaleX) < 0.0001) { offsetY = 0.5 * (scaleY - scale) * imgHeight; offsetX = 0; } else { offsetX = 0.5 * (scaleX - scale) * imgWidth; offsetY = 0; } _transformRGB = new MyTransform(scale, offsetX, offsetY); } } #endregion } }