using OpenCvSharp; using PaintDotNet.Base.CommTool; using PaintDotNet.DbOpreate.DbBll; using PaintDotNet.DbOpreate.DbModel; using System; using System.Data; using System.Drawing; using System.IO; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; /// /// 位图分析 /// namespace PaintDotNet.Measuring { partial class BitmapAnalysisDialog : Form { /// /// 主控件 /// private AppWorkspace appWorkspace; /// /// 画布 /// private PanelEx panel; /// /// 图像mat /// private Mat mat; /// /// 单元格宽度 /// private int unitWidth = 50; /// /// 单元格高度 /// private int unitHeight = 20; private HScrollBar hScrollBar1; private VScrollBar vScrollBar1; private int num = 3; private decimal oldNumValue = 0; /// /// 画笔 /// private Pen pen = new Pen(Color.Black); public BitmapAnalysisDialog(AppWorkspace appWorkspace) { this.appWorkspace = appWorkspace; this.InitializeComponent(); InitializeLanguageText(); this.InitializeControl(); } private void InitializeControl() { if (this.appWorkspace.ActiveDocumentWorkspace != null) { mat = PaintDotNet.Camera.Tools.ToMat( this.appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreateAliasedBitmap()); } if(mat != null) { this.panel = new PanelEx(); this.panel.Location = new System.Drawing.Point(0, 0); this.panel.Size = this.panel1.ClientSize; this.panel.Paint += this.panel_Paint; this.panel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom; this.panel1.Controls.Add(this.panel); this.hScrollBar1.Maximum = (mat.Width + 1) * unitWidth * num - this.panel1.ClientSize.Width + unitWidth; this.vScrollBar1.Maximum = (mat.Height + 2) * unitHeight - this.panel1.ClientSize.Height; if (mat.Type() == MatType.CV_8UC1) { this.hScrollBar1.LargeChange = unitWidth; this.hScrollBar1.SmallChange = unitWidth; } else { this.hScrollBar1.LargeChange = 3 * unitWidth; this.hScrollBar1.SmallChange = 3 * unitWidth; } this.vScrollBar1.LargeChange = unitHeight; this.vScrollBar1.SmallChange = unitHeight; this.hScrollBar1.Scroll += this.panel1_Scroll; this.vScrollBar1.Scroll += this.panel1_Scroll; } else { return; } } private void panel1_MouseWheel(object sender, MouseEventArgs e) { this.panel.Location = new System.Drawing.Point(0, 0); this.panel.Refresh(); } private void panel1_Scroll(object sender, ScrollEventArgs e) { this.panel.Location = new System.Drawing.Point(0, 0); this.panel.Refresh(); } private void panel1_Resize(object sender, EventArgs e) { if (this.panel1 != null && mat!=null) { this.hScrollBar1.Maximum = (mat.Width + 1) * unitWidth * num - this.panel1.ClientSize.Width + 50; this.vScrollBar1.Maximum = (mat.Height + 2) * unitHeight - this.panel1.ClientSize.Height; this.panel.Size = this.panel1.ClientSize; this.panel.Location = new System.Drawing.Point(0, 0); this.panel.Refresh(); } } private void panel_Paint(object sender, PaintEventArgs e) { //计算起止数据 int h_s_index = Math.Abs(this.hScrollBar1.Value / unitWidth / num); int v_s_index = Math.Abs(this.vScrollBar1.Value / unitHeight); int h_e_index = 0; if (this.oldNumValue > 1) h_e_index = h_s_index + this.panel1.ClientSize.Width * (int)this.oldNumValue / unitWidth / num; else h_e_index = h_s_index + this.panel1.ClientSize.Width / unitWidth / num; int v_e_index = 0; if (this.oldNumValue > 1) v_e_index = v_s_index + this.panel1.ClientSize.Height * (int)this.oldNumValue / unitHeight; else v_e_index = v_s_index + this.panel1.ClientSize.Height / unitHeight; Vec3b vec3B = new Vec3b(); int v1 = 1; int v2 = 1; int v3 = v_s_index; for (int v = v_s_index; v < v_e_index; v++) { int h1 = 1; if(v1 == 1) { e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, unitWidth, unitHeight)); e.Graphics.DrawString("Pixel", new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Red), 0, 0); } if (this.oldNumValue > 1) { if ((v1 - 1 + v_s_index) % this.oldNumValue == 0) { if (v < mat.Height) { e.Graphics.DrawRectangle(pen, new Rectangle(0, v2 * unitHeight, unitWidth, unitHeight)); e.Graphics.DrawString((v1 - 1 + v_s_index) + "", new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), 0, v2 * unitHeight); } for (int h = h_s_index; h < h_e_index; h++) { if (mat.Type() == MatType.CV_8UC1) { if (h < mat.Width && v < mat.Height) { if (h % this.oldNumValue == 0) { if (v3 == v_s_index) { e.Graphics.DrawRectangle(pen, new Rectangle(h1 * unitWidth, 0, unitWidth, unitHeight)); e.Graphics.DrawString(h + "", new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), h1 * unitWidth, 0); } e.Graphics.DrawString("" + mat.At(v, h), new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), h1 * unitWidth, v2 * unitHeight); e.Graphics.DrawRectangle(pen, new Rectangle(h1 * unitWidth, 0, h1 * unitWidth, v2 * unitHeight)); h1++; } } } else { if (h < mat.Width && v < mat.Height) { if(h % this.oldNumValue == 0) { if (v3 == v_s_index) { e.Graphics.DrawRectangle(pen, new Rectangle(h1 * unitWidth, 0, unitWidth, unitHeight)); e.Graphics.DrawRectangle(pen, new Rectangle((h1 + 1) * unitWidth, 0, unitWidth, unitHeight)); e.Graphics.DrawRectangle(pen, new Rectangle((h1 + 2) * unitWidth, 0, unitWidth, unitHeight)); e.Graphics.DrawString(h + "", new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), (h1 + 2) * unitWidth, 0); } vec3B = mat.At(v, h); e.Graphics.DrawString("" + vec3B.Item0, new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), h1 * unitWidth, v2 * unitHeight); e.Graphics.DrawString("" + vec3B.Item1, new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), (h1 + 1) * unitWidth, v2 * unitHeight); e.Graphics.DrawString("" + vec3B.Item2, new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), (h1 + 2) * unitWidth, v2 * unitHeight); e.Graphics.DrawRectangle(pen, new Rectangle(h1 * unitWidth, v2 * unitHeight, unitWidth, unitHeight)); e.Graphics.DrawRectangle(pen, new Rectangle((h1 + 1) * unitWidth, v2 * unitHeight, unitWidth, unitHeight)); e.Graphics.DrawRectangle(pen, new Rectangle((h1 + 2) * unitWidth, v2 * unitHeight, unitWidth, unitHeight)); h1 = h1 + 3; } } } } v2++; v3++; } } else { if (v < mat.Height) { e.Graphics.DrawRectangle(pen, new Rectangle(0, v1 * unitHeight, unitWidth, unitHeight)); e.Graphics.DrawString((v1 - 1 + v_s_index) + "", new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), 0, v1 * unitHeight); } for (int h = h_s_index; h < h_e_index; h++) { if (mat.Type() == MatType.CV_8UC1) { if (h < mat.Width && v < mat.Height) { e.Graphics.DrawString("" + mat.At(v, h), new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), h1 * unitWidth, v1 * unitHeight); e.Graphics.DrawRectangle(pen, new Rectangle(h1 * unitWidth, v1 * unitHeight, unitWidth, unitHeight)); h1++; } } else { if (h < mat.Width && v < mat.Height) { if (v == v_s_index) { e.Graphics.DrawRectangle(pen, new Rectangle(h1 * unitWidth, 0, unitWidth, unitHeight)); e.Graphics.DrawRectangle(pen, new Rectangle((h1 + 1) * unitWidth, 0, unitWidth, unitHeight)); e.Graphics.DrawRectangle(pen, new Rectangle((h1 + 2) * unitWidth, 0, unitWidth, unitHeight)); e.Graphics.DrawString(h + "", new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), (h1 + 2) * unitWidth, 0); } vec3B = mat.At(v, h); e.Graphics.DrawString("" + vec3B.Item0, new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), h1 * unitWidth, v1 * unitHeight); e.Graphics.DrawString("" + vec3B.Item1, new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), (h1 + 1) * unitWidth, v1 * unitHeight); e.Graphics.DrawString("" + vec3B.Item2, new Font("Microsoft Sans Serif", 12), new SolidBrush(Color.Black), (h1 + 2) * unitWidth, v1 * unitHeight); e.Graphics.DrawRectangle(pen, new Rectangle(h1 * unitWidth, v1 * unitHeight, unitWidth, unitHeight)); e.Graphics.DrawRectangle(pen, new Rectangle((h1 + 1) * unitWidth, v1 * unitHeight, unitWidth, unitHeight)); e.Graphics.DrawRectangle(pen, new Rectangle((h1 + 2) * unitWidth, v1 * unitHeight, unitWidth, unitHeight)); } h1 = h1 + 3; } } } v1++; } } /// /// 数据导出按钮按下 /// /// /// private void button2_Click(object sender, EventArgs e) { if(mat!=null) { if (mat.Width > 1500 && mat.Height > 1500) MessageBox.Show(PdnResources.GetString("Menu.Thepictureistoolarge.Text")+"!"); else this.OutPutExcel(); } } /// /// 确认按钮按下 /// /// /// private void button1_Click(object sender, EventArgs e) { if(this.panel!=null) { this.oldNumValue = this.numericUpDown1.Value; this.panel.Location = new System.Drawing.Point(0, 0); this.panel.Refresh(); } } /// /// Excel导出 /// private void OutPutExcel() { string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + PdnResources.GetString("Menu.Statisticaldattable.Text"); SaveFileDialog exe = new SaveFileDialog(); exe.Filter = "Execl files (*.xls)|*.xls"; exe.FilterIndex = 0; exe.RestoreDirectory = true; exe.Title = "Export Excel File"; exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); exe.FileName = fileName; DialogResult dr = exe.ShowDialog(); if (dr != DialogResult.OK) { return; } ProgressThreadProcClass procClass = new ProgressThreadProcClass(); System.Threading.ThreadStart copyThreadProc = delegate () { bool isNeedZoom;//是否需要缩放 int dotPitch = Startup.instance.configModel.DotPitchId;//获取系统激活的点距id double pointPitch = 0; if (dotPitch > 0) { mic_screen_rules screenList = mic_screen_rules_BLL.FindDefault(dotPitch);//从数据库查询点距具体信息 if (screenList != null) { try { pointPitch = double.Parse(screenList.point_pitch);//获取点距 if (pointPitch > 0) isNeedZoom = true; } catch (System.Exception) { } } } try { DataTable dtb = new DataTable(); Vec3b vec3B = new Vec3b(); bool a = true; if (mat.Type() == MatType.CV_8UC1) { for (int i = 0; i <= mat.Width; i++) { if (this.oldNumValue > 1) { if (i % this.oldNumValue == 0) { if (i == 0) dtb.Columns.Add("one"); dtb.Columns.Add(i.ToString()); } } else { if (i == 0) dtb.Columns.Add("one"); dtb.Columns.Add(i.ToString()); } } int height; if (this.oldNumValue > 1) height = mat.Height + (int)this.oldNumValue; else height = mat.Height + 1; for (int i = 0; i < height; i++) { DataRow dataRow = dtb.NewRow(); for (int j = 0; j < mat.Width; j++) { if (i == 0 && j == 0) dataRow["one"] = "Pixel"; if (i == 0 && j > 0) { if (this.oldNumValue > 1) { if (j - this.oldNumValue == 0) dataRow["0"] = (j - this.oldNumValue).ToString(); if (j % this.oldNumValue == 0) { dataRow[j.ToString()] = j.ToString(); } } else { dataRow["0"] = "0"; dataRow[j.ToString()] = j.ToString(); } } if (i > 0 && j == 0) { if (this.oldNumValue > 1) { if ((i - this.oldNumValue) % this.oldNumValue == 0) { dataRow["one"] = (i - this.oldNumValue).ToString(); } } else { dataRow["one"] = (i - 1).ToString(); } } if (i > 0 && j > 0) { if (this.oldNumValue > 1) { if (i % this.oldNumValue == 0 && j % this.oldNumValue == 0) { if (j - this.oldNumValue == 0) dataRow["0"] = "" + mat.At(i - (int)this.oldNumValue, 0); dataRow[j.ToString()] = "" + mat.At(i - (int)this.oldNumValue, j - (int)this.oldNumValue); } } else { if (j == 1) dataRow["0"] = "" + mat.At(i - 1, 0); dataRow[j.ToString()] = "" + mat.At(i - 1, j - 1); } } } if (this.oldNumValue > 1) { if (i % this.oldNumValue == 0) { dtb.Rows.Add(dataRow); } } else { dtb.Rows.Add(dataRow); } } } else { for (int i = 0; i <= mat.Width * 3 + 2; i++) { if (i == 0) dtb.Columns.Add("one"); dtb.Columns.Add(i.ToString()); } int height; if (this.oldNumValue > 1) height = mat.Height + (int)this.oldNumValue; else height = mat.Height + 1; for (int i = 0; i < height; i++) { DataRow dataRow = dtb.NewRow(); int oneCol = 0; int oneRow1 = 0; for (int j = 0; j <= mat.Width * 3; j++) { if (i == 0 && j == 0) dataRow["one"] = "Pixel"; if (i == 0 && j > 0 && oneCol < mat.Width) { if (this.oldNumValue > 1) { if (oneCol % this.oldNumValue == 0) { dataRow[(j + 1).ToString()] = oneCol.ToString(); j = j + 2; } else { j--; } oneCol++; } else { dataRow[(j + 1).ToString()] = oneCol.ToString(); oneCol++; j = j + 2; } } if (i > 0 && j == 0) { if (this.oldNumValue > 1) { if ((i - this.oldNumValue) % this.oldNumValue == 0) { dataRow["one"] = (i - this.oldNumValue).ToString(); } } else { dataRow["one"] = (i - 1).ToString(); } } if (i > 0 && j > 0 && oneRow1 <= mat.Width) { if (this.oldNumValue > 1) { if ((i - this.oldNumValue) % this.oldNumValue == 0 && oneRow1 % this.oldNumValue == 0) { if ((i - this.oldNumValue - 1) < mat.Height && oneRow1 < mat.Width) { vec3B = mat.At((int)(i - this.oldNumValue), oneRow1); dataRow[(j - 1).ToString()] = "" + vec3B.Item0; dataRow[(j).ToString()] = "" + vec3B.Item1; dataRow[(j + 1).ToString()] = "" + vec3B.Item2; j = j + 2; } } else { j--; } oneRow1++; } else { if ((i - 1) < mat.Height && (j - 1) < mat.Width * 3) { vec3B = mat.At(i - 1, (j - 1) / 3); dataRow[(j - 1).ToString()] = "" + vec3B.Item0; dataRow[(j).ToString()] = "" + vec3B.Item1; dataRow[(j + 1).ToString()] = "" + vec3B.Item2; j = j + 2; } } } } if (this.oldNumValue > 1) { if (i % this.oldNumValue == 0) { dtb.Rows.Add(dataRow); } } else { dtb.Rows.Add(dataRow); } } } Stream ms; ms = exe.OpenFile(); StreamWriter sw = new StreamWriter(ms, System.Text.Encoding.GetEncoding(-0)); try { for (int j = 0; j < dtb.Rows.Count; j++) { string temp = ""; for (int k = 0; k < dtb.Columns.Count; k++) { if (k > 0) { temp += "\t"; } string cell = dtb.Rows[j][k].ToString(); cell = cell.Replace(" ", "").Replace("\r", "").Replace("\n", "").Replace("\r\n", ""); temp += cell; } sw.WriteLine(temp); } sw.Close(); ms.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { sw.Close(); ms.Close(); } if(OfficeFileHandleHelper.ConvertWorkbook(exe.FileName, fileName)) { if (System.IO.File.Exists(exe.FileName)) System.IO.File.Delete(exe.FileName); } procClass.DismissProgressAction(this); } catch (Exception) { } finally { procClass.DismissProgressAction(this); } }; procClass.StartProgressAutoAction(this, new System.Threading.ThreadStart(copyThreadProc), PdnResources.GetString("Menu.MeasureAction.BitmapAnalysis.Text")); } #region 设计器生成 /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.operation.text"); this.label1.Text = PdnResources.GetString("Menu.Imagement.Bitmapanalysis.Samplingvalue.text") + ":"; this.button2.Text = PdnResources.GetString("Menu.Imagement.Bitmapanalysis.exportdata.text"); this.button1.Text = PdnResources.GetString("Menu.ensure.text"); this.Text = PdnResources.GetString("Menu.MeasureAction.BitmapAnalysis.Text"); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.label1 = new System.Windows.Forms.Label(); this.button2 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); this.panel1 = new System.Windows.Forms.Panel(); this.vScrollBar1 = new System.Windows.Forms.VScrollBar(); this.hScrollBar1 = new System.Windows.Forms.HScrollBar(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.button2); this.groupBox1.Controls.Add(this.button1); this.groupBox1.Controls.Add(this.numericUpDown1); this.groupBox1.Location = new System.Drawing.Point(3, 5); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(792, 109); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; // // label1 // this.label1.AutoSize = true; this.label1.ForeColor = System.Drawing.SystemColors.Desktop; this.label1.Location = new System.Drawing.Point(35, 46); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(53, 12); this.label1.TabIndex = 4; this.label1.Text = "取样值:"; // // button2 // this.button2.BackColor = System.Drawing.SystemColors.Control; this.button2.Location = new System.Drawing.Point(523, 37); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(105, 31); this.button2.TabIndex = 3; this.button2.Text = "导出数据"; this.button2.UseVisualStyleBackColor = false; this.button2.Click += new System.EventHandler(this.button2_Click); // // button1 // this.button1.BackColor = System.Drawing.SystemColors.Control; this.button1.Location = new System.Drawing.Point(220, 37); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(79, 31); this.button1.TabIndex = 2; this.button1.Text = "确定"; this.button1.UseVisualStyleBackColor = false; this.button1.Click += new System.EventHandler(this.button1_Click); // // numericUpDown1 // this.numericUpDown1.Location = new System.Drawing.Point(95, 44); this.numericUpDown1.Maximum = new decimal(new int[] { 99999, 0, 0, 0}); this.numericUpDown1.Name = "numericUpDown1"; this.numericUpDown1.Size = new System.Drawing.Size(85, 21); this.numericUpDown1.TabIndex = 1; this.numericUpDown1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // panel1 // this.panel1.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.panel1.AutoScroll = true; this.panel1.Location = new System.Drawing.Point(3, 120); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(774, 538); this.panel1.TabIndex = 2; this.panel1.Resize += new System.EventHandler(this.panel1_Resize); // // vScrollBar1 // this.vScrollBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); this.vScrollBar1.LargeChange = 20; this.vScrollBar1.Location = new System.Drawing.Point(778, 119); this.vScrollBar1.Name = "vScrollBar1"; this.vScrollBar1.Size = new System.Drawing.Size(17, 539); this.vScrollBar1.SmallChange = 20; this.vScrollBar1.TabIndex = 1; // // hScrollBar1 // this.hScrollBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.hScrollBar1.LargeChange = 50; this.hScrollBar1.Location = new System.Drawing.Point(3, 659); this.hScrollBar1.Name = "hScrollBar1"; this.hScrollBar1.Size = new System.Drawing.Size(773, 18); this.hScrollBar1.SmallChange = 50; this.hScrollBar1.TabIndex = 0; // // BitmapAnalysisDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.Control; this.ClientSize = new System.Drawing.Size(798, 679); this.Controls.Add(this.hScrollBar1); this.Controls.Add(this.vScrollBar1); this.Controls.Add(this.panel1); this.Controls.Add(this.groupBox1); this.Name = "BitmapAnalysisDialog"; this.Text = "位图分析"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button1; private System.Windows.Forms.NumericUpDown numericUpDown1; private System.Windows.Forms.Panel panel1; #endregion } }