123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665 |
- 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
- /// <summary>
- /// 图像
- /// </summary>
- public Bitmap bitmap;
- /// <summary>
- /// 对照模式的图片
- /// </summary>
- private Bitmap comBitmap;
- /// <summary>
- /// 1对照图像 2图谱图片
- /// </summary>
- private int type = 1;
- /// <summary>
- /// 图像集合
- /// </summary>
- private ImageCollection imageCollection;
- /// <summary>
- /// 当前图片
- /// </summary>
- private int currentPosition;
- /// <summary>
- /// 已经按下
- /// </summary>
- private bool MoveFlag = false;
- /// <summary>
- /// 当前x坐标
- /// </summary>
- private int xPos;
- /// <summary>
- /// 当前y坐标
- /// </summary>
- private int yPos;
- /// <summary>
- /// 1选择 2平移
- /// </summary>
- private int pointerType = 1;
- /// <summary>
- /// 初始化按钮事件
- /// </summary>
- private bool initButtonEvent = false;
- /// <summary>
- /// 对照窗体
- /// </summary>
- private AtlasComparisonDialog atlasComparisonDialog;
- /// <summary>
- /// 画笔颜色,用来绘制边框
- /// </summary>
- private Pen pen = new Pen(Color.Red);
- /// <summary>
- /// panel的选中状态
- /// </summary>
- public bool selectFlag = false;
- /// <summary>
- /// sizemode 借用picturebox的枚举
- /// </summary>
- private PictureBoxSizeMode pictureBoxSizeMode = PictureBoxSizeMode.Zoom;
- /// <summary>
- /// 缩放比例
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 计算缩放比例
- /// 需要区分是适合大小还是
- /// </summary>
- 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;
- }
- }
- }
- /// <summary>
- /// 计算绘制的图像合适大小
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 初始化按钮事件
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 初始化图片事件
- /// </summary>
- 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);
- }
- /// <summary>
- /// 按下事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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();
- }
- }
- /// <summary>
- /// 移动事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PictureBox_MouseMove(object sender, MouseEventArgs e)
- {
- if (MoveFlag)
- {
- this.pictureBox1.Left += e.X - xPos;
- this.pictureBox1.Top += e.Y - yPos;
- }
- }
- /// <summary>
- /// 抬起事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PictureBox_MouseUp(object sender, MouseEventArgs e)
- {
- MoveFlag = false;
- }
- /// <summary>
- /// 放大按钮,picture最大宽高65535
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ZoomInButton_click(object sender, EventArgs e)
- {
- if (!this.atlasComparisonDialog.RelatedPic)
- {
- ZoomIn();
- }
- else
- {
- this.atlasComparisonDialog.ZoomInButton_click();
- }
- }
- /// <summary>
- /// 放大
- /// </summary>
- public void ZoomIn()
- {
- this.scaleFactor = this.scaleFactor * 1.1f;
- this.pictureBox1.Refresh();
- }
- /// <summary>
- /// 缩小按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ZoomOutButton_click(object sender, EventArgs e)
- {
- if (!this.atlasComparisonDialog.RelatedPic)
- {
- ZoomOut();
- }
- else
- {
- this.atlasComparisonDialog.ZoomOutButton_click();
- }
- }
- /// <summary>
- /// 缩小
- /// </summary>
- public void ZoomOut()
- {
- this.scaleFactor = this.scaleFactor * 0.9f;
- this.pictureBox1.Refresh();
- }
- /// <summary>
- /// 选择
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PointerButton_click(object sender, EventArgs e)
- {
- this.pointerType = 1;
- this.pictureBox1.Cursor = Cursors.Default;
- }
- /// <summary>
- /// 平移
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MoveButton_click(object sender, EventArgs e)
- {
- this.pictureBox1.Cursor = Cursors.Hand;
- this.pointerType = 2;
- }
- /// <summary>
- /// 适合大小
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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();
-
- }
- /// <summary>
- /// 原始大小
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OriginSizeButton_click(object sender, EventArgs e)
- {
- this.pictureBoxSizeMode = PictureBoxSizeMode.Normal;
- /*if (this.bitmap != null)
- {
- CalcScaleFactor();
- }*/
- this.scaleFactor = 1.0f;
- this.Refresh();
- }
- /// <summary>
- /// 顺时针旋转90度
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Rotate90Button_click(object sender, EventArgs e)
- {
- if (!this.atlasComparisonDialog.RelatedPic)
- {
- Rotate90();
- }
- else
- {
- this.atlasComparisonDialog.Rotate90Button_click();
- }
- }
- /// <summary>
- /// 顺时针90度
- /// </summary>
- 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();
- }
- }
- /// <summary>
- /// 逆时针旋转90度
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Rotate_90Button_click(object sender, EventArgs e)
- {
- if (!this.atlasComparisonDialog.RelatedPic)
- {
- Rotate_90();
- }
- else
- {
- this.atlasComparisonDialog.Rotate_90Button_click();
- }
- }
- /// <summary>
- /// 逆时针旋转90度
- /// </summary>
- 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();
- }
- }
- /// <summary>
- /// 第一个
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// 上一个
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- }
- /// <summary>
- /// 下一个
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- }
- /// <summary>
- /// 最后一个
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// 初始化工具栏
- /// </summary>
- 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);
- }
- }
- }
|