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
{
///
/// 景深扩展
///
internal class ImageDepthExtensionDialog : PdnBaseForm
{
private Button button3;
private GroupBox groupBox2;
///
/// 辅助判断目前最新一个自定义控件的tag值
///
private int toolsNum = 1;
///
/// 图片选择窗口
///
private InterImagePickerDialog imagePickerDialog;
///
/// 辅助为合并的图片命名
///
private int nameNum = 1;
///
/// 是否重置picturebox中的矩形尺寸
///
private bool isFirst = true;
///
/// 是否为拖动picturebox中的矩形框
///
private bool isMoved = false;
///
/// 是否为修改picturebox中的矩形框大小
///
private bool isResized = false;
///
/// 辅助计算偏移量
///
private Point lastPoint = new Point(0, 0);
///
/// picturebox中图片对应矩形
///
private Rectangle imageRect;
///
/// picturebox中选择框对应矩形
///
private Rectangle zoomRect;
///
/// 合成后的图片
///
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");
}
///
/// 初始化基础控件
///
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);
}
///
/// 初始化其他
///
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);
}
///
/// 添加一个图片的自定义控件
///
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;
///
/// 绘制删除图标
///
///
///
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);
}
}
///
/// 绘制添加功能的十字线
///
///
///
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();
}
///
/// 点击添加标志
///
///
///
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 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();
//}
}
///
/// 点击删除图标
///
///
///
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();
}
}
///
/// 重置所有图片控件的位置
///
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 listTools = new List();
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);
}
}
///
/// 图库选择按钮
///
///
///
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();
}
}
}
///
/// 从图库选择待对照图片
///
///
///
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;
}
}
}
}
}
///
/// 从硬盘选择单张图片
///
///
private List ChooseImageFromHardDisk()
{
List bitmaps = new List();
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;
//}
}
///
/// 从硬盘选择多张图片
///
///
private List 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 listBitmap = new List();
foreach(string imgName in imageNames)
{
Image image = Image.FromFile(imgName);
listBitmap.Add(new Bitmap(image));
}
return listBitmap;
}
else
{
return null;
}
}
///
/// 硬盘选择图片按钮
///
///
///
private void button5_Click(object sender, EventArgs e)
{
List 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();
}
}
}
}
}
///
/// 图像融合
///
///
///
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 listBitmaps = new List();
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;
}
///
/// 返回按钮
///
///
///
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;
}
///
/// 第一步按钮
///
///
///
private void button1_Click(object sender, EventArgs e)
{
//this.button4.PerformClick();
}
///
/// 第二步按钮
///
///
///
private void button2_Click(object sender, EventArgs e)
{
this.button3.PerformClick();
}
///
/// 生成景深扩展后的图片名
///
///
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;
}
///
/// 保存按钮
///
///
///
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();
}
}
///
/// 绘制picturebox中的矩形框
///
///
///
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();
}
}
///
/// 矩形框的选中
///
///
///
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;
}
///
/// 矩形框的拖动和改变大小
///
///
///
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();
}
}
///
/// 实现景深扩展,返回图片
///
private Bitmap GetDepthExtensionImage(List 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;
}
}
}