using PaintDotNet.Base.CommTool; using PaintDotNet.Base.SettingModel; using PaintDotNet.File.BatchSaveComponet; using System; using System.Drawing; using System.IO; using System.Windows.Forms; namespace PaintDotNet.File { /// /// 批量保存 /// internal class BatchSaveDialog : PdnBaseForm { private AppWorkspace appWorkspace; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private Button button4; private Panel panel1; private FolderBrowserDialog folderBrowserDialog1; private HandSaveControl handSaveControl; private AutoSaveControl autoSaveControl; /// /// 文件 - 批量保存自动配置 /// public BatchSaveAutoModel batchSaveAutoModel; /// /// 1手动 2自动 /// private int typesOf = 2; public BatchSaveDialog(AppWorkspace appWorkspace) { this.appWorkspace = appWorkspace; InitializeComponent(); InitializeLanguageText(); this.Text = PdnResources.GetString("Menu.File.BulkSave.Text"); this.button3.Focus(); this.autoSaveControl = new AutoSaveControl(this.appWorkspace); this.panel1.Controls.Add(this.autoSaveControl); } private void InitializeLanguageText() { batchSaveAutoModel = XmlSerializeHelper.DESerializer(FileOperationHelper.ReadStringFromFile(Application.StartupPath + @"\Config\Default\File\BatchSaveAuto.xml", FileMode.Open)); this.groupBox1.Text = PdnResources.GetString("Menu.Type.text"); this.button3.Text = PdnResources.GetString("Menu.file.Batchsaving.autosave.text"); this.button2.Text = PdnResources.GetString("Menu.file.Batchsaving.Savemanually.text"); this.groupBox2.Text = PdnResources.GetString("Menu.saveroute.text"); this.button4.Text = PdnResources.GetString("Menu.MeasureAction.MeasureSelect.Text"); this.button1.Text = PdnResources.GetString("Menu.File.Save.Text"); //this.textBox1.Text = batchSaveAutoModel.fileRoute; this.textBox1.Text = Startup.instance.configModel.FileOpenPath; } private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.button3 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.button4 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.panel1 = new System.Windows.Forms.Panel(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); this.groupBox1.Controls.Add(this.button3); this.groupBox1.Controls.Add(this.button2); this.groupBox1.Location = new System.Drawing.Point(13, 13); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(115, 386); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "类型"; // // button3 // this.button3.Location = new System.Drawing.Point(21, 59); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 23); this.button3.TabIndex = 0; this.button3.Text = "自动保存"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // button2 // this.button2.Location = new System.Drawing.Point(21, 29); 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); // // groupBox2 // this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox2.Controls.Add(this.button4); this.groupBox2.Controls.Add(this.button1); this.groupBox2.Controls.Add(this.textBox1); this.groupBox2.Location = new System.Drawing.Point(134, 13); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(547, 52); this.groupBox2.TabIndex = 2; this.groupBox2.TabStop = false; this.groupBox2.Text = "保存路径"; // // button4 // this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.button4.Location = new System.Drawing.Point(385, 19); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(75, 23); this.button4.TabIndex = 2; this.button4.Text = "选择"; this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); // // button1 // this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); this.button1.Location = new System.Drawing.Point(466, 19); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "保存"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.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.textBox1.Location = new System.Drawing.Point(7, 20); this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.Size = new System.Drawing.Size(372, 21); this.textBox1.TabIndex = 0; // // 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.Location = new System.Drawing.Point(135, 72); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(546, 327); this.panel1.TabIndex = 3; // // BatchSaveDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(693, 411); this.Controls.Add(this.panel1); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Name = "BatchSaveDialog"; this.Text = "批量保存"; this.Controls.SetChildIndex(this.groupBox1, 0); this.Controls.SetChildIndex(this.groupBox2, 0); this.Controls.SetChildIndex(this.panel1, 0); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); this.ResumeLayout(false); } /// /// 手动保存 /// /// /// private void button2_Click(object sender, EventArgs e) { this.typesOf = 1; this.handSaveControl = new HandSaveControl(appWorkspace); this.handSaveControl.Dock = DockStyle.Fill; this.panel1.Controls.Clear(); this.panel1.Controls.Add(this.handSaveControl); } /// /// 自动保存 /// /// /// private void button3_Click(object sender, EventArgs e) { this.typesOf = 2; this.autoSaveControl = new AutoSaveControl(this.appWorkspace); this.panel1.Controls.Clear(); this.panel1.Controls.Add(this.autoSaveControl); } /// /// 选择保存路径 /// /// /// private void button4_Click(object sender, EventArgs e) { DialogResult dialogResult = this.folderBrowserDialog1.ShowDialog(); if(dialogResult == DialogResult.OK) { this.textBox1.Text = this.folderBrowserDialog1.SelectedPath; } } /// /// 保存 /// /// /// private void button1_Click(object sender, EventArgs e) { if (this.textBox1.Text == null || this.textBox1.Text.Equals("")) { MessageBox.Show(PdnResources.GetString("Menu.Pleaseselectthesavepathfirst.text")); return; } if (!System.IO.Directory.Exists(this.textBox1.Text)) { MessageBox.Show("文件夹不存在,请检查"); return; } if (this.typesOf==1) //手动 { //判断图片名称是否为空,并且判断list内是否有重复的文件名称 todo //this.handSaveControl.textBoxes; //this.handSaveControl.comboBoxes; bool next = true; for (int i=0; i< this.handSaveControl.textBoxes.Count; i++) { int sum = this.handSaveControl.textBoxes.FindAll(a => a.Text.Equals(this.handSaveControl.textBoxes[i].Text)).Count; if (sum > 1) { next = false; } } if(!next) { MessageBox.Show(PdnResources.GetString("Menu.Thefilenandmodify.text")+"!"); return; } //是否生成配置文件 bool createConfig = this.handSaveControl.checkBox1.Checked; //保存后是否关闭图像 bool afterSaveClost = this.handSaveControl.checkBox2.Checked; //保存时是否保存网格数据 bool saveGridLine = this.handSaveControl.checkBox3.Checked; //标注及测量信息是否嵌入图像 bool implantLabelAndMeasure = this.handSaveControl.checkBox4.Checked; //相信息是否嵌入图像 bool implantPhase = this.handSaveControl.checkBox6.Checked; //是否压缩 bool compress = this.handSaveControl.checkBox5.Checked; //压缩比例 decimal compressRate = this.handSaveControl.numericUpDown1.Value; if (this.appWorkspace != null && this.appWorkspace.DocumentWorkspaces != null) { //组织包含所有名称的数组 string[] fileNames = new string[this.appWorkspace.DocumentWorkspaces.Length]; for (int i = 0; i < this.appWorkspace.DocumentWorkspaces.Length; i++) { string file = this.textBox1.Text + "\\" + this.handSaveControl.textBoxes[i].Text; file += this.handSaveControl.comboBoxes[i].SelectedValue; fileNames[i] = file; } // 1、检查是否存在重名 2、覆盖 3、移动 int actionType = 1; for (int i = this.appWorkspace.DocumentWorkspaces.Length - 1; i >= 0 ; i--) { this.appWorkspace.DocumentWorkspaces[i].GraphicsList.UnselectAll(); string fileName = this.textBox1.Text + "\\" + this.handSaveControl.textBoxes[i].Text; fileName += this.handSaveControl.comboBoxes[i].SelectedValue; //判断文件夹内是否存在重名的 if (System.IO.File.Exists(fileName)) { if (actionType == 1) { TaskButton saveTB = new TaskButton( PdnResources.GetImageResource("Icons.MenuFileSaveIcon.png").Reference, PdnResources.GetString("Menu.cover.Text"), //PdnResources.GetString("CloseWorkspaceAction.SaveButton.ActionText"), PdnResources.GetString("Menu.Replaceexistingfiles.Text") //PdnResources.GetString("CloseWorkspaceAction.SaveButton.ExplanationText") ); TaskButton dontSaveTB = new TaskButton( PdnResources.GetImageResource("Icons.MenuFileCloseIcon.png").Reference, PdnResources.GetString("Menu.Movekeeptwofiles.Text"), //PdnResources.GetString("CloseWorkspaceAction.DontSaveButton.ActionText"), PdnResources.GetString("Menu.Thenewfilewillberenamed.Text") //PdnResources.GetString("CloseWorkspaceAction.DontSaveButton.ExplanationText") ); TaskButton cancelTB = new TaskButton( PdnResources.GetImageResource("Icons.CancelIcon.png").Reference, PdnResources.GetString("CloseWorkspaceAction.CancelButton.ActionText"), PdnResources.GetString("CloseWorkspaceAction.CancelButton.ExplanationText")); string title = PdnResources.GetString("Menu.Thefilealreadyxists.Text"); string introTextFormat = PdnResources.GetString("CloseWorkspaceAction.IntroText.Format"); string introText = fileName; Image taskImage = DrawRulerHelper.ResizeImage(this.appWorkspace.DocumentWorkspaces[i].CompositionSurface.CreateAliasedBitmap(), 115, 86, Base.Enum.ThumbnailMode.Cut); Form mainForm = appWorkspace.FindForm(); if (mainForm != null) { PdnBaseForm asPDF = mainForm as PdnBaseForm; if (asPDF != null) { asPDF.RestoreWindow(); } } Icon warningIcon; ImageResource warningIconImageRes = PdnResources.GetImageResource("Icons.WarningIcon.png"); if (warningIconImageRes != null) { Image warningIconImage = warningIconImageRes.Reference; warningIcon = Utility.ImageToIcon(warningIconImage, false); } else { warningIcon = null; } //是否选中了为之后的n个冲突执行此操作 bool useChecked = false; //获取在这个文件之后还有多少重名文件 int num = FileOperationHelper.GetNumAfterThisFile(fileNames, i); TaskButton clickedTB = TaskDialog.Show( appWorkspace, warningIcon, title, taskImage, false, introText, new TaskButton[] { saveTB, dontSaveTB, cancelTB }, saveTB, cancelTB, 340, true, num, out useChecked); if (clickedTB == saveTB) //覆盖 { if(useChecked) actionType = 2; this.appWorkspace.DocumentWorkspaces[i].SaveForBatch(fileName, createConfig, afterSaveClost, saveGridLine, implantLabelAndMeasure, compress, compressRate, implantPhase); } else if (clickedTB == dontSaveTB) //移动 { if (useChecked) actionType = 3; fileName = FileOperationHelper.GetReNameFile(fileName); this.appWorkspace.DocumentWorkspaces[i].SaveForBatch(fileName, createConfig, afterSaveClost, saveGridLine, implantLabelAndMeasure, compress, compressRate, implantPhase); } } else if (actionType == 2) //覆盖 { this.appWorkspace.DocumentWorkspaces[i].SaveForBatch(fileName, createConfig, afterSaveClost, saveGridLine, implantLabelAndMeasure, compress, compressRate, implantPhase); } else if (actionType == 3) //移动 { //获取新文件名 fileName = FileOperationHelper.GetReNameFile(fileName); this.appWorkspace.DocumentWorkspaces[i].SaveForBatch(fileName, createConfig, afterSaveClost, saveGridLine, implantLabelAndMeasure, compress, compressRate, implantPhase); } } else { this.appWorkspace.DocumentWorkspaces[i].SaveForBatch(fileName, createConfig, afterSaveClost, saveGridLine, implantLabelAndMeasure, compress, compressRate, implantPhase); } } } batchSaveAutoModel.fileRoute = this.textBox1.Text; batchSaveAutoModel.createConfig = createConfig; batchSaveAutoModel.afterSaveClost = afterSaveClost; batchSaveAutoModel.saveGridLine = saveGridLine; batchSaveAutoModel.implantLabelAndMeasure = implantLabelAndMeasure; batchSaveAutoModel.implantPhase = implantPhase; batchSaveAutoModel.compress = compress; batchSaveAutoModel.compressRate = compressRate; batchSaveAutoModel.Whether = true; //另存为XML string stageModelXml = XmlSerializeHelper.XmlSerialize(batchSaveAutoModel); string filePath = Application.StartupPath + @"\Config\Default\File\BatchSaveAuto.xml"; FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create); MessageBox.Show(PdnResources.GetString("Menu.Batchsavesuccessfully.text")); this.Close(); } else if(this.typesOf == 2) //自动 { //新文件名 string fileName = this.autoSaveControl.textBox1.Text; //开始数字 decimal startNum = this.autoSaveControl.numericUpDown2.Value; //前缀 string prefix = string.IsNullOrEmpty(this.autoSaveControl.textBox3.Text) ? "" : this.autoSaveControl.textBox3.Text + "_"; //后缀 string suffix = string.IsNullOrEmpty(this.autoSaveControl.textBox4.Text) ? "" : "_" + this.autoSaveControl.textBox4.Text; //图片格式 string format = this.autoSaveControl.comboBox1.Text; //是否生成配置文件 bool createConfig = this.autoSaveControl.checkBox1.Checked; //保存后是否关闭图像 bool afterSaveClost = this.autoSaveControl.checkBox2.Checked; //保存时是否保存网格数据 bool saveGridLine = this.autoSaveControl.checkBox3.Checked; //标注及测量信息是否嵌入图像 bool implantLabelAndMeasure = this.autoSaveControl.checkBox4.Checked; //相信息是否嵌入图像 bool implantPhase = this.autoSaveControl.checkBox6.Checked; //是否压缩 bool compress = this.autoSaveControl.checkBox5.Checked; //压缩比例 decimal compressRate = this.autoSaveControl.numericUpDown1.Value; if (string.IsNullOrEmpty(fileName)) { //MessageBox.Show("请输入新文件名"); //return; fileName = PdnResources.GetString("Menu.File.BulkSave.Text") + DateTime.Now.ToString("HHmmss"); } //if (string.IsNullOrEmpty(prefix)) //{ // MessageBox.Show("请输入文件前缀名"); // return; //} //if (string.IsNullOrEmpty(suffix)) //{ // MessageBox.Show("请输入文件后缀"); // return; //} if (this.appWorkspace != null && this.appWorkspace.DocumentWorkspaces != null) { //组织包含所有名称的数组 string[] fileNames = new string[this.appWorkspace.DocumentWorkspaces.Length]; for (int i = 0; i < this.appWorkspace.DocumentWorkspaces.Length; i++) { this.appWorkspace.DocumentWorkspaces[i].GraphicsList.UnselectAll(); string file = this.textBox1.Text + "\\" + prefix + fileName + "_" + (((int)startNum) + i) + suffix; file += this.autoSaveControl.comboBox1.SelectedValue; fileNames[i] = file; } // 1、检查是否存在重名 2、覆盖 3、移动 int actionType = 1; for (int i = this.appWorkspace.DocumentWorkspaces.Length - 1; i >= 0 ; i--) { string fileName1 = this.textBox1.Text +"\\"+ prefix + fileName + "_" + (((int)startNum) + i) + suffix; fileName1 += this.autoSaveControl.comboBox1.SelectedValue; //判断文件夹内是否存在重名的 if (System.IO.File.Exists(fileName1)) { if (actionType == 1) { TaskButton saveTB = new TaskButton( PdnResources.GetImageResource("Icons.MenuFileSaveIcon.png").Reference, PdnResources.GetString("Menu.cover.Text"), PdnResources.GetString("Menu.Replaceexistingfiles.Text") ); TaskButton dontSaveTB = new TaskButton( PdnResources.GetImageResource("Icons.MenuFileCloseIcon.png").Reference, PdnResources.GetString("Menu.Movekeeptwofiles.Text"), PdnResources.GetString("Menu.Thenewfilewillberenamed.Text") ); TaskButton cancelTB = new TaskButton( PdnResources.GetImageResource("Icons.CancelIcon.png").Reference, PdnResources.GetString("CloseWorkspaceAction.CancelButton.ActionText"), PdnResources.GetString("CloseWorkspaceAction.CancelButton.ExplanationText")); string title = PdnResources.GetString("Menu.Thefilealreadyxists.Text"); string introTextFormat = PdnResources.GetString("CloseWorkspaceAction.IntroText.Format"); string introText = fileName; Image taskImage = DrawRulerHelper.ResizeImage(this.appWorkspace.DocumentWorkspaces[i].CompositionSurface.Thumbnail/**CreateAliasedBitmap()**/, 115, 86, Base.Enum.ThumbnailMode.Cut); Form mainForm = appWorkspace.FindForm(); if (mainForm != null) { PdnBaseForm asPDF = mainForm as PdnBaseForm; if (asPDF != null) { asPDF.RestoreWindow(); } } Icon warningIcon; ImageResource warningIconImageRes = PdnResources.GetImageResource("Icons.WarningIcon.png"); if (warningIconImageRes != null) { Image warningIconImage = warningIconImageRes.Reference; warningIcon = Utility.ImageToIcon(warningIconImage, false); } else { warningIcon = null; } //是否选中了为之后的n个冲突执行此操作 bool useChecked = false; //获取在这个文件之后还有多少重名文件 int num = FileOperationHelper.GetNumAfterThisFile(fileNames, i); TaskButton clickedTB = TaskDialog.Show( appWorkspace, warningIcon, title, taskImage, false, introText, new TaskButton[] { saveTB, dontSaveTB, cancelTB }, saveTB, cancelTB, 340, true, num, out useChecked); if (clickedTB == saveTB) //覆盖 { if (useChecked) actionType = 2; this.appWorkspace.DocumentWorkspaces[i].SaveForBatch(fileName1, createConfig, afterSaveClost, saveGridLine, implantLabelAndMeasure, compress, compressRate, implantPhase); } else if (clickedTB == dontSaveTB) //移动 { if (useChecked) actionType = 3; fileName1 = FileOperationHelper.GetReNameFile(fileName1); this.appWorkspace.DocumentWorkspaces[i].SaveForBatch(fileName1, createConfig, afterSaveClost, saveGridLine, implantLabelAndMeasure, compress, compressRate, implantPhase); } } else if (actionType == 2) //覆盖 { this.appWorkspace.DocumentWorkspaces[i].SaveForBatch(fileName1, createConfig, afterSaveClost, saveGridLine, implantLabelAndMeasure, compress, compressRate, implantPhase); } else if (actionType == 3) //移动 { //获取新文件名 fileName1 = FileOperationHelper.GetReNameFile(fileName1); this.appWorkspace.DocumentWorkspaces[i].SaveForBatch(fileName1, createConfig, afterSaveClost, saveGridLine, implantLabelAndMeasure, compress, compressRate, implantPhase); } } else { this.appWorkspace.DocumentWorkspaces[i].SaveForBatch(fileName1, createConfig, afterSaveClost, saveGridLine, implantLabelAndMeasure, compress, compressRate, implantPhase); } } } batchSaveAutoModel.fileRoute = this.textBox1.Text; batchSaveAutoModel.fileName = fileName; batchSaveAutoModel.startNum = startNum + this.appWorkspace.DocumentWorkspaces.Length; batchSaveAutoModel.prefix = prefix == "" ? prefix : prefix.Substring(0,prefix.Length-1); batchSaveAutoModel.suffix = suffix == "" ? suffix : suffix.Substring(1,suffix.Length - 1); batchSaveAutoModel.format = format; batchSaveAutoModel.createConfig = createConfig; batchSaveAutoModel.afterSaveClost = afterSaveClost; batchSaveAutoModel.saveGridLine = saveGridLine; batchSaveAutoModel.implantLabelAndMeasure = implantLabelAndMeasure; batchSaveAutoModel.implantPhase = implantPhase; batchSaveAutoModel.compress = compress; batchSaveAutoModel.compressRate = compressRate; batchSaveAutoModel.Whether = true; //另存为XML string stageModelXml = XmlSerializeHelper.XmlSerialize(batchSaveAutoModel); string filePath = Application.StartupPath + @"\Config\Default\File\BatchSaveAuto.xml"; FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create); MessageBox.Show(PdnResources.GetString("Menu.Batchsavesuccessfully.text")); this.Close(); } appWorkspace.Widgets.ImageIndexDialog.InitData(); } } }