图像标记_3_11
还有窗口自适应大小未完成
1 using System; 2 using System.IO; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Data; 6 using System.Drawing; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 using System.Xml; 12 13 namespace Image_mark 14 { 15 16 public partial class Form1 : Form 17 { 18 // 我设置的全局变量 19 private List<string> all_picture_path = new List<string>(); 20 private List<string> all_picture_name = new List<string>(); 21 private string current_picture_path; 22 private int current_picture_index = 0; // 从1开始 23 private int old_picture_index = 0; 24 25 private List<string> all_type_picture_path = new List<string>(); 26 private string current_type_picture_path = ""; 27 28 private List<string> all_join_picture_path = new List<string>(); 29 30 private ImageList join_image_list = new ImageList(); 31 private List<string> join_image_list_name = new List<string>(); 32 private List<string> join_image_list_path = new List<string>(); 33 private const int show_join_image_num = 4; 34 private Button[] show_join_image_button = new Button[show_join_image_num]; 35 36 private string rewrite_picture = ""; 37 private bool rewrite_mode = false; 38 private string BackgroundImagePath_rewrite = ""; 39 40 private int function_select = 0; 41 private const int BROW = 1; 42 private const int EYE = 2; 43 private const int NOSE = 3; 44 private const int MOUTH = 4; 45 46 private int xml_node_num = 0; 47 48 private string brow_tpye_path = ".\\brow\\"; 49 private string eye_tpye_path = ".\\eye\\"; 50 private string nose_tpye_path = ".\\nose\\"; 51 private string mouth_tpye_path = ".\\mouth\\"; 52 53 private Button[] type_button = new Button[20]; 54 private const int type_button_WIDTH = 100; 55 private const int type_button_HEIGHT = 50; 56 private const int type_button_1_location_x = 450; 57 private const int type_button_1_location_y = 70; 58 59 public static Keys[] key_type = new Keys[9]; 60 61 private Auto_size_form auto_size_form = new Auto_size_form(); 62 63 64 #region 构造函数,初始化 65 public Form1() 66 { 67 InitializeComponent(); 68 69 Init(); 70 71 } 72 #endregion 73 74 private void Init() 75 {// 一些初始化 76 77 key_type[0] = Keys.NumPad7; 78 key_type[1] = Keys.NumPad8; 79 key_type[2] = Keys.NumPad9; 80 key_type[3] = Keys.NumPad4; 81 key_type[4] = Keys.NumPad5; 82 key_type[5] = Keys.NumPad6; 83 key_type[6] = Keys.NumPad1; 84 key_type[7] = Keys.NumPad2; 85 key_type[8] = Keys.NumPad3; 86 87 Set_show_join_image_button(); 88 89 Using_select_picture_button(false); 90 91 } 92 93 #region 设置4个回看按钮 94 private void Set_show_join_image_button() 95 { 96 int x = 10, y = 10; 97 int per_x_add = (this.groupBox2.Width - 10) / 4; 98 for (int i = 0; i < show_join_image_num; i++) 99 { 100 show_join_image_button[i] = new Button(); 101 show_join_image_button[i].Location = new Point(x, y); 102 show_join_image_button[i].Size = new Size(per_x_add - 10, this.groupBox2.Height - 20); 103 show_join_image_button[i].BackgroundImageLayout = ImageLayout.Stretch; 104 show_join_image_button[i].Name = ("joinButton" + i.ToString()); 105 show_join_image_button[i].Text = ""; 106 show_join_image_button[i].Click += new System.EventHandler(this.join_image_button_Click); 107 108 this.groupBox2.Controls.Add(show_join_image_button[i]); 109 110 x += per_x_add; 111 } 112 113 } 114 #endregion 115 116 #region 根据标记功能加载相应的 XML 文件 设置按钮 117 private void Load_xml_set_button() 118 { 119 XmlDocument xmlDoc = new XmlDocument(); 120 XmlReaderSettings settings = new XmlReaderSettings(); 121 settings.IgnoreComments = true; 122 123 OpenFileDialog openFileDialog1 = new OpenFileDialog(); 124 125 openFileDialog1.InitialDirectory = "E:\\"; 126 openFileDialog1.Filter = "xml files (*.xml)|*.xml"; 127 openFileDialog1.RestoreDirectory = true; 128 129 // 先把之前的功能按钮释放 130 if(xml_node_num != 0) 131 { 132 for (int i = 0; i < xml_node_num; i++) 133 type_button[i].Dispose(); 134 } 135 136 // 移除之前的图片路径 137 all_type_picture_path.Clear(); 138 139 string xml_type = ""; 140 switch(function_select) 141 { 142 case BROW: 143 { 144 xml_type = "brow"; 145 current_type_picture_path = brow_tpye_path; 146 } break; 147 case EYE: 148 { 149 xml_type = "eye"; 150 current_type_picture_path = eye_tpye_path; 151 } break; 152 case NOSE: 153 { 154 xml_type = "nose"; 155 current_type_picture_path = nose_tpye_path; 156 } break; 157 case MOUTH: 158 { 159 xml_type = "mouth"; 160 current_type_picture_path = mouth_tpye_path; 161 } break; 162 } 163 164 if ((openFileDialog1.ShowDialog() == DialogResult.OK) && (openFileDialog1.FileName != "")) 165 { 166 string xml_path = openFileDialog1.FileName; 167 168 XmlReader reader = XmlReader.Create(xml_path, settings); 169 xmlDoc.Load(reader); 170 171 if (xmlDoc.DocumentElement.ChildNodes.Count <= 0) 172 { 173 MessageBox.Show("该 xml 无配置信息,请重新选择"); 174 return; 175 } 176 if (xmlDoc.DocumentElement.ChildNodes[0].Name != xml_type) 177 { 178 MessageBox.Show("该 xml 与标记功能不对应,请重新选择"); 179 return; 180 } 181 } 182 else 183 { 184 return; 185 } 186 187 // 标记类型样本数量 188 xml_node_num = xmlDoc.DocumentElement.ChildNodes.Count; 189 190 for (int i = 0; i < xml_node_num; i++) 191 { 192 switch (function_select) 193 { 194 case BROW: 195 { 196 all_type_picture_path.Add( brow_tpye_path + 197 ((XmlElement)xmlDoc.DocumentElement.ChildNodes[i]).GetAttribute("type").ToString() + 198 ".jpg"); 199 200 } 201 break; 202 case EYE: 203 { 204 all_type_picture_path.Add( eye_tpye_path + 205 ((XmlElement)xmlDoc.DocumentElement.ChildNodes[i]).GetAttribute("type").ToString() + 206 ".jpg"); 207 } 208 break; 209 case NOSE: 210 { 211 all_type_picture_path.Add( nose_tpye_path + 212 ((XmlElement)xmlDoc.DocumentElement.ChildNodes[i]).GetAttribute("type").ToString() + 213 ".jpg"); 214 } 215 break; 216 case MOUTH: 217 { 218 all_type_picture_path.Add( mouth_tpye_path + 219 ((XmlElement)xmlDoc.DocumentElement.ChildNodes[i]).GetAttribute("type").ToString() + 220 ".jpg"); 221 } 222 break; 223 default: break; 224 } 225 } 226 227 int x = 10, y = 20; 228 int per_x_add = (this.groupBox1.Width - 10) / 3; 229 int per_y_add = (this.groupBox1.Height - 20) / 3; 230 int x_button_num = 0; 231 232 for (int i = 0; i < xml_node_num; i++) 233 { 234 type_button[i] = new Button(); 235 type_button[i].Location = new Point(x, y); 236 type_button[i].Size = new Size(per_x_add - 10, per_y_add - 10); 237 type_button[i].BackgroundImageLayout = ImageLayout.Stretch; 238 type_button[i].Name = ((XmlElement)xmlDoc.DocumentElement.ChildNodes[i]).GetAttribute("type").ToString(); 239 //type_button[i].Text = ((XmlElement)xmlDoc.DocumentElement.ChildNodes[i]).GetAttribute("type").ToString(); 240 type_button[i].BackgroundImage = Image.FromFile(all_type_picture_path[i]); 241 242 type_button[i].Click += new System.EventHandler(this.type_button_Click); 243 244 this.groupBox1.Controls.Add(type_button[i]); 245 246 x += per_x_add; 247 ++x_button_num; 248 if (x_button_num == 3) 249 { 250 x = 10; 251 x_button_num = 0; 252 y += per_y_add; 253 } 254 255 } 256 257 } 258 #endregion 259 260 261 #region 标记选项按钮消息 262 private void type_button_Click(object sender, EventArgs e) 263 { 264 if (Write_output_xml(sender) == 1) 265 { 266 for (int i = 0; i < xml_node_num; i++) 267 { 268 int len = all_type_picture_path[i].Length - current_type_picture_path.Length; 269 if (all_type_picture_path[i].Substring(all_type_picture_path[i].Length - len, len) == 270 (((Button)sender).Name + ".jpg")) 271 { 272 // 拼接图片 273 Join_image(sender, all_picture_path[current_picture_index - 1], all_type_picture_path[i]); 274 275 Show_join_image_in_button(); 276 277 } 278 279 } 280 } 281 // 自动开始下一张 282 Next_picture(); 283 284 } 285 #endregion 286 287 #region 写图片信息到 xml 输出文件 288 // 返回 0 表示已经标记过 289 private int Write_output_xml(object sender) 290 { 291 if (old_picture_index != current_picture_index) 292 rewrite_mode = false; 293 if(rewrite_mode) 294 { 295 if (File.Exists(BackgroundImagePath_rewrite)) 296 { 297 int index = BackgroundImagePath_rewrite.IndexOf(".jpg"); 298 string str = BackgroundImagePath_rewrite.Substring(0, index + 4); 299 if ( BackgroundImagePath_rewrite == (str + ((Button)sender).Name + ".jpg") ) 300 { 301 //MessageBox.Show(BackgroundImagePath_rewrite + "修改的类型与之前一样,直接返回"); 302 rewrite_mode = false; 303 return 0; 304 } 305 if (File.Exists(str + ((Button)sender).Name + ".jpg")) 306 { 307 //MessageBox.Show(BackgroundImagePath_rewrite + "修改后的类型也存在,直接返回"); 308 rewrite_mode = false; 309 return 0; 310 } 311 } 312 rewrite_mode = false; 313 Delete_one_picture_xml(); 314 315 } 316 317 if (!File.Exists("output.xml")) 318 { 319 XmlTextWriter output_xml = new XmlTextWriter("output.xml", Encoding.UTF8); 320 output_xml.Formatting = Formatting.Indented; 321 output_xml.WriteStartDocument(false); 322 323 output_xml.WriteStartElement("图片标记信息"); 324 325 output_xml.WriteComment("这是使用软件标记后输出的图片信息"); 326 327 output_xml.WriteEndElement(); 328 329 output_xml.Flush(); 330 output_xml.Close(); 331 } 332 333 XmlDocument xmldoc = new XmlDocument(); 334 xmldoc.Load("output.xml"); 335 336 XmlNode root = xmldoc.SelectSingleNode("图片标记信息"); 337 338 switch(function_select) 339 { 340 case BROW: 341 { 342 if(if_mark_the_picture(root, function_select)) 343 { 344 MessageBox.Show("该图片此类型已经标记过"); 345 return 0; 346 } 347 348 // 查找xml中是否有当前图片信息,有的话不要创建节点,直接添加 349 XmlNodeList nodelist = root.ChildNodes; 350 if (nodelist.Count > 0) 351 { 352 foreach (XmlNode node1 in nodelist) 353 { 354 if (node1.Name == all_picture_name[current_picture_index - 1]) 355 { 356 node1.Attributes["brow_type"].Value = ((Button)sender).Name; 357 358 xmldoc.Save("output.xml"); 359 360 return 1; 361 } 362 } 363 } 364 365 //创建一个<Node>节点 366 XmlElement xe1 = xmldoc.CreateElement(all_picture_name[current_picture_index - 1]); 367 368 //设置该节点 type 属性 369 xe1.SetAttribute("brow_type", ((Button)sender).Name); 370 371 if (xe1.Attributes["eye_type"] == null) 372 xe1.SetAttribute("eye_type", ""); 373 if (xe1.Attributes["nose_type"] == null) 374 xe1.SetAttribute("nose_type", ""); 375 if (xe1.Attributes["mouth_type"] == null) 376 xe1.SetAttribute("mouth_type", ""); 377 378 //添加到<图片标记信息>节点中 379 root.AppendChild(xe1); 380 381 xmldoc.Save("output.xml"); 382 383 } 384 break; 385 case EYE: 386 { 387 if (if_mark_the_picture(root, function_select)) 388 { 389 MessageBox.Show("该图片此类型已经标记过"); 390 return 0; 391 } 392 393 // 查找xml中是否有当前图片信息,有的话不要创建节点,直接添加 394 XmlNodeList nodelist = root.ChildNodes; 395 if (nodelist.Count > 0) 396 { 397 foreach (XmlNode node1 in nodelist) 398 { 399 if (node1.Name == all_picture_name[current_picture_index - 1]) 400 { 401 node1.Attributes["eye_type"].Value = ((Button)sender).Name; 402 403 xmldoc.Save("output.xml"); 404 405 return 1; 406 } 407 } 408 } 409 410 //创建一个<Node>节点 411 XmlElement xe1 = xmldoc.CreateElement(all_picture_name[current_picture_index - 1]); 412 413 //设置该节点 type 属性 414 if (xe1.Attributes["brow_type"] == null) 415 xe1.SetAttribute("brow_type", ""); 416 417 xe1.SetAttribute("eye_type", ((Button)sender).Name); 418 419 if (xe1.Attributes["nose_type"] == null) 420 xe1.SetAttribute("nose_type", ""); 421 if (xe1.Attributes["mouth_type"] == null) 422 xe1.SetAttribute("mouth_type", ""); 423 424 //添加到<图片标记信息>节点中 425 root.AppendChild(xe1); 426 427 xmldoc.Save("output.xml"); 428 429 } 430 break; 431 case NOSE: 432 { 433 if (if_mark_the_picture(root, function_select)) 434 { 435 MessageBox.Show("该图片此类型已经标记过"); 436 return 0; 437 } 438 439 // 查找xml中是否有当前图片信息,有的话不要创建节点,直接添加 440 XmlNodeList nodelist = root.ChildNodes; 441 if (nodelist.Count > 0) 442 { 443 foreach (XmlNode node1 in nodelist) 444 { 445 if (node1.Name == all_picture_name[current_picture_index - 1]) 446 { 447 node1.Attributes["nose_type"].Value = ((Button)sender).Name; 448 449 xmldoc.Save("output.xml"); 450 451 return 1; 452 } 453 } 454 } 455 456 //创建一个<Node>节点 457 XmlElement xe1 = xmldoc.CreateElement(all_picture_name[current_picture_index - 1]); 458 459 //设置该节点 type 属性 460 461 if (xe1.Attributes["brow_type"] == null) 462 xe1.SetAttribute("brow_type", ""); 463 if (xe1.Attributes["eye_type"] == null) 464 xe1.SetAttribute("eye_type", ""); 465 466 xe1.SetAttribute("nose_type", ((Button)sender).Name); 467 468 if (xe1.Attributes["mouth_type"] == null) 469 xe1.SetAttribute("mouth_type", ""); 470 471 //添加到<图片标记信息>节点中 472 root.AppendChild(xe1); 473 474 xmldoc.Save("output.xml"); 475 476 } 477 break; 478 case MOUTH: 479 { 480 if (if_mark_the_picture(root, function_select)) 481 { 482 MessageBox.Show("该图片此类型已经标记过"); 483 return 0; 484 } 485 486 // 查找xml中是否有当前图片信息,有的话不要创建节点,直接添加 487 XmlNodeList nodelist = root.ChildNodes; 488 if (nodelist.Count > 0) 489 { 490 foreach (XmlNode node1 in nodelist) 491 { 492 if (node1.Name == all_picture_name[current_picture_index - 1]) 493 { 494 node1.Attributes["mouth_type"].Value = ((Button)sender).Name; 495 496 xmldoc.Save("output.xml"); 497 498 return 1; 499 } 500 } 501 } 502 503 //创建一个<Node>节点 504 XmlElement xe1 = xmldoc.CreateElement(all_picture_name[current_picture_index - 1]); 505 506 //设置该节点 type 属性 507 508 if (xe1.Attributes["brow_type"] == null) 509 xe1.SetAttribute("brow_type", ""); 510 if (xe1.Attributes["eye_type"] == null) 511 xe1.SetAttribute("eye_type", ""); 512 if (xe1.Attributes["nose_type"] == null) 513 xe1.SetAttribute("nose_type", ""); 514 515 xe1.SetAttribute("mouth_type", ((Button)sender).Name); 516 517 //添加到<图片标记信息>节点中 518 root.AppendChild(xe1); 519 520 xmldoc.Save("output.xml"); 521 522 } 523 break; 524 default: break; 525 } 526 return 1; 527 } 528 #endregion 529 530 #region 此图片该功能类型是否标记过 标记过的返回 true 531 private bool if_mark_the_picture(XmlNode root, int function) 532 { 533 //得到顶层节点列表 534 //XmlNodeList topM = xmld.DocumentElement.ChildNodes; 535 536 XmlNodeList nodelist = root.ChildNodes; 537 if (nodelist.Count > 0) 538 { 539 foreach(XmlNode node1 in nodelist) 540 { 541 if(node1.Name == all_picture_name[current_picture_index - 1]) 542 { 543 switch (function) 544 { 545 case BROW: 546 { 547 if (node1.Attributes["brow_type"].Value != "") 548 return true; 549 } 550 break; 551 case EYE: 552 { 553 if (node1.Attributes["eye_type"].Value != "") 554 return true; 555 } 556 break; 557 case NOSE: 558 { 559 if (node1.Attributes["nose_type"].Value != "") 560 return true; 561 } 562 break; 563 case MOUTH: 564 { 565 if (node1.Attributes["mouth_type"].Value != "") 566 return true; 567 } 568 break; 569 default: break; 570 571 } 572 } 573 } 574 } 575 return false; 576 577 } 578 #endregion 579 580 #region 图片 拼接 参数为图片绝对路径 581 private void Join_image(object sender, string picture_name1, string picture_name2) 582 { 583 int total_width = 0; 584 int total_height = 0; 585 int next_width = 0; 586 587 total_width = Image.FromFile(picture_name1).Width + Image.FromFile(picture_name2).Width; 588 // 选高度最高的 589 total_height = Image.FromFile(picture_name1).Height > Image.FromFile(picture_name2).Height ? 590 Image.FromFile(picture_name1).Height : Image.FromFile(picture_name2).Height; 591 592 next_width = Image.FromFile(picture_name1).Width; 593 594 Bitmap new_image = new Bitmap(total_width, total_height); 595 Graphics graph = Graphics.FromImage(new_image); 596 //初始化这个大图 597 graph.DrawImage(new_image, total_width, total_height); 598 599 graph.DrawImage(Image.FromFile(picture_name1), 0, 0, Image.FromFile(picture_name1).Width, Image.FromFile(picture_name1).Height); 600 graph.DrawImage(Image.FromFile(picture_name2), next_width, 0, Image.FromFile(picture_name2).Width, Image.FromFile(picture_name2).Height); 601 602 if (!Directory.Exists("join_image")) 603 { 604 Directory.CreateDirectory("join_image"); 605 } 606 607 // 源图片绝对路径 - 路径 = 源图片.jpg 608 // 要减去源图片名前的 \\ 字符 609 string new_image_name = "join_image\\" + picture_name1.Substring( (picture_name1.Length - 610 (picture_name1.Length - current_picture_path.Length - 1)), picture_name1.Length - current_picture_path.Length - 1) + 611 picture_name2.Substring( (picture_name2.Length - 612 (picture_name2.Length - current_type_picture_path.Length)), (picture_name2.Length - current_type_picture_path.Length)); 613 614 // 保存拼接的图片 615 new_image.Save(new_image_name); 616 617 // 同时保存拼接图片绝对路径 618 join_image_list_path.Add(new_image_name); 619 join_image_list_name.Add(((Button)sender).Name); 620 621 } 622 #endregion 623 624 #region 在按钮上显示拼接后图片 625 private void Show_join_image_in_button() 626 { 627 if (join_image_list_path.Count <= 0) 628 return; 629 630 else if (join_image_list_path.Count < 4) 631 { 632 int index = 0; 633 for (int i = join_image_list_path.Count - 1; i >= 0; i--) 634 { 635 show_join_image_button[index++].BackgroundImage = Image.FromFile(join_image_list_path[i]); 636 --index; 637 show_join_image_button[index++].Text = join_image_list_path[i]; 638 } 639 } 640 else 641 { 642 int index = 0; 643 for (int i = join_image_list_path.Count - 1; i >= join_image_list_path.Count - 4; i--) 644 { 645 show_join_image_button[index++].BackgroundImage = Image.FromFile(join_image_list_path[i]); 646 --index; 647 show_join_image_button[index++].Text = join_image_list_path[i]; 648 } 649 650 } 651 652 } 653 #endregion 654 655 #region 选择某张最近标记的图片重新标记 656 private void join_image_button_Click(object sender, EventArgs e) 657 { 658 BackgroundImagePath_rewrite = ((Button)sender).Text; 659 660 if(BackgroundImagePath_rewrite != "") 661 { 662 // 切换到重写模式 (在选定某项类型后,先删除,在写入,与正常写入相比多了删除) 663 rewrite_mode = true; 664 665 // 得到当前源图片名 666 Get_rewrite_picture_name(BackgroundImagePath_rewrite); 667 668 Find_picture_and_show(rewrite_picture); 669 old_picture_index = current_picture_index; 670 671 } 672 673 } 674 #endregion 675 676 #region 根据按钮Text 分割字符串得到需重写的图片名 677 private void Get_rewrite_picture_name(string str) 678 { 679 // 去除 join_image\\ 字符串, 680 str = str.Substring(11, str.Length - 11); 681 682 // 查找字串中指定字符或字串首次出现的位置,返首索引值 683 int index = str.IndexOf(".jpg"); 684 685 rewrite_picture = str.Substring(0, index + 4); 686 //MessageBox.Show(rewrite_picture); 687 688 } 689 #endregion 690 691 #region 查找图片 692 private void Find_picture_and_show(string strName) 693 { 694 for(int i=0; i<all_picture_path.Count; i++) 695 { 696 if (all_picture_path[i] == (current_picture_path + "\\" + strName)) 697 { 698 current_picture_index = i + 1; 699 700 pictureBox1.Load(current_picture_path + "\\" + strName); 701 label1.Text = "正在修改标记过的图片"; 702 break; 703 } 704 705 } 706 } 707 #endregion 708 709 710 #region 删除每张图片的标记类型 711 private void Delete_one_picture_xml() 712 { 713 XmlDocument xmldoc = new XmlDocument(); 714 xmldoc.Load("output.xml"); 715 716 XmlNode root = xmldoc.SelectSingleNode("图片标记信息"); 717 718 switch (function_select) 719 { 720 case BROW: 721 { 722 XmlNodeList nodelist = root.ChildNodes; 723 if (nodelist.Count > 0) 724 { 725 foreach (XmlNode node1 in nodelist) 726 { 727 if (node1.Name == rewrite_picture) 728 { 729 node1.Attributes["brow_type"].Value = ""; 730 731 xmldoc.Save("output.xml"); 732 733 } 734 } 735 } 736 } 737 break; 738 case EYE: 739 { 740 XmlNodeList nodelist = root.ChildNodes; 741 if (nodelist.Count > 0) 742 { 743 foreach (XmlNode node1 in nodelist) 744 { 745 if (node1.Name == rewrite_picture) 746 { 747 node1.Attributes["eye_type"].Value = ""; 748 749 xmldoc.Save("output.xml"); 750 751 } 752 } 753 } 754 } 755 break; 756 case NOSE: 757 { 758 XmlNodeList nodelist = root.ChildNodes; 759 if (nodelist.Count > 0) 760 { 761 foreach (XmlNode node1 in nodelist) 762 { 763 if (node1.Name == rewrite_picture) 764 { 765 node1.Attributes["nose_type"].Value = ""; 766 767 xmldoc.Save("output.xml"); 768 769 } 770 } 771 } 772 } 773 break; 774 case MOUTH: 775 { 776 XmlNodeList nodelist = root.ChildNodes; 777 if (nodelist.Count > 0) 778 { 779 foreach (XmlNode node1 in nodelist) 780 { 781 if (node1.Name == rewrite_picture) 782 { 783 node1.Attributes["mouth_type"].Value = ""; 784 785 xmldoc.Save("output.xml"); 786 787 } 788 } 789 } 790 } 791 break; 792 default: break; 793 } 794 } 795 #endregion 796 797 798 #region 设置图片路径 799 private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) 800 { 801 current_picture_path = ""; 802 all_picture_path.Clear(); 803 all_picture_name.Clear(); 804 805 FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog(); 806 FolderBrowserDialog1.Description = "请选择图片路径:"; 807 808 if (FolderBrowserDialog1.ShowDialog() == DialogResult.OK) 809 { 810 current_picture_path = FolderBrowserDialog1.SelectedPath; 811 // E:\picture 812 //MessageBox.Show(current_picture_path); 813 814 DirectoryInfo TheFolder = new DirectoryInfo(current_picture_path); 815 List<string> all = new List<string>(); 816 817 foreach (FileInfo NextFile in TheFolder.GetFiles("*.jpg")) 818 { 819 all.Add(NextFile.Name); 820 821 } 822 //foreach (FileInfo NextFile in TheFolder.GetFiles("*.bmp")) 823 //{ 824 // all.Add(NextFile.Name); 825 826 //} 827 //foreach (FileInfo NextFile in TheFolder.GetFiles("*.png")) 828 //{ 829 // all.Add(NextFile.Name); 830 831 //} 832 833 if(all.Count > 0) 834 { 835 for (int i = 0; i < all.Count; i++) 836 { 837 all_picture_path.Add(current_picture_path + "\\" + all[i]); 838 all_picture_name.Add(all[i]); 839 } 840 841 current_picture_index = 1; 842 843 // 显示图片 844 pictureBox1.Load(all_picture_path[current_picture_index - 1]); 845 label1.Text = " 第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index-1]; 846 847 Using_select_picture_button(true); 848 849 } 850 else 851 { 852 MessageBox.Show("此路径下无图片,重新设置"); 853 } 854 855 } 856 else 857 { 858 return; 859 } 860 } 861 #endregion 862 863 #region 上一张或下一张图片 864 private void buttonLeft_Click(object sender, EventArgs e) 865 { 866 rewrite_mode = false; 867 868 if (current_picture_index > 1) 869 { 870 --current_picture_index; 871 } 872 else 873 current_picture_index = all_picture_path.Count; 874 875 pictureBox1.Load(all_picture_path[current_picture_index - 1]); 876 877 switch (function_select) 878 { 879 case BROW: 880 { 881 label1.Text = "正在 标记眉毛...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 882 } 883 break; 884 case EYE: 885 { 886 label1.Text = "正在 标记眼睛...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 887 } 888 break; 889 case NOSE: 890 { 891 label1.Text = "正在 标记鼻子...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 892 } 893 break; 894 case MOUTH: 895 { 896 label1.Text = "正在 标记嘴巴...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 897 } 898 break; 899 default: 900 label1.Text = " 第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 901 break; 902 } 903 904 } 905 906 private void buttonRight_Click(object sender, EventArgs e) 907 { 908 rewrite_mode = false; 909 910 if (current_picture_index < all_picture_path.Count ) 911 { 912 ++current_picture_index; 913 } 914 else 915 current_picture_index = 1; 916 917 pictureBox1.Load(all_picture_path[current_picture_index - 1]); 918 919 switch(function_select) 920 { 921 case BROW: 922 { 923 label1.Text = "正在 标记眉毛...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 924 } 925 break; 926 case EYE: 927 { 928 label1.Text = "正在 标记眼睛...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 929 } 930 break; 931 case NOSE: 932 { 933 label1.Text = "正在 标记鼻子...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 934 } 935 break; 936 case MOUTH: 937 { 938 label1.Text = "正在 标记嘴巴...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 939 } 940 break; 941 default: 942 label1.Text = " 第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 943 break; 944 } 945 946 } 947 #endregion 948 949 #region 上一张图片 950 951 private void Back_picture() 952 { 953 rewrite_mode = false; 954 955 if (current_picture_index > 1) 956 { 957 --current_picture_index; 958 } 959 else 960 current_picture_index = all_picture_path.Count; 961 962 pictureBox1.Load(all_picture_path[current_picture_index - 1]); 963 964 switch (function_select) 965 { 966 case BROW: 967 { 968 label1.Text = "正在 标记眉毛...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 969 } 970 break; 971 case EYE: 972 { 973 label1.Text = "正在 标记眼睛...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 974 } 975 break; 976 case NOSE: 977 { 978 label1.Text = "正在 标记鼻子...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 979 } 980 break; 981 case MOUTH: 982 { 983 label1.Text = "正在 标记嘴巴...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 984 } 985 break; 986 default: break; 987 } 988 989 } 990 #endregion 991 992 #region 下一张图片 993 private void Next_picture() 994 { 995 rewrite_mode = false; 996 997 if (current_picture_index < all_picture_path.Count ) 998 { 999 ++current_picture_index; 1000 } 1001 else 1002 current_picture_index = 1; 1003 1004 pictureBox1.Load(all_picture_path[current_picture_index - 1]); 1005 1006 switch(function_select) 1007 { 1008 case BROW: 1009 { 1010 label1.Text = "正在 标记眉毛...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 1011 } 1012 break; 1013 case EYE: 1014 { 1015 label1.Text = "正在 标记眼睛...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 1016 } 1017 break; 1018 case NOSE: 1019 { 1020 label1.Text = "正在 标记鼻子...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 1021 } 1022 break; 1023 case MOUTH: 1024 { 1025 label1.Text = "正在 标记嘴巴...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 1026 } 1027 break; 1028 default: break; 1029 } 1030 1031 } 1032 #endregion 1033 1034 1035 #region 标记功能选择 1036 private void 标记眉毛ToolStripMenuItem_Click(object sender, EventArgs e) 1037 { 1038 function_select = BROW; 1039 1040 if (all_picture_path.Count != 0) 1041 { 1042 label1.Text = "正在 标记眉毛...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 1043 1044 } 1045 else 1046 { 1047 MessageBox.Show("先设置图片路径"); 1048 return; 1049 } 1050 1051 Load_xml_set_button(); 1052 1053 } 1054 1055 private void 标记眼睛ToolStripMenuItem_Click(object sender, EventArgs e) 1056 { 1057 1058 function_select = EYE; 1059 1060 if(all_picture_path.Count != 0) 1061 { 1062 label1.Text = "正在 标记眼睛...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 1063 1064 } 1065 else 1066 { 1067 MessageBox.Show("先设置图片路径"); 1068 return; 1069 } 1070 1071 Load_xml_set_button(); 1072 } 1073 1074 private void 标记鼻子ToolStripMenuItem_Click(object sender, EventArgs e) 1075 { 1076 function_select = NOSE; 1077 1078 if (all_picture_path.Count != 0) 1079 { 1080 label1.Text = "正在 标记鼻子...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 1081 1082 } 1083 else 1084 { 1085 MessageBox.Show("先设置图片路径"); 1086 return; 1087 } 1088 1089 Load_xml_set_button(); 1090 } 1091 1092 private void 标记嘴巴ToolStripMenuItem_Click(object sender, EventArgs e) 1093 { 1094 function_select = MOUTH; 1095 1096 if (all_picture_path.Count != 0) 1097 { 1098 label1.Text = "正在 标记嘴巴...第 " + current_picture_index.ToString() + " 张图片," + all_picture_name[current_picture_index - 1]; 1099 1100 } 1101 else 1102 { 1103 MessageBox.Show("先设置图片路径"); 1104 return; 1105 } 1106 1107 Load_xml_set_button(); 1108 } 1109 #endregion 1110 1111 #region 是否使图片选择按钮不可见 1112 private void Using_select_picture_button(bool if_or_not) 1113 { 1114 if(if_or_not == true) 1115 { 1116 buttonLeft.Visible = true; 1117 buttonRight.Visible = true; 1118 } 1119 else 1120 { 1121 buttonLeft.Visible = false; 1122 buttonRight.Visible = false; 1123 } 1124 1125 } 1126 #endregion 1127 1128 #region 选择一种类型后,使该按钮禁用,使其它类型按钮不可见 1129 private void Select_one_type_button(object sender) 1130 { 1131 for(int i=0;i < xml_node_num; i++) 1132 { 1133 if(type_button[i].Name == ((Button)sender).Name) 1134 { 1135 type_button[i].Enabled = false; 1136 1137 } 1138 else 1139 { 1140 type_button[i].Visible = false; 1141 } 1142 } 1143 1144 } 1145 #endregion 1146 1147 #region 使所有类型按钮可用可见 1148 private void All_type_button_ok() 1149 { 1150 if (xml_node_num != 0) 1151 { 1152 for (int i = 0; i < xml_node_num; i++) 1153 { 1154 type_button[i].Visible = true; 1155 type_button[i].Enabled = true; 1156 } 1157 } 1158 else 1159 return; 1160 1161 } 1162 #endregion 1163 1164 1165 private void Form1_Load(object sender, EventArgs e) 1166 { 1167 //auto_size_form.Init_control_size(this); 1168 } 1169 1170 private void Form1_SizeChanged(object sender, EventArgs e) 1171 { 1172 //auto_size_form.Auto_control_size(this); 1173 1174 } 1175 1176 #region 快捷键按下处理消息 1177 private void Form1_KeyDown(object sender, KeyEventArgs e) 1178 { 1179 if (xml_node_num > 0) 1180 { 1181 for (int i = 0; i < xml_node_num; i++) 1182 { 1183 if (e.KeyCode == key_type[i]) 1184 { 1185 type_button_Click(type_button[i], EventArgs.Empty); 1186 } 1187 1188 } 1189 1190 } 1191 //else 1192 // MessageBox.Show("先加载 xml 文件"); 1193 } 1194 #endregion 1195 1196 private void 恢复默认设置ToolStripMenuItem_Click(object sender, EventArgs e) 1197 { 1198 key_type[0] = Keys.NumPad7; 1199 key_type[1] = Keys.NumPad8; 1200 key_type[2] = Keys.NumPad9; 1201 key_type[3] = Keys.NumPad4; 1202 key_type[4] = Keys.NumPad5; 1203 key_type[5] = Keys.NumPad6; 1204 key_type[6] = Keys.NumPad1; 1205 key_type[7] = Keys.NumPad2; 1206 key_type[8] = Keys.NumPad3; 1207 } 1208 1209 private void 自定义ToolStripMenuItem_Click(object sender, EventArgs e) 1210 { 1211 Form f2 = new Form2Hotkeys(); 1212 f2.ShowDialog(); 1213 1214 } 1215 1216 } 1217 1218 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace Image_mark 12 { 13 public partial class Form2Hotkeys : Form 14 { 15 16 private Keys[] key = new Keys[9]; 17 private bool[] change_ok = new bool[9]; 18 19 public Form2Hotkeys() 20 { 21 InitializeComponent(); 22 23 } 24 25 private void textBox1_KeyDown(object sender, KeyEventArgs e) 26 { 27 change_ok[0] = false; 28 29 if (textBox1.Text != null) 30 { 31 change_ok[0] = true; 32 key[0] = e.KeyCode; 33 } 34 35 } 36 37 private void textBox2_KeyDown(object sender, KeyEventArgs e) 38 { 39 change_ok[1] = false; 40 41 if (textBox2.Text != null) 42 { 43 change_ok[1] = true; 44 key[1] = e.KeyCode; 45 } 46 } 47 48 private void textBox3_KeyDown(object sender, KeyEventArgs e) 49 { 50 change_ok[2] = false; 51 52 if (textBox3.Text != null) 53 { 54 change_ok[2] = true; 55 key[2] = e.KeyCode; 56 } 57 } 58 59 private void textBox4_KeyDown(object sender, KeyEventArgs e) 60 { 61 change_ok[3] = false; 62 63 if (textBox4.Text != null) 64 { 65 change_ok[3] = true; 66 key[3] = e.KeyCode; 67 } 68 } 69 70 private void textBox5_KeyDown(object sender, KeyEventArgs e) 71 { 72 change_ok[4] = false; 73 74 if (textBox5.Text != null) 75 { 76 change_ok[4] = true; 77 key[4] = e.KeyCode; 78 } 79 } 80 81 private void textBox6_KeyDown(object sender, KeyEventArgs e) 82 { 83 change_ok[5] = false; 84 85 if (textBox6.Text != null) 86 { 87 change_ok[5] = true; 88 key[5] = e.KeyCode; 89 } 90 } 91 92 private void textBox7_KeyDown(object sender, KeyEventArgs e) 93 { 94 change_ok[6] = false; 95 96 if (textBox7.Text != null) 97 { 98 change_ok[6] = true; 99 key[6] = e.KeyCode; 100 } 101 } 102 103 private void textBox8_KeyDown(object sender, KeyEventArgs e) 104 { 105 change_ok[7] = false; 106 107 if (textBox8.Text != null) 108 { 109 change_ok[7] = true; 110 key[7] = e.KeyCode; 111 } 112 } 113 114 private void textBox9_KeyDown(object sender, KeyEventArgs e) 115 { 116 change_ok[8] = false; 117 118 if (textBox9.Text != null) 119 { 120 change_ok[8] = true; 121 key[8] = e.KeyCode; 122 } 123 } 124 125 private void button1_Click(object sender, EventArgs e) 126 { 127 for(int i=0; i<9; i++) 128 { 129 if(change_ok[i]) 130 { 131 Form1.key_type[i] = key[i]; 132 } 133 } 134 135 this.Close(); 136 137 } 138 139 } 140 }