using System; using System.Drawing; using System.Threading.Tasks; using System.Windows.Forms; using static System.Windows.Forms.ImageList; using OpenCvSharp; using PaintDotNet.Adjust; using System.Threading; namespace PaintDotNet.GeneralAnalysis.CustomInterface { internal class PictureUserControl : UserControl { #region 控件 private GroupBox groupBox1; private Panel panel1; public PanelEx pictureBox1; public PlayUserContrtol playUserContrtol; private ToolUserContrtol toolUserContrtol; #endregion /// /// 图像 /// public Bitmap bitmap; /// /// 对照模式的图片 /// private Bitmap comBitmap; /// /// 1对照图像 2图谱图片 /// private int type = 1; /// /// 图像集合 /// private ImageCollection imageCollection; /// /// 当前图片 /// private int currentPosition; /// /// 已经按下 /// private bool MoveFlag = false; /// /// 当前x坐标 /// private int xPos; /// /// 当前y坐标 /// private int yPos; /// /// 1选择 2平移 /// private int pointerType = 1; /// /// 初始化按钮事件 /// private bool initButtonEvent = false; /// /// 对照窗体 /// private AtlasComparisonDialog atlasComparisonDialog; /// /// 画笔颜色,用来绘制边框 /// private Pen pen = new Pen(Color.Red); /// /// panel的选中状态 /// public bool selectFlag = false; /// /// sizemode 借用picturebox的枚举 /// private PictureBoxSizeMode pictureBoxSizeMode = PictureBoxSizeMode.Zoom; /// /// 缩放比例 /// private float scaleFactor = 1f; public Bitmap Bitmap { set { this.bitmap = value; if(this.bitmap!=null) { CalcScaleFactor(); } this.pictureBox1.Refresh(); } get { return this.bitmap; } } public Bitmap ComBitmap { set { this.comBitmap = value; this.pictureBox1.Refresh(); } get { return this.comBitmap; } } public ImageCollection ImageCollection { set { this.imageCollection = value; } } public int CurrentPosition { set { this.currentPosition = value; SetCurrentImageAndPositionAndText(); } get { return this.currentPosition; } } private void SetCurrentImageAndPositionAndText() { if (this.imageCollection != null && this.imageCollection.Count > 0) { if (this.imageCollection.Count >= this.currentPosition) { this.Bitmap = this.atlasComparisonDialog.atlasModels.Find(a => a.id.Equals(this.imageCollection.Keys[this.currentPosition - 1])).bitmap; this.playUserContrtol.SetCollectionText(this.currentPosition, this.imageCollection.Count); } else { this.Bitmap = null; this.playUserContrtol.SetCollectionText(0, 0); } this.pictureBox1.Refresh(); } } public PictureUserControl(int type, AtlasComparisonDialog atlasComparisonDialog) { this.type = type; this.atlasComparisonDialog = atlasComparisonDialog; InitializeComponent(); InitializeLanguageText(); InitializeTools(); InitializePictureEvent(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (!this.initButtonEvent && this.bitmap != null) { this.initButtonEvent = true; _ = Task.Run(() => { Thread.Sleep(200); InitializeButtonEvent(); }); } if (this.bitmap != null) { if (this.pictureBoxSizeMode == PictureBoxSizeMode.Zoom) { e.Graphics.DrawImage(this.bitmap, CalcRectangle(this.bitmap)); } else { e.Graphics.DrawImage(this.bitmap, CalcRectangle(this.bitmap)); } } if (this.comBitmap != null) { e.Graphics.DrawImage(this.comBitmap, CalcRectangle(this.comBitmap)); } if (this.selectFlag) { e.Graphics.DrawRectangle(pen, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.X + e.ClipRectangle.Width - 1, e.ClipRectangle.Y + e.ClipRectangle.Height - 1); } } /// /// 计算缩放比例 /// 需要区分是适合大小还是 /// private void CalcScaleFactor() { if (this.pictureBox1.Width > this.pictureBox1.Height) { if (this.bitmap.Width > this.pictureBox1.Width || this.bitmap.Height > this.pictureBox1.Height) { float a = this.bitmap.Height * 1.0f / this.pictureBox1.Height; float b = this.bitmap.Width * 1.0f / this.pictureBox1.Width; if (a < b) { this.scaleFactor = this.pictureBox1.Width * 1.0f / this.bitmap.Width; } else { this.scaleFactor = this.pictureBox1.Height * 1.0f / this.bitmap.Height; } } else { this.scaleFactor = 1.0f; } } else { if (this.bitmap.Width > this.pictureBox1.Width || this.bitmap.Height > this.pictureBox1.Height) { float a = this.bitmap.Height * 1.0f / this.pictureBox1.Height; float b = this.bitmap.Width * 1.0f / this.pictureBox1.Width; if (a < b) { this.scaleFactor = this.pictureBox1.Width * 1.0f / this.bitmap.Width; } else { this.scaleFactor = this.pictureBox1.Height * 1.0f / this.bitmap.Height; } } else { this.scaleFactor = 1.0f; } } } /// /// 计算绘制的图像合适大小 /// /// private RectangleF CalcRectangle(Bitmap bmp) { RectangleF rect = new RectangleF(); rect.X = (this.pictureBox1.Width - bmp.Width * this.scaleFactor) / 2; rect.Y = (this.pictureBox1.Height - bmp.Height * this.scaleFactor) / 2; rect.Width = bmp.Width * this.scaleFactor; rect.Height = bmp.Height * this.scaleFactor; return rect; } /// /// 初始化按钮事件 /// private void InitializeButtonEvent() { if (this.type == 1) { this.toolUserContrtol.button1.Click += new EventHandler(this.ZoomInButton_click); this.toolUserContrtol.button2.Click += new EventHandler(this.ZoomOutButton_click); this.toolUserContrtol.button3.Click += new EventHandler(this.PointerButton_click); this.toolUserContrtol.button4.Click += new EventHandler(this.MoveButton_click); this.toolUserContrtol.button5.Click += new EventHandler(this.FitSizeButton_click); this.toolUserContrtol.button6.Click += new EventHandler(this.OriginSizeButton_click); this.toolUserContrtol.button7.Click += new EventHandler(this.Rotate90Button_click); this.toolUserContrtol.button8.Click += new EventHandler(this.Rotate_90Button_click); } else if(this.type == 2) { this.playUserContrtol.button1.Click += new EventHandler(this.FirstButton_click); this.playUserContrtol.button3.Click += new EventHandler(this.PrevButton_click); this.playUserContrtol.button7.Click += new EventHandler(this.NextButton_click); this.playUserContrtol.button8.Click += new EventHandler(this.FinalButton_click); } } /// /// 初始化图片事件 /// private void InitializePictureEvent() { this.pictureBox1.MouseDown += new MouseEventHandler(this.PictureBox_MouseDown); this.pictureBox1.MouseMove += new MouseEventHandler(this.PictureBox_MouseMove); this.pictureBox1.MouseUp += new MouseEventHandler(this.PictureBox_MouseUp); } /// /// 按下事件 /// /// /// private void PictureBox_MouseDown(object sender, MouseEventArgs e) { if (this.pointerType == 2) { MoveFlag = true; xPos = e.X; yPos = e.Y; } if(this.type == 2 && this.bitmap!=null) { this.atlasComparisonDialog.SetSelectFlag(); this.selectFlag = true; this.pictureBox1.Refresh(); } } /// /// 移动事件 /// /// /// private void PictureBox_MouseMove(object sender, MouseEventArgs e) { if (MoveFlag) { this.pictureBox1.Left += e.X - xPos; this.pictureBox1.Top += e.Y - yPos; } } /// /// 抬起事件 /// /// /// private void PictureBox_MouseUp(object sender, MouseEventArgs e) { MoveFlag = false; } /// /// 放大按钮,picture最大宽高65535 /// /// /// private void ZoomInButton_click(object sender, EventArgs e) { if (!this.atlasComparisonDialog.RelatedPic) { ZoomIn(); } else { this.atlasComparisonDialog.ZoomInButton_click(); } } /// /// 放大 /// public void ZoomIn() { this.scaleFactor = this.scaleFactor * 1.1f; this.pictureBox1.Refresh(); } /// /// 缩小按钮 /// /// /// private void ZoomOutButton_click(object sender, EventArgs e) { if (!this.atlasComparisonDialog.RelatedPic) { ZoomOut(); } else { this.atlasComparisonDialog.ZoomOutButton_click(); } } /// /// 缩小 /// public void ZoomOut() { this.scaleFactor = this.scaleFactor * 0.9f; this.pictureBox1.Refresh(); } /// /// 选择 /// /// /// private void PointerButton_click(object sender, EventArgs e) { this.pointerType = 1; this.pictureBox1.Cursor = Cursors.Default; } /// /// 平移 /// /// /// private void MoveButton_click(object sender, EventArgs e) { this.pictureBox1.Cursor = Cursors.Hand; this.pointerType = 2; } /// /// 适合大小 /// /// /// private void FitSizeButton_click(object sender, EventArgs e) { this.PointerButton_click(null, null); this.pictureBoxSizeMode = PictureBoxSizeMode.Zoom; this.pictureBox1.Width = this.groupBox1.Width - 6; this.pictureBox1.Height = this.groupBox1.Height - 41 - 9; this.pictureBox1.Left = 6; this.pictureBox1.Top = 21; if (this.bitmap != null) { CalcScaleFactor(); } this.pictureBox1.Refresh(); } /// /// 原始大小 /// /// /// private void OriginSizeButton_click(object sender, EventArgs e) { this.pictureBoxSizeMode = PictureBoxSizeMode.Normal; /*if (this.bitmap != null) { CalcScaleFactor(); }*/ this.scaleFactor = 1.0f; this.Refresh(); } /// /// 顺时针旋转90度 /// /// /// private void Rotate90Button_click(object sender, EventArgs e) { if (!this.atlasComparisonDialog.RelatedPic) { Rotate90(); } else { this.atlasComparisonDialog.Rotate90Button_click(); } } /// /// 顺时针90度 /// public void Rotate90() { if (this.bitmap != null) { Mat src = PaintDotNet.Camera.Tools.ToMat(this.bitmap); Mat dst = new Mat(); GeometryIntent.ImageRotate(src, dst, FlipMode.Y); this.Bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(dst); this.pictureBox1.Refresh(); } } /// /// 逆时针旋转90度 /// /// /// private void Rotate_90Button_click(object sender, EventArgs e) { if (!this.atlasComparisonDialog.RelatedPic) { Rotate_90(); } else { this.atlasComparisonDialog.Rotate_90Button_click(); } } /// /// 逆时针旋转90度 /// public void Rotate_90() { if (this.bitmap != null) { Mat src = PaintDotNet.Camera.Tools.ToMat(this.bitmap); Mat dst = new Mat(); GeometryIntent.ImageRotate(src, dst, FlipMode.X); this.Bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(dst); this.pictureBox1.Refresh(); } } /// /// 第一个 /// /// /// private void FirstButton_click(object sender, EventArgs e) { if (imageCollection!=null && imageCollection.Count>0) { this.currentPosition = 1; this.Bitmap = this.atlasComparisonDialog.atlasModels.Find(a => a.id.Equals(this.imageCollection.Keys[0])).bitmap; this.pictureBox1.Refresh(); this.playUserContrtol.SetCollectionText(1, this.imageCollection.Count); } } /// /// 上一个 /// /// /// private void PrevButton_click(object sender, EventArgs e) { if (imageCollection != null && imageCollection.Count > 0) { if(this.currentPosition>1) { this.currentPosition -= 1; this.Bitmap = this.atlasComparisonDialog.atlasModels.Find(a => a.id.Equals(this.imageCollection.Keys[this.currentPosition - 1])).bitmap; this.pictureBox1.Refresh(); this.playUserContrtol.SetCollectionText(this.currentPosition, this.imageCollection.Count); } } } /// /// 下一个 /// /// /// private void NextButton_click(object sender, EventArgs e) { if (imageCollection != null && imageCollection.Count > 0) { if (this.currentPosition < imageCollection.Count) { this.currentPosition += 1; this.Bitmap = this.atlasComparisonDialog.atlasModels.Find(a => a.id.Equals(this.imageCollection.Keys[this.currentPosition - 1])).bitmap; this.pictureBox1.Refresh(); this.playUserContrtol.SetCollectionText(this.currentPosition, this.imageCollection.Count); } } } /// /// 最后一个 /// /// /// private void FinalButton_click(object sender, EventArgs e) { if (imageCollection != null && imageCollection.Count > 0) { this.currentPosition = imageCollection.Count; this.Bitmap = this.atlasComparisonDialog.atlasModels.Find(a => a.id.Equals(this.imageCollection.Keys[this.currentPosition - 1])).bitmap; this.pictureBox1.Refresh(); this.playUserContrtol.SetCollectionText(this.currentPosition, this.imageCollection.Count); } } /// /// 初始化工具栏 /// private void InitializeTools() { if (this.type==1) { this.groupBox1.Text = PdnResources.GetString("Menu.Generalanalysis.Atlascomparison.Controlimage.text"); this.toolUserContrtol = new ToolUserContrtol(); this.panel1.Controls.Add(this.toolUserContrtol); } else { this.groupBox1.Text = PdnResources.GetString("Menu.Generalanalysis.Atlascomparison.Atlasimage.text"); this.playUserContrtol = new PlayUserContrtol(); this.panel1.Controls.Add(this.playUserContrtol); } } private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.Mapimage.Text"); } private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.panel1 = new System.Windows.Forms.Panel(); this.pictureBox1 = new PanelEx(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox1.Controls.Add(this.panel1); this.groupBox1.Controls.Add(this.pictureBox1); this.groupBox1.Location = new System.Drawing.Point(4, 4); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(300, 319); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "图谱图片"; // // panel1 // this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.panel1.Location = new System.Drawing.Point(7, 272); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(287, 41); this.panel1.TabIndex = 1; // // pictureBox1 // this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.pictureBox1.Location = new System.Drawing.Point(6, 21); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(288, 245); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); // // PictureUserControl // this.Controls.Add(this.groupBox1); this.Name = "PictureUserControl"; this.Size = new System.Drawing.Size(307, 326); this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } } }