using PaintDotNet.Processing; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PaintDotNet.Instrument { internal class ImageComparison : PdnBaseForm { private GroupBox groupBox1; private Button button2; private GroupBox groupBox2; private GroupBox groupBox3; private GroupBox groupBox4; private Button button4; private Button button3; private Button button6; private Button button5; private PictureBox pictureBox1; private TrackBar trackBar1; private Button button7; private PictureBox pictureBox2; private TrackBar trackBar2; private Button button8; private Button button1; private PictureBox pictureBox3; /// /// 图片选择窗口 /// private InterImagePickerDialog imagePickerDialog; /// /// 第一个小picturebox的原图 /// private Bitmap bitmap1; /// /// 第二个小picturebox的原图 /// private Bitmap bitmap2; /// /// 合并之后的图片 /// private Bitmap combineBitmap; /// /// 合并时所使用的修改过透明度的图片数组 /// private Bitmap[] bitArray = new Bitmap[2]; /// /// 判断两张图片的绘制顺序 /// private bool ordered = true; /// /// 文件名排序 /// string stepLength = "00" + Startup.instance.step_length; /// /// 判断当前点击的按钮是哪个小picturebox的 /// private int pictureBoxNum = 0; private AppWorkspace appWorkspace; public ImageComparison(AppWorkspace appWorkspace) { this.appWorkspace = appWorkspace; InitializeComponent(); InitializeLanguageText(); } private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.operation.text"); this.button2.Text = PdnResources.GetString("Menu.Createpicture.text"); this.button1.Text = PdnResources.GetString("Menu.File.Close.Text"); this.groupBox2.Text = PdnResources.GetString("Menu.tool.imagecomparison.compareresults.text"); this.groupBox3.Text = PdnResources.GetString("Menu.picture.Text"); this.button4.Text = PdnResources.GetString("Menu.Gallery.text"); this.button3.Text = PdnResources.GetString("Menu.harddisk.text"); this.groupBox4.Text = PdnResources.GetString("Menu.picture.Text"); this.button6.Text = PdnResources.GetString("Menu.Gallery.text"); this.button5.Text = PdnResources.GetString("Menu.harddisk.text"); this.Text = PdnResources.GetString("Menu.Tools.PictureComparison.Text"); } private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.button2 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.pictureBox3 = new System.Windows.Forms.PictureBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.button7 = new System.Windows.Forms.Button(); this.trackBar1 = new System.Windows.Forms.TrackBar(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.button4 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.groupBox4 = new System.Windows.Forms.GroupBox(); this.button8 = new System.Windows.Forms.Button(); this.trackBar2 = new System.Windows.Forms.TrackBar(); this.pictureBox2 = new System.Windows.Forms.PictureBox(); this.button6 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit(); this.groupBox3.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.groupBox4.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.trackBar2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).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.button2); this.groupBox1.Controls.Add(this.button1); this.groupBox1.Location = new System.Drawing.Point(13, 13); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(621, 55); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "操作"; // // button2 // this.button2.Anchor = System.Windows.Forms.AnchorStyles.Right; this.button2.Location = new System.Drawing.Point(426, 20); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 1; this.button2.Text = "创建图片"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button1 // this.button1.Anchor = System.Windows.Forms.AnchorStyles.Right; this.button1.Location = new System.Drawing.Point(524, 20); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "关闭"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // groupBox2 // this.groupBox2.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.groupBox2.Controls.Add(this.pictureBox3); this.groupBox2.Location = new System.Drawing.Point(13, 75); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(383, 469); this.groupBox2.TabIndex = 1; this.groupBox2.TabStop = false; this.groupBox2.Text = "对比结果"; // // pictureBox3 // this.pictureBox3.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.pictureBox3.Location = new System.Drawing.Point(7, 21); this.pictureBox3.Name = "pictureBox3"; this.pictureBox3.Size = new System.Drawing.Size(370, 434); this.pictureBox3.TabIndex = 0; this.pictureBox3.TabStop = false; // // groupBox3 // this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.groupBox3.Controls.Add(this.button7); this.groupBox3.Controls.Add(this.trackBar1); this.groupBox3.Controls.Add(this.pictureBox1); this.groupBox3.Controls.Add(this.button4); this.groupBox3.Controls.Add(this.button3); this.groupBox3.Location = new System.Drawing.Point(402, 75); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(232, 232); this.groupBox3.TabIndex = 2; this.groupBox3.TabStop = false; this.groupBox3.Text = "图片"; // // button7 // this.button7.Location = new System.Drawing.Point(197, 181); this.button7.Name = "button7"; this.button7.Size = new System.Drawing.Size(23, 23); this.button7.TabIndex = 4; this.button7.Text = "↑"; this.button7.UseVisualStyleBackColor = true; this.button7.Click += new System.EventHandler(this.button7_Click); // // trackBar1 // this.trackBar1.Cursor = System.Windows.Forms.Cursors.Hand; this.trackBar1.Location = new System.Drawing.Point(6, 181); this.trackBar1.Maximum = 255; this.trackBar1.Name = "trackBar1"; this.trackBar1.Size = new System.Drawing.Size(185, 45); this.trackBar1.TabIndex = 3; this.trackBar1.TickStyle = System.Windows.Forms.TickStyle.None; this.trackBar1.Value = 125; this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll); // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(7, 47); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(219, 127); this.pictureBox1.TabIndex = 2; this.pictureBox1.TabStop = false; // // button4 // this.button4.Location = new System.Drawing.Point(125, 20); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(67, 21); this.button4.TabIndex = 1; this.button4.Text = "图库"; this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); // // button3 // this.button3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.button3.Location = new System.Drawing.Point(52, 20); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(67, 21); this.button3.TabIndex = 0; this.button3.Text = "硬盘"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // groupBox4 // this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.groupBox4.Controls.Add(this.button8); this.groupBox4.Controls.Add(this.trackBar2); this.groupBox4.Controls.Add(this.pictureBox2); this.groupBox4.Controls.Add(this.button6); this.groupBox4.Controls.Add(this.button5); this.groupBox4.Location = new System.Drawing.Point(402, 313); this.groupBox4.Name = "groupBox4"; this.groupBox4.Size = new System.Drawing.Size(231, 231); this.groupBox4.TabIndex = 3; this.groupBox4.TabStop = false; this.groupBox4.Text = "图片"; // // button8 // this.button8.Location = new System.Drawing.Point(197, 181); this.button8.Name = "button8"; this.button8.Size = new System.Drawing.Size(23, 23); this.button8.TabIndex = 4; this.button8.Text = "↑"; this.button8.UseVisualStyleBackColor = true; this.button8.Click += new System.EventHandler(this.button8_Click); // // trackBar2 // this.trackBar2.Cursor = System.Windows.Forms.Cursors.Hand; this.trackBar2.Location = new System.Drawing.Point(7, 181); this.trackBar2.Maximum = 255; this.trackBar2.Name = "trackBar2"; this.trackBar2.Size = new System.Drawing.Size(185, 45); this.trackBar2.TabIndex = 3; this.trackBar2.TickStyle = System.Windows.Forms.TickStyle.None; this.trackBar2.Value = 125; this.trackBar2.Scroll += new System.EventHandler(this.trackBar2_Scroll); // // pictureBox2 // this.pictureBox2.Location = new System.Drawing.Point(7, 47); this.pictureBox2.Name = "pictureBox2"; this.pictureBox2.Size = new System.Drawing.Size(219, 127); this.pictureBox2.TabIndex = 2; this.pictureBox2.TabStop = false; // // button6 // this.button6.Location = new System.Drawing.Point(125, 20); this.button6.Name = "button6"; this.button6.Size = new System.Drawing.Size(67, 21); this.button6.TabIndex = 1; this.button6.Text = "图库"; this.button6.UseVisualStyleBackColor = true; this.button6.Click += new System.EventHandler(this.button6_Click); // // button5 // this.button5.Location = new System.Drawing.Point(52, 20); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(67, 21); this.button5.TabIndex = 0; this.button5.Text = "硬盘"; this.button5.UseVisualStyleBackColor = true; this.button5.Click += new System.EventHandler(this.button5_Click); // // ImageComparison // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.ClientSize = new System.Drawing.Size(646, 556); this.Controls.Add(this.groupBox4); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.MinimizeBox = false; this.Name = "ImageComparison"; this.Text = "图片对比"; this.Resize += new System.EventHandler(this.ImageComparison_Resize); this.Controls.SetChildIndex(this.groupBox1, 0); this.Controls.SetChildIndex(this.groupBox2, 0); this.Controls.SetChildIndex(this.groupBox3, 0); this.Controls.SetChildIndex(this.groupBox4, 0); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit(); this.groupBox3.ResumeLayout(false); this.groupBox3.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.groupBox4.ResumeLayout(false); this.groupBox4.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.trackBar2)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit(); this.ResumeLayout(false); } /// /// 关闭按钮 /// /// /// private void button1_Click(object sender, EventArgs e) { this.Close(); } /// /// 第一张小图的图库选择按钮 /// /// /// private void button4_Click(object sender, EventArgs e) { //辅助判断点击的是第一个picturebox对应的按钮 pictureBoxNum = 1; ImagePickerDialog(sender); } /// /// 第二张小图的图库选择按钮 /// /// /// private void button6_Click(object sender, EventArgs e) { //辅助判断点击的是第二个picturebox对应的按钮 pictureBoxNum = 2; ImagePickerDialog(sender); } /// /// 弹出图库选择窗口 /// /// private void ImagePickerDialog(object sender) { if (imagePickerDialog == null) { imagePickerDialog = new InterImagePickerDialog(this.appWorkspace); imagePickerDialog.StartPosition = FormStartPosition.Manual; imagePickerDialog.Left = ((Button)sender).Left + 12 + this.Left + this.groupBox2.Width; switch (pictureBoxNum) { case 1: imagePickerDialog.Top = ((Button)sender).Bottom + 46 + this.Top + this.groupBox1.Height; break; case 2: imagePickerDialog.Top = ((Button)sender).Bottom + 50 + this.Top + this.groupBox1.Height + this.groupBox3.Height; break; } 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 + this.groupBox2.Width; switch (pictureBoxNum) { case 1: imagePickerDialog.Top = ((Button)sender).Bottom + 46 + this.Top + this.groupBox1.Height; break; case 2: imagePickerDialog.Top = ((Button)sender).Bottom + 50 + this.Top + this.groupBox1.Height + this.groupBox3.Height; break; } imagePickerDialog.ValueChanged += new System.EventHandler(this.inputMat_ValueChanged); imagePickerDialog.Show(); } } } /// /// 从图库选择待对照图片 /// /// /// private void inputMat_ValueChanged(object sender, EventArgs e) { if (sender is DocumentWorkspace) { switch (pictureBoxNum) { case 1: this.bitmap1 = ((DocumentWorkspace)sender).CompositionSurface.CreateAliasedBitmap(); if (bitmap1.Width > pictureBox1.Size.Width || bitmap1.Height > pictureBox1.Size.Height) { pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; } else { pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; } this.pictureBox1.Image = this.bitmap1; CombinePicture(); break; case 2: this.bitmap2 = ((DocumentWorkspace)sender).CompositionSurface.CreateAliasedBitmap(); if (bitmap2.Width > pictureBox2.Size.Width || bitmap2.Height > pictureBox2.Size.Height) { pictureBox2.SizeMode = PictureBoxSizeMode.Zoom; } else { pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage; } this.pictureBox2.Image = this.bitmap2; CombinePicture(); break; } } } /// /// 第一张小图的硬盘选择图片按钮 /// /// /// private void button3_Click(object sender, EventArgs e) { pictureBoxNum = 1; Bitmap newBit1 = ChooseImageFromHardDisk();//暂时接收一下,如果为空则不替换原有的原图对象 if (newBit1 != null) { this.bitmap1 = newBit1; if (bitmap1.Width > pictureBox1.Size.Width || bitmap1.Height > pictureBox1.Size.Height) { pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; } else { pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; } this.pictureBox1.Image = this.bitmap1; CombinePicture(); } } /// /// 第二张小图的硬盘选择图片按钮 /// /// /// private void button5_Click(object sender, EventArgs e) { pictureBoxNum = 2; Bitmap newBit2 = ChooseImageFromHardDisk();//暂时接收一下,如果为空则不替换原有的原图对象 if (newBit2 != null) { this.bitmap2 = newBit2; if (bitmap2.Width > pictureBox2.Size.Width || bitmap2.Height > pictureBox2.Size.Height) { pictureBox2.SizeMode = PictureBoxSizeMode.Zoom; } else { pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage; } this.pictureBox2.Image = this.bitmap2; CombinePicture(); } } /// /// 从硬盘选择图片 /// /// 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 Bitmap GetTransparencyImage(Image image, int opcity) { if (opcity < 0 || opcity > 255) return null; if (image == null) return null; float imgTransparency = float.Parse(opcity.ToString()) / 255; float[][] nArray ={ new float[] {1, 0, 0, 0, 0}, new float[] {0, 1, 0, 0, 0}, new float[] {0, 0, 1, 0, 0}, new float[] {0, 0, 0, imgTransparency, 0}, new float[] {0, 0, 0, 0, 1}}; ColorMatrix matrix = new ColorMatrix(nArray); ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Bitmap resultImage = new Bitmap(image.Width, image.Height); Graphics g = Graphics.FromImage(resultImage); g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); return resultImage; } /// /// 第一张小图的透明度滚动条 /// /// /// private void trackBar1_Scroll(object sender, EventArgs e) { if (this.pictureBox1.Image != null) { CombinePicture(); } } /// /// 第二张小图的透明度滚动条 /// /// /// private void trackBar2_Scroll(object sender, EventArgs e) { if (this.pictureBox2.Image != null) { CombinePicture(); } } /// /// 合并两个小picturebox的图片 /// private void CombinePicture() { if (this.pictureBox1.Image == null && this.pictureBox2.Image == null) return; if (this.pictureBox1.Image != null && this.pictureBox2.Image == null) { combineBitmap = GetTransparencyImage(this.pictureBox1.Image, this.trackBar1.Value); ordered = true; } if (this.pictureBox2.Image != null && this.pictureBox1.Image == null) { combineBitmap = GetTransparencyImage(this.pictureBox2.Image, this.trackBar2.Value); ordered = false; } if (this.pictureBox1.Image != null && this.pictureBox2.Image != null) { bitArray[0] = GetTransparencyImage(this.pictureBox1.Image, this.trackBar1.Value); bitArray[1] = GetTransparencyImage(this.pictureBox2.Image, this.trackBar2.Value); int[] widthArr = new int[] { bitArray[0].Width, bitArray[1].Width }; int[] heightArr = new int[] { bitArray[0].Height, bitArray[1].Height }; combineBitmap = new Bitmap(widthArr.Max(), heightArr.Max()); Graphics g = Graphics.FromImage(combineBitmap); if (ordered) { g.DrawImage(bitArray[0], new PointF(0, 0)); g.DrawImage(bitArray[1], new PointF(0, 0)); } else { g.DrawImage(bitArray[1], new PointF(0, 0)); g.DrawImage(bitArray[0], new PointF(0, 0)); } } if (combineBitmap.Width > pictureBox3.Size.Width || combineBitmap.Height > pictureBox3.Size.Height) { pictureBox3.SizeMode = PictureBoxSizeMode.Zoom; } else { pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage; } this.pictureBox3.Image = combineBitmap; } /// /// 第一张小图的层级向上按钮 /// /// /// private void button7_Click(object sender, EventArgs e) { if (this.pictureBox1.Image != null && this.pictureBox2.Image != null) { reverseOrder(); CombinePicture(); } } /// /// 第二个小图的层级向上按钮 /// /// /// private void button8_Click(object sender, EventArgs e) { if (this.pictureBox1.Image != null && this.pictureBox2.Image != null) { reverseOrder(); CombinePicture(); } } /// /// 反转图片的绘制顺序 /// private void reverseOrder() { if (ordered) { ordered = false; } else { ordered = true; } } /// /// 创建图片 /// /// /// private void button2_Click(object sender, EventArgs e) { if (combineBitmap != null) { DocumentWorkspace dw = this.appWorkspace.AddNewDocumentWorkspace(); dw.Document = Document.FromImageMat(Camera.Tools.ToMat(combineBitmap)); dw.xmlSaveModel = Startup.instance.ruleDB; dw.InitRulerInfo(); stepLength = "00" + Startup.instance.step_length; // TODO ZYH 这里的图像名称,需要走系统的配置 dw.fileText = this.Text + "-" + stepLength; this.appWorkspace.ActiveDocumentWorkspace = dw; //this.appWorkspace.ActiveDocumentWorkspace.PanelBottom.documentStrip.ShowPictures = false; this.appWorkspace.ActiveDocumentWorkspace.Refresh(); Startup.instance.step_length += 1; } } /// /// 窗体尺寸改变后重新判断合并图片的显示方式 /// /// /// private void ImageComparison_Resize(object sender, EventArgs e) { if (combineBitmap != null) { if (combineBitmap.Width > pictureBox3.Size.Width || combineBitmap.Height > pictureBox3.Size.Height) { pictureBox3.SizeMode = PictureBoxSizeMode.Zoom; } else { pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage; } this.pictureBox3.Invalidate(); } } } }