123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO.Ports;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Threading;
- namespace IDInfoExtraction
- {
- public class RoiInfo
- {
- public Rect RoiRect { get; set; }
- public System.Windows.Media.Brush Color { get; set; }
- }
- 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 private variables
- #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.Opacity = 0.3; // 透明化
- RGBImageShow.Source = bitmapImage;
- }));
- }
- public Bitmap GetRGBImageAsBitmap()
- {
- return _imageRGB.Clone(new System.Drawing.Rectangle(0, 0, _imageRGB.Width, _imageRGB.Height), _imageRGB.PixelFormat);
- }
- #endregion public funcs
- #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);
- }
- }
- public void UpdateRoughPathResults(List<System.Drawing.Point> pathPts, string name)
- {
- Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
- {
- DrawContoursInCanvas(pathPts, name, System.Windows.Media.Brushes.Cyan);
- }));
- }
- public void ClearCanvas()
- {
- Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
- {
- // 清空画布上的所有内容
- RGBImageCanvas.Children.Clear();
- }));
- }
- /// <summary>
- /// 在画布上画出轮廓
- /// </summary>
- /// <param name="pts"></param>
- /// <param name="registerName"></param>
- private void DrawContoursInCanvas(List<System.Drawing.Point> pts, string registerName, System.Windows.Media.Brush color)
- {
- if (_transformRGB == null)
- {
- return;
- }
- // 已有则删除
- RemoveContoursInCanvas(registerName);
- if (pts == null)
- {
- return;
- }
- System.Windows.Media.PointCollection contourPoints = new PointCollection();
- foreach (var pt in pts)
- {
- var point = new System.Windows.Point(pt.X, pt.Y);
- var transPoint = _transformRGB.Transform(point);
- contourPoints.Add(transPoint);
- }
- // 添加第一个点 闭合矩形框
- var point0 = new System.Windows.Point(pts[0].X, pts[0].Y);
- var transPoint0 = _transformRGB.Transform(point0);
- contourPoints.Add(transPoint0);
- System.Windows.Shapes.Polyline contourDraw = new System.Windows.Shapes.Polyline();
- contourDraw.Points = contourPoints;
- contourDraw.StrokeThickness = 3;
- contourDraw.Stroke = color;
- RGBImageCanvas.Children.Add(contourDraw);
- RGBImageCanvas.RegisterName(registerName, contourDraw);
- }
- /// <summary>
- /// 从画布上将轮廓移除
- /// </summary>
- /// <param name="registerName"></param>
- public void RemoveContoursInCanvas(string registerName)
- {
- System.Windows.Shapes.Polyline contourExist = RGBImageCanvas.FindName(registerName) as System.Windows.Shapes.Polyline;
- if (contourExist != null)
- {
- RGBImageCanvas.Children.Remove(contourExist);
- RGBImageCanvas.UnregisterName(registerName);
- }
- }
- public void UpdateCamBox()
- {
- Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
- {
- // 清空画布上的所有内容
- RGBImageCanvas.Children.Clear();
- Rect[] rcst = new Rect[]
- {
- new Rect(70, 70, 500, 300)
- };
- DrawRectsInCanvas(rcst, "box");
- }));
- }
- /// <summary>
- /// 在画布上画出矩形框
- /// </summary>
- /// <param name="rcs"></param>
- /// <param name="rcName"></param>
- private void DrawRectsInCanvas(Rect[] rcs, string rcName)
- {
- if (_transformRGB == null)
- {
- return;
- }
- // 已有则删除
- RemoveRectsCanvas(rcName);
- List<System.Windows.Shapes.Rectangle> rcRenders = new List<System.Windows.Shapes.Rectangle> { };
- foreach (var rc in rcs)
- {
- System.Windows.Shapes.Rectangle rcRender = new System.Windows.Shapes.Rectangle();
- rcRender.StrokeThickness = 2;
- rcRender.Stroke = System.Windows.Media.Brushes.AliceBlue;
- var transPtLeftTop = _transformRGB.Transform(new System.Windows.Point(rc.Left, rc.Top));
- var transPtRightDown = _transformRGB.Transform(new System.Windows.Point(rc.Left + rc.Width, rc.Top + rc.Height));
- Canvas.SetLeft(rcRender, transPtLeftTop.X);
- Canvas.SetTop(rcRender, transPtLeftTop.Y);
- rcRender.Width = transPtRightDown.X - transPtLeftTop.X;
- rcRender.Height = transPtRightDown.Y - transPtLeftTop.Y;
- RGBImageCanvas.Children.Add(rcRender);
- //RGBImageCanvas.RegisterName(rcName, rcRender);
- rcRenders.Add(rcRender);
- }
- RGBImageCanvas.RegisterName(rcName, rcRenders);
- }
- /// <summary>
- /// 从画布上移除矩形框
- /// </summary>
- /// <param name="roiName"></param>
- private void RemoveRectsCanvas(string roiName)
- {
- // 框
- string textName = roiName;
- List<System.Windows.Shapes.Rectangle> rcsExist = RGBImageCanvas.FindName(textName) as List<System.Windows.Shapes.Rectangle>;
- if (rcsExist != null)
- {
- foreach (var rcExist in rcsExist)
- {
- RGBImageCanvas.Children.Remove(rcExist);
- }
- RGBImageCanvas.UnregisterName(textName);
- }
- }
- private Bitmap CropImage(Bitmap source, Rect cropRect)
- {
- // 创建一个与矩形大小相同的Bitmap
- Bitmap croppedImage = new Bitmap((int)cropRect.Width, (int)cropRect.Height);
- // 创建一个Graphics对象,并从原始图像中裁剪指定区域的图像
- using (Graphics g = Graphics.FromImage(croppedImage))
- {
- g.DrawImage(source, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height),
- (int)cropRect.Left, (int)cropRect.Top, croppedImage.Width, croppedImage.Height,
- GraphicsUnit.Pixel);
- }
- return croppedImage;
- }
- #endregion private funcs
- }
- }
|