123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047 |
- using PaintDotNet.Base.CommTool;
- using PaintDotNet.CustomControl;
- using PaintDotNet.ImageCollect.CameraEDOF;
- using PaintDotNet.Processing.DepthExtensionComponent;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace PaintDotNet.Processing
- {
- /// <summary>
- /// 景深扩展
- /// </summary>
- internal class ImageDepthExtensionDialog : PdnBaseForm
- {
- private Button button3;
- private GroupBox groupBox2;
- /// <summary>
- /// 辅助判断目前最新一个自定义控件的tag值
- /// </summary>
- private int toolsNum = 1;
- /// <summary>
- /// 图片选择窗口
- /// </summary>
- private InterImagePickerDialog imagePickerDialog;
- /// <summary>
- /// 辅助为合并的图片命名
- /// </summary>
- private int nameNum = 1;
- /// <summary>
- /// 是否重置picturebox中的矩形尺寸
- /// </summary>
- private bool isFirst = true;
- /// <summary>
- /// 是否为拖动picturebox中的矩形框
- /// </summary>
- private bool isMoved = false;
- /// <summary>
- /// 是否为修改picturebox中的矩形框大小
- /// </summary>
- private bool isResized = false;
- /// <summary>
- /// 辅助计算偏移量
- /// </summary>
- private Point lastPoint = new Point(0, 0);
- /// <summary>
- /// picturebox中图片对应矩形
- /// </summary>
- private Rectangle imageRect;
- /// <summary>
- /// picturebox中选择框对应矩形
- /// </summary>
- private Rectangle zoomRect;
- /// <summary>
- /// 合成后的图片
- /// </summary>
- private Bitmap depthBitmap;
- private Label label1;
- private Button button7;
- private TextBox textBox1;
- private PictureBox pictureBox1;
- private Panel panel1;
- private GroupBox groupBox3;
- private AppWorkspace appWorkspace;
- public ImageDepthExtensionDialog(AppWorkspace appWorkspace)
- {
- this.appWorkspace = appWorkspace;
- InitializeComponent();
- InitializeLanguageText();
- InitializeOtherInfo();
- AddDepthExtensionPicTools();
- }
- private void InitializeLanguageText()
- {
- //this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
- //this.button4.Text = PdnResources.GetString("Menu.return.text");
- this.button3.Text = PdnResources.GetString("Menu.ronghe.Text");
- //this.button2.Text = PdnResources.GetString("Menu.Secondstep.text");
- //this.button1.Text = PdnResources.GetString("Menu.firststep.text");
- this.groupBox2.Text = PdnResources.GetString("Menu.Selectimagefrom.text");
- //this.button6.Text = PdnResources.GetString("Menu.Currentsoftware.text");
- //this.button5.Text = PdnResources.GetString("Menu.Theharddiskimage.Text");
- this.groupBox3.Text = PdnResources.GetString("Menu.Preview.text");
- //this.groupBox4.Text = PdnResources.GetString("Menu.finish.text");
- this.button7.Text = PdnResources.GetString("Menu.File.Save.Text");
- this.label1.Text = PdnResources.GetString("Menu.Imagename.text") + ":";
- this.Text = PdnResources.GetString("Menu.Image.DepthExtension.Text");
- }
- /// <summary>
- /// 初始化基础控件
- /// </summary>
- private void InitializeComponent()
- {
- this.button3 = new System.Windows.Forms.Button();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.textBox1 = new System.Windows.Forms.TextBox();
- this.label1 = new System.Windows.Forms.Label();
- this.button7 = new System.Windows.Forms.Button();
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
- this.panel1 = new System.Windows.Forms.Panel();
- this.groupBox3 = new System.Windows.Forms.GroupBox();
- this.groupBox2.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
- this.groupBox3.SuspendLayout();
- this.SuspendLayout();
- //
- // button3
- //
- this.button3.Location = new System.Drawing.Point(442, 20);
- this.button3.Name = "button3";
- this.button3.Size = new System.Drawing.Size(75, 23);
- this.button3.TabIndex = 2;
- this.button3.Text = "图像融合";
- this.button3.UseVisualStyleBackColor = true;
- this.button3.Click += new System.EventHandler(this.button3_Click);
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.textBox1);
- this.groupBox2.Controls.Add(this.button3);
- this.groupBox2.Controls.Add(this.label1);
- this.groupBox2.Controls.Add(this.button7);
- this.groupBox2.Location = new System.Drawing.Point(15, 9);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(623, 60);
- this.groupBox2.TabIndex = 1;
- this.groupBox2.TabStop = false;
- //
- // textBox1
- //
- this.textBox1.Location = new System.Drawing.Point(72, 22);
- this.textBox1.Name = "textBox1";
- this.textBox1.ReadOnly = true;
- this.textBox1.Size = new System.Drawing.Size(167, 21);
- this.textBox1.TabIndex = 2;
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(7, 25);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(65, 12);
- this.label1.TabIndex = 0;
- this.label1.Text = "图像名称:";
- //
- // button7
- //
- this.button7.Enabled = false;
- this.button7.Location = new System.Drawing.Point(537, 20);
- this.button7.Name = "button7";
- this.button7.Size = new System.Drawing.Size(75, 23);
- this.button7.TabIndex = 1;
- this.button7.Text = "保存";
- this.button7.UseVisualStyleBackColor = true;
- this.button7.Click += new System.EventHandler(this.button7_Click);
- //
- // pictureBox1
- //
- this.pictureBox1.Location = new System.Drawing.Point(6, 14);
- this.pictureBox1.Name = "pictureBox1";
- this.pictureBox1.Size = new System.Drawing.Size(611, 491);
- this.pictureBox1.TabIndex = 0;
- this.pictureBox1.TabStop = false;
- this.pictureBox1.Visible = false;
- //
- // panel1
- //
- this.panel1.AutoScroll = true;
- this.panel1.BackColor = System.Drawing.Color.Transparent;
- this.panel1.Location = new System.Drawing.Point(6, 14);
- this.panel1.Name = "panel1";
- this.panel1.Size = new System.Drawing.Size(611, 491);
- this.panel1.TabIndex = 0;
- //
- // groupBox3
- //
- this.groupBox3.Controls.Add(this.panel1);
- this.groupBox3.Controls.Add(this.pictureBox1);
- this.groupBox3.Location = new System.Drawing.Point(15, 85);
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.Size = new System.Drawing.Size(623, 511);
- this.groupBox3.TabIndex = 2;
- this.groupBox3.TabStop = false;
- this.groupBox3.Text = "预览";
- //
- // ImageDepthExtensionDialog
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
- this.ClientSize = new System.Drawing.Size(653, 612);
- this.Controls.Add(this.groupBox3);
- this.Controls.Add(this.groupBox2);
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
- this.MaximizeBox = false;
- this.MinimizeBox = false;
- this.Name = "ImageDepthExtensionDialog";
- this.Text = "景深扩展";
- this.Controls.SetChildIndex(this.groupBox2, 0);
- this.Controls.SetChildIndex(this.groupBox3, 0);
- this.groupBox2.ResumeLayout(false);
- this.groupBox2.PerformLayout();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
- this.groupBox3.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- /// <summary>
- /// 初始化其他
- /// </summary>
- private void InitializeOtherInfo()
- {
- //第一次进入页面的"第一步"按钮样式
- //this.button1.BackColor = Color.Gray;
- //this.button1.ForeColor = Color.White;
- //this.button1.FlatStyle = FlatStyle.Flat;
- //this.button1.FlatAppearance.BorderSize = 0;
- this.textBox1.Text = GetDepthImageName();
-
- this.pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
- this.pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
- this.pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
- }
- /// <summary>
- /// 添加一个图片的自定义控件
- /// </summary>
- private void AddDepthExtensionPicTools()
- {
- //判断panel上存在几个图片自定义控件
- if (this.panel1.Controls.Count > 0)
- {
- int toolControls = 0;
- foreach (Control C in this.panel1.Controls)
- {
- if (C is DepthExtensionPicTool)
- toolControls++;
- }
- if (toolControls > 0)
- {
- int rowNum = toolControls / 5;//判断行数
- int coloumNum = toolControls % 5;//判断列数
- Point panelScroll = this.panel1.AutoScrollPosition;
- DepthExtensionPicTool depthExtensionPicTool = new DepthExtensionPicTool();
- depthExtensionPicTool.Tag = toolsNum;
- depthExtensionPicTool.Size = new Size(this.panel1.Width / 5 - 6, this.panel1.Width / 5 - 6);
- depthExtensionPicTool.Location = new Point(0 + ((this.panel1.Width / 5 - 4) * coloumNum), 4 + ((this.panel1.Width / 5) * rowNum) + panelScroll.Y);
- depthExtensionPicTool.panel1.Cursor = Cursors.Hand;
- depthExtensionPicTool.panel1.Paint += new PaintEventHandler(panelDel_Paint);
- depthExtensionPicTool.panel1.Click += new EventHandler(panelDel_Click);
- depthExtensionPicTool.panel2.Cursor = Cursors.Hand;
- depthExtensionPicTool.panel2.Paint += new PaintEventHandler(panelAdd_Paint);
- depthExtensionPicTool.panel2.Click += new EventHandler(panelAdd_Click);
- this.panel1.Controls.Add(depthExtensionPicTool);
- }
- else
- {
- //第一个自定义控件,理论上无法走进这个判断,可删除
- DepthExtensionPicTool depthExtensionPicTool = new DepthExtensionPicTool();
- depthExtensionPicTool.Tag = toolsNum;
- depthExtensionPicTool.Size = new Size(this.panel1.Width / 5 - 6, this.panel1.Width / 5 - 6);
- depthExtensionPicTool.Location = new Point(0, 4);
- depthExtensionPicTool.panel1.Cursor = Cursors.Hand;
- depthExtensionPicTool.panel1.Paint += new PaintEventHandler(panelDel_Paint);
- depthExtensionPicTool.panel1.Click += new EventHandler(panelDel_Click);
- depthExtensionPicTool.panel2.Cursor = Cursors.Hand;
- depthExtensionPicTool.panel2.Paint += new PaintEventHandler(panelAdd_Paint);
- depthExtensionPicTool.panel2.Click += new EventHandler(panelAdd_Click);
- this.panel1.Controls.Add(depthExtensionPicTool);
- }
- }
- else
- {
- //实例化第一个自定义图片控件,坐标固定
- DepthExtensionPicTool depthExtensionPicTool = new DepthExtensionPicTool();
- depthExtensionPicTool.Tag = toolsNum;
- depthExtensionPicTool.Size = new Size(this.panel1.Width / 5 - 6, this.panel1.Width / 5 - 6);
- depthExtensionPicTool.Location = new Point(0, 4);
- depthExtensionPicTool.panel1.Cursor = Cursors.Hand;
- depthExtensionPicTool.panel1.Paint += new PaintEventHandler(panelDel_Paint);
- depthExtensionPicTool.panel1.Click += new EventHandler(panelDel_Click);
- depthExtensionPicTool.panel2.Cursor = Cursors.Hand;
- depthExtensionPicTool.panel2.Paint += new PaintEventHandler(panelAdd_Paint);
- depthExtensionPicTool.panel2.Click += new EventHandler(panelAdd_Click);
- this.panel1.Controls.Add(depthExtensionPicTool);
- }
- picturePath = new PicturePath();
- picturePath.Size = new Size(this.panel1.Width / 5 - 6, this.panel1.Width / 5 - 6);
- picturePath.button5.Click += new EventHandler(this.button5_Click);
- picturePath.button6.Click += new EventHandler(this.button6_Click);
- toolsNum++;
- }
- PicturePath picturePath;
- /// <summary>
- /// 绘制删除图标
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void panelDel_Paint(object sender, PaintEventArgs e)
- {
- Image deleteImg = PdnResources.GetImageResource("Images.ImageStrip.CloseButton.Hot.png").Reference;//从项目中随便找了一个红色的删除图片
- if (deleteImg != null)
- {
- Graphics g = e.Graphics;
- g.DrawImage(deleteImg, 0, 0);
- }
- }
- /// <summary>
- /// 绘制添加功能的十字线
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void panelAdd_Paint(object sender, PaintEventArgs e)
- {
- Panel panel = (Panel)sender;
- Size panelSize = panel.Size;
- Graphics g = e.Graphics;
- Pen linePen = new Pen(Color.Black);
- linePen.Width = 3;
- g.DrawLine(linePen, new Point(0, panelSize.Height / 2), new Point(panelSize.Width, panelSize.Height / 2));
- g.DrawLine(linePen, new Point(panelSize.Width / 2, 0), new Point(panelSize.Width / 2, panelSize.Height));
- linePen.Dispose();
- }
- /// <summary>
- /// 点击添加标志
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void panelAdd_Click(object sender, EventArgs e)
- {
- this.panel1.Controls.Add(picturePath);
- this.panel1.Controls[this.panel1.Controls.Count - 1].Location = this.panel1.Controls[this.panel1.Controls.Count - 2].Location;
- this.panel1.Controls[this.panel1.Controls.Count - 2].Visible = false;
- //this.panel1.Controls.Remove(picturePath);
- //List<Bitmap> listBitmap = ChooseImageFromHardDisk();
- //if (listBitmap != null && listBitmap.Count > 0)
- //{
- // for (int i = 0; i < listBitmap.Count; i++)
- // {
- // foreach (Control C in this.panel1.Controls)
- // {
- // if (C is DepthExtensionPicTool && Convert.ToInt32(C.Tag) == (toolsNum - 1))//取得当前最新的一个图片自定义控件
- // {
- // DepthExtensionPicTool latestDepthTool = (DepthExtensionPicTool)C;
- // if (listBitmap[i].Width > latestDepthTool.pictureBox1.Size.Width || listBitmap[i].Height > latestDepthTool.pictureBox1.Size.Height)
- // {
- // latestDepthTool.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
- // }
- // else
- // {
- // latestDepthTool.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
- // }
- // latestDepthTool.pictureBox1.Image = listBitmap[i];
- // latestDepthTool.panel2.Visible = false;
- // latestDepthTool.panel1.Visible = true;
- // AddDepthExtensionPicTools();
- // }
- // }
- // }
- //}
- //Bitmap bitmap = ChooseImageFromHardDisk();
- //if (bitmap != null)
- //{
- // Panel panel = (Panel)sender;
- // DepthExtensionPicTool parentTool = (DepthExtensionPicTool)panel.Parent;
- // if (bitmap.Width > parentTool.pictureBox1.Size.Width || bitmap.Height > parentTool.pictureBox1.Size.Height)
- // {
- // parentTool.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
- // }
- // else
- // {
- // parentTool.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
- // }
- // parentTool.pictureBox1.Image = bitmap;
- // parentTool.panel2.Visible = false;
- // parentTool.panel1.Visible = true;
- // AddDepthExtensionPicTools();
- //}
- }
- /// <summary>
- /// 点击删除图标
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void panelDel_Click(object sender, EventArgs e)
- {
- Panel panel = (Panel)sender;
- DepthExtensionPicTool parentTool = (DepthExtensionPicTool)panel.Parent;
- //判断当前存在几个自定义图片控件
- int toolControls = 0;
- foreach (Control C in this.panel1.Controls)
- {
- if (C is DepthExtensionPicTool)
- toolControls++;
- }
- if (toolControls == 1)
- {
- //只有一个该控件时不删除当前操作控件
- if (parentTool.pictureBox1.Image != null)
- {
- parentTool.pictureBox1.Image = null;
- parentTool.panel2.Visible = true;
- parentTool.panel1.Visible = false;
- }
- }
- else
- {
- this.panel1.Controls.Remove(parentTool);
- ResizeAllDepthTools();
- }
- }
- /// <summary>
- /// 重置所有图片控件的位置
- /// </summary>
- private void ResizeAllDepthTools()
- {
- if (this.panel1.Controls.Count > 1)
- {
- this.panel1.Controls[this.panel1.Controls.Count - 2].Visible = true;
- foreach (Control c in this.panel1.Controls)
- {
- if (c is PicturePath)
- {
- this.panel1.Controls.Remove(c);
- }
- }
- }
- List<DepthExtensionPicTool> listTools = new List<DepthExtensionPicTool>();
- foreach (Control C in this.panel1.Controls)
- {
- if (C is DepthExtensionPicTool)
- listTools.Add((DepthExtensionPicTool)C);
- }
- Point panelScroll = this.panel1.AutoScrollPosition;//panel的滚轮位置
- for (int i = 0; i < listTools.Count; i++)
- {
- int rowNum = i / 5;//判断行数
- int coloumNum = i % 5;//判断列数
-
- listTools[i].Location = new Point(0 + ((this.panel1.Width / 5 - 4) * coloumNum), 4 + ((this.panel1.Width / 5) * rowNum) + panelScroll.Y);
- }
- }
- /// <summary>
- /// 图库选择按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button6_Click(object sender, EventArgs e)
- {
- this.panel1.Controls[this.panel1.Controls.Count - 2].Visible = true;
- //this.panel1.Controls.Remove(this.panel1.Controls.Remove(this.panel1.Controls[this.panel1.Controls.Count - 1]));
- foreach (Control c in this.panel1.Controls)
- {
- if (c is PicturePath)
- {
- this.panel1.Controls.Remove(c);
- }
- }
- if (imagePickerDialog == null)
- {
- imagePickerDialog = new InterImagePickerDialog(this.appWorkspace);
- imagePickerDialog.StartPosition = FormStartPosition.Manual;
- imagePickerDialog.Left = ((Button)sender).Left + 12 + this.Left;
- imagePickerDialog.Top = ((Button)sender).Bottom + 36 + this.Top;
- imagePickerDialog.ValueChanged += new System.EventHandler(this.inputMat_ValueChanged);
- imagePickerDialog.Show();
- }
- else
- {
- if (!imagePickerDialog.IsDisposed)
- {
- imagePickerDialog.WindowState = FormWindowState.Normal;
- }
- else
- {
- imagePickerDialog = new InterImagePickerDialog(this.appWorkspace);
- imagePickerDialog.StartPosition = FormStartPosition.Manual;
- imagePickerDialog.Left = ((Button)sender).Left + 12 + this.Left;
- imagePickerDialog.Top = ((Button)sender).Bottom + 36 + this.Top;
- imagePickerDialog.ValueChanged += new System.EventHandler(this.inputMat_ValueChanged);
- imagePickerDialog.Show();
- }
- }
-
- }
- /// <summary>
- /// 从图库选择待对照图片
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void inputMat_ValueChanged(object sender, EventArgs e)
- {
- if (sender is DocumentWorkspace)
- {
- Bitmap bitmap = ((DocumentWorkspace)sender).CompositionSurface.CreateAliasedBitmap();
- foreach(Control C in this.panel1.Controls)
- {
- if(C.Tag != null && C is DepthExtensionPicTool)
- {
- int nowNum = Convert.ToInt32(C.Tag);
- if (nowNum == (toolsNum - 1))//取得当前最新的一个图片自定义控件
- {
- DepthExtensionPicTool depthExtensionPicTool = (DepthExtensionPicTool)C;
- if (bitmap.Width > depthExtensionPicTool.pictureBox1.Size.Width || bitmap.Height > depthExtensionPicTool.pictureBox1.Size.Height)
- {
- depthExtensionPicTool.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
- }
- else
- {
- depthExtensionPicTool.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
- }
- depthExtensionPicTool.pictureBox1.Image = bitmap;
- depthExtensionPicTool.panel2.Visible = false;
- depthExtensionPicTool.panel1.Visible = true;
- AddDepthExtensionPicTools();
- return;
- }
- }
- }
- }
- }
- /// <summary>
- /// 从硬盘选择单张图片
- /// </summary>
- /// <returns></returns>
- private List<Bitmap> ChooseImageFromHardDisk()
- {
- List<Bitmap> bitmaps = new List<Bitmap>();
- var openFileDialog = new OpenFileDialog { Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf" };
- openFileDialog.Multiselect = true;
- string[] picArr;
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- picArr = openFileDialog.FileNames;
- foreach (string item in picArr)
- {
- Image image = Image.FromFile(item);
- Bitmap newBit = new Bitmap(image);
- bitmaps.Add(newBit);
- }
- return bitmaps;
- }
- else
- {
- return null;
- }
- //var openFileDialog = new OpenFileDialog { Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf" };
- //if (openFileDialog.ShowDialog() == DialogResult.OK)
- //{
- // Image image = Image.FromFile(openFileDialog.FileName);
- // Bitmap newBit = new Bitmap(image);
- // return newBit;
- //}
- //else
- //{
- // return null;
- //}
- }
- /// <summary>
- /// 从硬盘选择多张图片
- /// </summary>
- /// <returns></returns>
- private List<Bitmap> ChooseImagesFromHardDisk()
- {
- var openFileDialog = new OpenFileDialog { Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf" };
- openFileDialog.Multiselect = true;
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- string[] imageNames = openFileDialog.FileNames;
- List<Bitmap> listBitmap = new List<Bitmap>();
- foreach(string imgName in imageNames)
- {
- Image image = Image.FromFile(imgName);
- listBitmap.Add(new Bitmap(image));
- }
- return listBitmap;
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 硬盘选择图片按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button5_Click(object sender, EventArgs e)
- {
- List<Bitmap> listBitmap = ChooseImagesFromHardDisk();
- this.panel1.Controls[this.panel1.Controls.Count - 2].Visible = true;
- foreach (Control c in this.panel1.Controls)
- {
- if (c is PicturePath)
- {
- this.panel1.Controls.Remove(c);
- }
- }
- if (listBitmap != null && listBitmap.Count > 0)
- {
- for (int i = 0; i < listBitmap.Count; i++)
- {
- foreach (Control C in this.panel1.Controls)
- {
- if (C is DepthExtensionPicTool && Convert.ToInt32(C.Tag) == (toolsNum - 1))//取得当前最新的一个图片自定义控件
- {
- DepthExtensionPicTool latestDepthTool = (DepthExtensionPicTool)C;
- if (listBitmap[i].Width > latestDepthTool.pictureBox1.Size.Width || listBitmap[i].Height > latestDepthTool.pictureBox1.Size.Height)
- {
- latestDepthTool.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
- }
- else
- {
- latestDepthTool.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
- }
- latestDepthTool.pictureBox1.Image = listBitmap[i];
- latestDepthTool.panel2.Visible = false;
- latestDepthTool.panel1.Visible = true;
- AddDepthExtensionPicTools();
- }
- }
- }
- }
-
- }
- /// <summary>
- /// 图像融合
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button3_Click(object sender, EventArgs e)
- {
- if (PdnResources.GetString("Menu.ronghe.Text").Equals(this.button3.Text))
- {
- if (this.pictureBox1.Visible == true /*|| this.groupBox4.Visible == true*/)
- return;
-
- List<Bitmap> listBitmaps = new List<Bitmap>();
- foreach (Control C in this.panel1.Controls)
- {
- if (C is DepthExtensionPicTool)
- {
- DepthExtensionPicTool depthExtensionPicTool = (DepthExtensionPicTool)C;
- if (depthExtensionPicTool.pictureBox1.Image != null)
- {
- listBitmaps.Add(new Bitmap(depthExtensionPicTool.pictureBox1.Image));
- }
- }
- }
- //没有上传或选择图片不进行下一步
- if (listBitmaps == null || listBitmaps.Count == 0)
- {
- MessageBox.Show(PdnResources.GetString("Menu.Pleaseopenthepicture.Text"));
- return;
- }
-
- //this.panel1.Visible = false;
- //this.groupBox2.Visible = false;
- //this.pictureBox1.Visible = true;
- //this.groupBox4.Visible = true;
- //this.button1.BackColor = Color.FromArgb(255, 240, 240, 240);
- //this.button1.ForeColor = Color.FromArgb(255, 0, 0, 0);
- //this.button1.FlatStyle = FlatStyle.Standard;
- //this.button1.FlatAppearance.BorderSize = 1;
- //this.button2.BackColor = Color.Gray;
- //this.button2.ForeColor = Color.White;
- //this.button2.FlatStyle = FlatStyle.Flat;
- //this.button2.FlatAppearance.BorderSize = 0;
- String errorMsg = "";
- try
- {
- depthBitmap = GetDepthExtensionImage(listBitmaps, ref errorMsg);
- }
- catch (Exception)
- {
- MessageBox.Show(PdnResources.GetString("Menu.Depthoffieldexpansionfailed.Text"));
- depthBitmap = null;
- }
- finally
- {
- if (errorMsg != null)
- depthBitmap = null;
- }
- if (depthBitmap != null)
- {
- this.panel1.Visible = false;
- //this.groupBox2.Visible = false;
- this.pictureBox1.Visible = true;
- //this.groupBox4.Visible = true;
- //this.button1.BackColor = Color.FromArgb(255, 240, 240, 240);
- //this.button1.ForeColor = Color.FromArgb(255, 0, 0, 0);
- //this.button1.FlatStyle = FlatStyle.Standard;
- //this.button1.FlatAppearance.BorderSize = 1;
- //this.button2.BackColor = Color.Gray;
- //this.button2.ForeColor = Color.White;
- //this.button2.FlatStyle = FlatStyle.Flat;
- //this.button2.FlatAppearance.BorderSize = 0;
- if (depthBitmap.Width > this.pictureBox1.Size.Width || depthBitmap.Height > this.pictureBox1.Size.Height)
- {
- this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
- }
- else
- {
- this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
- }
- this.pictureBox1.Image = depthBitmap;
- lastPoint = new Point(0, 0);
- isFirst = true;
- }
- if (depthBitmap != null)
- {
- this.button3.Text = PdnResources.GetString("Menu.Previous.text");
- this.button7.Enabled = true;
- }
- }
- else if (PdnResources.GetString("Menu.Previous.text").Equals(this.button3.Text))
- {
- if (this.panel1.Visible == true /*|| this.groupBox2.Visible == true*/)
- return;
- this.button3.Text = PdnResources.GetString("Menu.ronghe.Text");
- this.button7.Enabled = false;
- this.pictureBox1.Visible = false;
- //this.groupBox4.Visible = false;
- this.panel1.Visible = true;
- //this.groupBox2.Visible = true;
- }
-
- //lastPoint = new Point(0, 0);
- //isFirst = true;
- }
- /// <summary>
- /// 返回按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button4_Click(object sender, EventArgs e)
- {
- if (this.panel1.Visible == true || this.groupBox2.Visible == true)
- return;
- this.pictureBox1.Visible = false;
- //this.groupBox4.Visible = false;
- this.panel1.Visible = true;
- this.groupBox2.Visible = true;
- //this.button2.BackColor = Color.FromArgb(255, 240, 240, 240);
- //this.button2.ForeColor = Color.FromArgb(255, 0, 0, 0);
- //this.button2.FlatStyle = FlatStyle.Standard;
- //this.button2.FlatAppearance.BorderSize = 1;
- //this.button1.BackColor = Color.Gray;
- //this.button1.ForeColor = Color.White;
- //this.button1.FlatStyle = FlatStyle.Flat;
- //this.button1.FlatAppearance.BorderSize = 0;
- }
- /// <summary>
- /// 第一步按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, EventArgs e)
- {
- //this.button4.PerformClick();
- }
- /// <summary>
- /// 第二步按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button2_Click(object sender, EventArgs e)
- {
- this.button3.PerformClick();
- }
- /// <summary>
- /// 生成景深扩展后的图片名
- /// </summary>
- /// <returns></returns>
- private string GetDepthImageName()
- {
- int numLength = nameNum.ToString().Length;
- string joinName = "";
- switch (numLength)
- {
- case 1:
- joinName = "00" + nameNum;
- break;
- case 2:
- joinName = "0" + nameNum;
- break;
- default:
- joinName = nameNum.ToString();
- break;
- }
- return PdnResources.GetString("Menu.Image.Text")+"-"+ PdnResources.GetString("Menu.Image.DepthExtension.Text")+ "-" + joinName;
- }
- /// <summary>
- /// 保存按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button7_Click(object sender, EventArgs e)
- {
- if (this.pictureBox1.Image != null)
- {
- double widthRatio = imageRect.Width / (depthBitmap.Width * 1d);//缩略图与原图的宽度比
- double heightRatio = imageRect.Height / (depthBitmap.Height * 1d);//缩略图与原图的高度比
- int xc = (this.pictureBox1.Width - this.imageRect.Width) / 2;//缩略图横坐标与控件原点的偏移量
- int yc = (this.pictureBox1.Height - this.imageRect.Height) / 2;//缩略图纵坐标与控件原点的偏移量
- int cutX = Convert.ToInt32((zoomRect.X - xc) / widthRatio);
- int cutY = Convert.ToInt32((zoomRect.Y - yc) / heightRatio);
- int cutWidth = Convert.ToInt32(zoomRect.Width / widthRatio);
- int cutHeight = Convert.ToInt32(zoomRect.Height / heightRatio);
- Bitmap cutBitmap = DrawRulerHelper.KiCut(new Bitmap(this.pictureBox1.Image), cutX, cutY, cutWidth, cutHeight);
- if (cutBitmap != null)
- {
- DocumentWorkspace dw = appWorkspace.AddNewDocumentWorkspace();
- Document document = Document.FromImageMat(Camera.Tools.ToMat(cutBitmap));
- dw.Document = document;
- dw.xmlSaveModel = Startup.instance.ruleDB;
- dw.InitRulerInfo();
- dw.fileText = this.textBox1.Text;
- appWorkspace.ActiveDocumentWorkspace = dw;
- }
- nameNum++;
- this.textBox1.Text = GetDepthImageName();
- }
- }
- /// <summary>
- /// 绘制picturebox中的矩形框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void pictureBox1_Paint(object sender, PaintEventArgs e)
- {
- if (this.pictureBox1.Image != null)
- {
- Pen rectPen = new Pen(Color.LightGreen);
- rectPen.Width = 3;
- if (isFirst)
- {
- //获取缩略图对应矩形
- PropertyInfo _ImageRectanglePropert = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
- imageRect = (Rectangle)_ImageRectanglePropert.GetValue(this.pictureBox1, null);
- zoomRect = new Rectangle(imageRect.X, imageRect.Y, imageRect.Width, imageRect.Height);
- }
- e.Graphics.DrawRectangle(rectPen, zoomRect.X, zoomRect.Y, zoomRect.Width - 1, zoomRect.Height - 1);
- e.Graphics.FillRectangle(new SolidBrush(Color.Black), new Rectangle(zoomRect.Right - 5, zoomRect.Bottom - 5, 7, 7));
- rectPen.Dispose();
- }
- }
- /// <summary>
- /// 矩形框的选中
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
- {
- lastPoint.X = e.X;
- lastPoint.Y = e.Y;
- //是否平移
- if (zoomRect.Contains(e.Location) && !new Rectangle(zoomRect.Right - 5, zoomRect.Bottom - 5, 7, 7).Contains(e.Location))
- isMoved = true;
- else
- isMoved = false;
- //是否调整大小
- if (new Rectangle(zoomRect.Right - 5, zoomRect.Bottom - 5, 7, 7).Contains(e.Location))
- isResized = true;
- else
- isResized = false;
- }
- /// <summary>
- /// 矩形框的拖动和改变大小
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
- {
- //修改鼠标形状
- this.pictureBox1.Cursor = Cursors.Default;
- if (zoomRect != null)
- {
- if (zoomRect.Contains(e.Location) && !new Rectangle(zoomRect.Right - 5, zoomRect.Bottom - 5, 7, 7).Contains(e.Location))
- {
- this.pictureBox1.Cursor = Cursors.SizeAll;
- }
- if (new Rectangle(zoomRect.Right - 5, zoomRect.Bottom - 5, 7, 7).Contains(e.Location))
- {
- this.pictureBox1.Cursor = Cursors.SizeNWSE;
- }
- }
- if (e.Button != MouseButtons.Left)
- return;
- //拖拽
- if (zoomRect != null && isMoved)
- {
- isFirst = false;
- Point p = e.Location;
- int dx = p.X - lastPoint.X;
- int dy = p.Y - lastPoint.Y;
- lastPoint.X = p.X;
- lastPoint.Y = p.Y;
- zoomRect.X += dx;
- zoomRect.Y += dy;
- //控制矩形框不超过缩略图尺寸
- int xc = (this.pictureBox1.Width - this.imageRect.Width) / 2;
- int yc = (this.pictureBox1.Height - this.imageRect.Height) / 2;
- if (zoomRect.X < xc) zoomRect.X = xc;
- if (zoomRect.Y < yc) zoomRect.Y = yc;
- if (zoomRect.X + zoomRect.Width > imageRect.Width + xc) zoomRect.X = imageRect.Width - zoomRect.Width + xc;
- if (zoomRect.Y + zoomRect.Height > imageRect.Height + yc) zoomRect.Y = imageRect.Height - zoomRect.Height + yc;
- this.pictureBox1.Refresh();
- }
- //调整大小
- if (zoomRect != null && isResized)
- {
- isFirst = false;
- Point p = e.Location;
- //不允许超出矩形框的原点
- if (p.X < zoomRect.X + 5)
- {
- p.X = zoomRect.X + 5;
- }
- if (p.Y < zoomRect.Y + 5)
- {
- p.Y = zoomRect.Y + 5;
- }
- //修改矩形框尺寸
- int top = zoomRect.Top;
- int left = zoomRect.Left;
- int right = zoomRect.Right;
- int bottom = zoomRect.Bottom;
- right = p.X;
- bottom = p.Y;
- zoomRect = new Rectangle(left, top, right - left, bottom - top);
- //控制矩形框不超过缩略图尺寸
- int xc = (this.pictureBox1.Width - this.imageRect.Width) / 2;
- int yc = (this.pictureBox1.Height - this.imageRect.Height) / 2;
- if (zoomRect.X < xc) zoomRect.X = xc;
- if (zoomRect.Y < yc) zoomRect.Y = yc;
- if (zoomRect.X + zoomRect.Width > imageRect.Width + xc) zoomRect.Width = imageRect.Width + xc - zoomRect.X;
- if (zoomRect.Y + zoomRect.Height > imageRect.Height + yc) zoomRect.Height = imageRect.Height + yc - zoomRect.Y;
- this.pictureBox1.Refresh();
- }
- }
- /// <summary>
- /// 实现景深扩展,返回图片
- /// </summary>
- private Bitmap GetDepthExtensionImage(List<Bitmap> listBitmaps, ref String errorMsg)
- {
- OpenCvSharp.Mat[] mats = new OpenCvSharp.Mat[listBitmaps.Count];
- for (int i = 0; i < listBitmaps.Count; i++)
- {
- mats[i] = PaintDotNet.Camera.Tools.ToMat(listBitmaps[i]);
- }
- if (mats != null && mats.Count() > 0)
- {
- Bitmap depthBitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(Merge.GetMergeMat(mats, ref errorMsg));
- return depthBitmap;
- }
- else
- return null;
- }
- }
- }
|