123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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);
- }
- }
- /// <summary>
- /// ImageCanvas.xaml 的交互逻辑
- /// </summary>
- 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
- }
- }
|