using PaintDotNet.Base.CommTool;
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.Instrument
{
///
/// 三维显示
///
internal class ImageThreeDShowDialog : PdnBaseForm
{
private GroupBox groupBox2;
private Button button6;
private Button button5;
private Button button4;
private GroupBox groupBox3;
private Panel panel1;
private PictureBox pictureBox1;
///
/// 辅助判断目前最新一个自定义控件的tag值
///
private int toolsNum = 1;
///
/// 图片选择窗口
///
private PaintDotNet.Processing.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 Button button1;
private AppWorkspace appWorkspace;
private List bitsHeight = new List();
public ImageThreeDShowDialog(AppWorkspace appWorkspace)
{
this.appWorkspace = appWorkspace;
InitializeComponent();
InitializeLanguageText();
InitializeOtherInfo();
AddDepthExtensionPicTools();
}
private void InitializeLanguageText()
{
this.groupBox2.Text = PdnResources.GetString("Menu.operation.text");
//this.groupBox2.Text = PdnResources.GetString("Menu.Selectimagefrom.text");
this.button1.Text = PdnResources.GetString("Menu.tool.Generateshortcut.Importconfiguration.text");
this.button6.Text = PdnResources.GetString("Menu.Currentsoftware.text");
this.button5.Text = PdnResources.GetString("Menu.Theharddiskimage.Text");
this.button4.Text = PdnResources.GetString("Menu.3Dimagegeneration.text");
this.groupBox3.Text = PdnResources.GetString("Menu.Imagelist.Text");
this.Text = PdnResources.GetString("Menu.3ddisplay.Text");
}
///
/// 初始化基础控件
///
private void InitializeComponent()
{
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.panel1 = new System.Windows.Forms.Panel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// groupBox2
//
this.groupBox2.Controls.Add(this.button1);
this.groupBox2.Controls.Add(this.button6);
this.groupBox2.Controls.Add(this.button5);
this.groupBox2.Controls.Add(this.button4);
this.groupBox2.Location = new System.Drawing.Point(13, 13);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(623, 60);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(210, 20);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.UseVisualStyleBackColor = true;
this.button1.Visible = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(306, 20);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 23);
this.button6.TabIndex = 1;
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(401, 20);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 0;
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(496, 20);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(105, 23);
this.button4.TabIndex = 0;
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.panel1);
this.groupBox3.Controls.Add(this.pictureBox1);
this.groupBox3.Location = new System.Drawing.Point(13, 89);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(623, 511);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "预览";
//
// 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;
//
// 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;
//
// ImageThreeDShowDialog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.ClientSize = new System.Drawing.Size(648, 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 = "ImageThreeDShowDialog";
this.Text = "三维显示";
this.Controls.SetChildIndex(this.groupBox2, 0);
this.Controls.SetChildIndex(this.groupBox3, 0);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
///
/// 初始化其他
///
private void InitializeOtherInfo()
{
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);
}
toolsNum++;
}
///
/// 绘制删除图标
///
///
///
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)
{
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()
{
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)
{
if (imagePickerDialog == null)
{
imagePickerDialog = new PaintDotNet.Processing.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 PaintDotNet.Processing.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 Bitmap ChooseImageFromHardDisk()
{
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()
{
bitsHeight.Clear();
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)
{
//查找xml配置文件
string pPath = null;// System.IO.Path.GetDirectoryName(cmbInput.Text);
string[] imageNames = openFileDialog.FileNames;
List listBitmap = new List();
List listBitNames = new List();
foreach (string imgName in imageNames)
{
if (pPath == null)
pPath = System.IO.Path.GetDirectoryName(imgName);
Image image = Image.FromFile(imgName);
listBitmap.Add(new Bitmap(image));
listBitNames.Add(System.IO.Path.GetFileName(imgName));
}
////没有找到配置文件
//if (pPath == null)
// return listBitmap;
List lst = new List();
List lstFiles = getFile(pPath, ".xml", lst);
//没有找到合适的配置文件
if (lstFiles.Count != 1)
return listBitmap;
string xmlPath = null;// pPath + "\\" + /*Path.GetFileNameWithoutExtension(fileName) + */".xml";
foreach (System.IO.FileInfo shpFile in lstFiles)
{
xmlPath = shpFile.FullName;// + "\n";
}
//没有找到配置文件
if (xmlPath == null)
return listBitmap;
//如果xml存在
if (System.IO.File.Exists(xmlPath))
{
using (System.IO.Stream shortbarStream = System.IO.File.OpenRead(xmlPath))
{
System.IO.StreamReader sr = new System.IO.StreamReader(shortbarStream);
string xmlNotes = sr.ReadToEnd();
Base.SettingModel.ZAxisScanModel toolbarXml = XmlSerializeHelper.DESerializer(xmlNotes);
if (toolbarXml.items.Count > 0)
{
this.lists.Clear();
this.lists.AddRange(toolbarXml.items);
//MessageBox.Show("导入配置成功");
if (listBitNames.Count > 0)
{
for (int names_index = 0; names_index < listBitNames.Count; names_index++)
{
bool findNames = false;
foreach (var itemBits in this.lists)
if (itemBits.fileName.Contains(listBitNames[names_index]))
{
findNames = true;
bitsHeight.Add(itemBits.height);
break;
}
if (!findNames)
bitsHeight.Add(decimal.Zero);
}
}
}
else
{
MessageBox.Show("配置文件格式不正确");
}
}
}
return listBitmap;
}
else
{
return null;
}
}
///
/// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
///
/// 文件夹路径
/// 扩展名可以多个 例如 .mp3.wma.rm
/// List
private List getFile(string path, string extName, List lst)
{
try
{
string[] dir = System.IO.Directory.GetDirectories(path); //文件夹列表
System.IO.DirectoryInfo fdir = new System.IO.DirectoryInfo(path);
System.IO.FileInfo[] file = fdir.GetFiles();
//FileInfo[] file = Directory.GetFiles(path); //文件列表
if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
{
foreach (System.IO.FileInfo f in file) //显示当前目录所有文件
if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
lst.Add(f);
foreach (string d in dir)
getFile(d, extName, lst);//递归
}
return lst;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 硬盘选择图片按钮
///
///
///
private void button5_Click(object sender, EventArgs e)
{
List listBitmap = ChooseImagesFromHardDisk();
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 button4_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Visible == true)
return;
bool isSameSize = true;
//Bitmap origin = this.appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreateAliasedBitmap();
string mode = "H";
int maxSize = 640;//用于限制图像大小
bool needResize = true;//需要限制图像大小
int bitmapWidth = -1;
int bitmapHeight = -1;
//if (origin.Width > origin.Height)
// mode = "W";
//Mat OrgImg = PaintDotNet.Camera.Tools.ToMat(this.appWorkspace.ActiveDocumentWorkspace.CompositionSurface.MakeThumbnail(origin, maxSize, maxSize, mode));// OpenCvSharp.Cv2.ImRead(fileName);
List listBitmaps = new List();
Bitmap originBitmap;
foreach (Control C in this.panel1.Controls)
{
if (C is DepthExtensionPicTool)
{
DepthExtensionPicTool depthExtensionPicTool = (DepthExtensionPicTool)C;
if (depthExtensionPicTool.pictureBox1.Image != null)
{
originBitmap = new Bitmap(depthExtensionPicTool.pictureBox1.Image);
if (bitmapWidth == -1/* && bitmapHeight == -1*/)
{
bitmapWidth = originBitmap.Width;
bitmapHeight = originBitmap.Height;
if (maxSize >= bitmapHeight && maxSize >= bitmapWidth)
needResize = false;
else if (bitmapWidth > bitmapHeight)
mode = "W";
}
else if (bitmapWidth != originBitmap.Width || bitmapHeight != originBitmap.Height)
{
isSameSize = false;
break;
}
if (needResize)
listBitmaps.Add(Surface.MakeThumbnail(originBitmap, maxSize, maxSize, mode));
else
listBitmaps.Add(originBitmap);
}
}
}
if (!isSameSize)//图像宽高不一致
{
MessageBox.Show(PdnResources.GetString("Menu.noyizhi.Text"));
return;
}
//没有上传或选择图片不进行下一步
if (listBitmaps == null || listBitmaps.Count == 0)
{
MessageBox.Show(PdnResources.GetString("Menu.pleaseintopicture.Text"));
return;
}
OpenCvSharp.Mat[] mats = new OpenCvSharp.Mat[listBitmaps.Count];
for (int i = 0; i < listBitmaps.Count; i++)
{
mats[i] = PaintDotNet.Camera.Tools.ToMat(listBitmaps[i]);
}
String errorMsg = "";
//if (mats != null && mats.Count() > 0)
//{
// Bitmap depthBitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(Merge.GetMergeMat(mats, ref errorMsg));
// //return depthBitmap;
//}
////else
//// return null;
//根据Z轴扫描生成三维图像数据
//if (this.lists.Count > 0 && this.bitsHeight.Count > 0)
//{
// if (mats != null && mats.Count() > 0 && Merge.GetMergeMatHeight(mats, ref errorMsg, this.bitsHeight))
// SRWindow.Run(this.appWorkspace);
// return;
//}
Form test = Application.OpenForms["SRWindow"];
if (mats != null && mats.Count() > 0 && Merge.GetMergeMatHeight(mats, ref errorMsg, this.bitsHeight))
{
if ((test == null) || (test.IsDisposed))
{
SRWindow.Run(this.appWorkspace);
}
else
{
test.Activate();
test.WindowState = FormWindowState.Normal;
}
}
}
///
/// 绘制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();
}
}
List lists = new List();
///
/// 导入配置
///
///
///
private void button1_Click(object sender, EventArgs e)
{
using (var openFileDialog = new OpenFileDialog { Filter = "*.xml|*.xml" })
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//openFileDialog.InitialDirectory
using (System.IO.Stream shortbarStream = System.IO.File.OpenRead(openFileDialog.FileName))
{
System.IO.StreamReader sr = new System.IO.StreamReader(shortbarStream);
string xmlNotes = sr.ReadToEnd();
Base.SettingModel.ZAxisScanModel toolbarXml = XmlSerializeHelper.DESerializer(xmlNotes);
if (toolbarXml.items.Count > 0)
{
this.lists.Clear();
this.lists.AddRange(toolbarXml.items);
MessageBox.Show("导入配置成功");
}
else
{
MessageBox.Show("配置文件格式不正确");
}
}
}
}
}
}
}