■ 카메라 사진 크기를 측정하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 |
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Globalization; using System.IO; using System.Text; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 카메라 정보 리스트 /// </summary> private List<CameraInfo> cameraInfoList = new List<CameraInfo>(); /// <summary> /// 축소/확대 /// </summary> private float zoom = 1; /// <summary> /// 시작 X 좌표 /// </summary> private int startX = 0; /// <summary> /// 시작 Y 좌표 /// </summary> private int startY = 0; /// <summary> /// 이동 X 좌표 /// </summary> private int moveX = 0; /// <summary> /// 이동 Y 좌표 /// </summary> private int moveY = 0; /// <summary> /// 측정 /// </summary> private double measure = 0; /// <summary> /// 객체 크기 /// </summary> private double objectSize = 0; /// <summary> /// 객체 거리 /// </summary> private double objectDistance = 0; /// <summary> /// 객체 크기 스케일 /// </summary> private double objectSizeScale = .01; /// <summary> /// 객체 거리 스케일 /// </summary> private double objectDistanceScale = 1; /// <summary> /// 측정 시작 X 좌표 /// </summary> private int measureStartX = 0; /// <summary> /// 측정 시작 Y 좌표 /// </summary> private int measureStartY = 0; /// <summary> /// 수평 여부 /// </summary> private bool horizontal = true; /// <summary> /// 비트맵 /// </summary> private Bitmap bitmap; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 메인 폼 - MainForm() /// <summary> /// 메인 폼 /// </summary> public MainForm() { InitializeComponent(); BuildCameraMenuItem(); #region 파일 열기 대화 상자를 설정한다. this.openFileDialog.FileName = ""; this.openFileDialog.Filter = "이미지 파일|*.*|JPG|*.jpg"; this.openFileDialog.RestoreDirectory = true; #endregion #region 이벤트를 설정한다. Load += Form_Load; Resize += Form_Resize; this.openImageMenuItem.Click += openImageMenuItem_Click; this.exitMenuItem.Click += exitMenuItem_Click; this.thumbnailPictureBox.MouseDown += thumbnailPictureBox_MouseDown; this.zoomTrackBar.Scroll += zoomTrackBar_Scroll; this.foscalLengthTextBox.TextChanged += foscalLengthTextBox_TextChanged; this.cameraFactoryTextBox.TextChanged += cameraFactoryTextBox_TextChanged; this.sensorWidthTextBox.TextChanged += foscalLengthTextBox_TextChanged; this.sensorHeightTextBox.TextChanged += foscalLengthTextBox_TextChanged; this.objectSizeTextBox.Click += objectSizeTextBox_Click; this.objectSizeTextBox.TextChanged += objectSizeTextBox_TextChanged; this.objectSizeScaleComboBox.TextChanged += objectSizeScaleComboBox_TextChanged; this.objectSizeScaleComboBox.DropDownClosed += objectSizeScaleComboBox_DropDownClosed; this.findDistanceButton.Click += findDistanceButton_Click; this.findSizeButton.Click += findSizeButton_Click; this.objectDistanceTextBox.Click += objectDistanceTextBox_Click; this.objectDistanceTextBox.TextChanged += objectDistanceTextBox_TextChanged; this.objectDistanceSacleComboBox.TextChanged += objectDistanceSacleComboBox_TextChanged; this.objectDistanceSacleComboBox.DropDownClosed += objectDistanceSacleComboBox_DropDownClosed; this.moveImageButton.Click += moveImageButton_Click; this.measureButton.Click += measureButton_Click; this.mainPictureBox.MouseDown += mainPictureBox_MouseDown; this.mainPictureBox.MouseMove += mainPictureBox_MouseMove; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { UpdatePictureBox(); } #endregion #region 폼 크기 조정시 처리하기 - Form_Resize(sender, e) /// <summary> /// 폼 크기 조정시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Resize(object sender, EventArgs e) { UpdatePictureBox(); } #endregion #region 이미지 열기 메뉴 항목 클릭시 처리하기 - openImageMenuItem_Click(sender, e) /// <summary> /// 이미지 열기 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openImageMenuItem_Click(object sender, EventArgs e) { if(this.openFileDialog.ShowDialog() == DialogResult.OK) { string imageFilePath = this.openFileDialog.FileName; try { Image image = Image.FromFile(imageFilePath); this.thumbnailPictureBox.Image = image; this.zoomTrackBar.Value = 50; this.startX = 0; this.startY = 0; this.objectSize = 0; this.objectDistance = 0; this.measure = 0; this.objectDistanceTextBox.Text = string.Empty; this.objectSizeTextBox.Text = string.Empty; this.locationOnFileLabel.Text = "파일상 위치 (pixel) : "; this.locationOnSenserLabel.Text = "센서상 위치 (mm) : "; this.sizeOnSensorLabel.Text = "센서상 크기 (mm) : "; this.imageWidthTextBox.Text = string.Empty; this.imageHeightTextBox.Text = string.Empty; this.foscalLengthTextBox.Text = string.Empty; this.cameraFactoryTextBox.Text = string.Empty; SetZoom(); PropertyItem propertyItem = image.GetPropertyItem(37386); byte[] lowByteArray = new byte[propertyItem.Len / 2]; byte[] highByteArray = new byte[propertyItem.Len / 2]; Array.Copy(propertyItem.Value, 0 , lowByteArray , 0, propertyItem.Len / 2); Array.Copy(propertyItem.Value, propertyItem.Len / 2, highByteArray, 0, propertyItem.Len / 2); this.foscalLengthTextBox.Text = Convert.ToString((float)GetUnsignedInteger(lowByteArray) / GetUnsignedInteger(highByteArray)); this.imageWidthTextBox.Text = image.PhysicalDimension.Width.ToString(); this.imageHeightTextBox.Text = image.PhysicalDimension.Height.ToString(); if(image.PhysicalDimension.Width > image.PhysicalDimension.Height) { this.horizontal = true; } else { this.horizontal = false; } Encoding encoding = Encoding.ASCII; string cameraFactory = encoding.GetString(image.GetPropertyItem(271).Value).Replace("\0", "\r\n"); cameraFactory += encoding.GetString(image.GetPropertyItem(272).Value); this.cameraFactoryTextBox.Text = cameraFactory; Text = "카메라 사진 크기 측정하기 " + " - " + openFileDialog.FileName; } catch { } } } #endregion #region 종료 메뉴 항목 클릭시 처리하기 - exitMenuItem_Click(sender, e) /// <summary> /// 종료 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void exitMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } #endregion #region 썸네일 픽처 박스 마우스 DOWN 처리하기 - thumbnailPictureBox_MouseDown(sender, e) /// <summary> /// 썸네일 픽처 박스 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void thumbnailPictureBox_MouseDown(object sender, MouseEventArgs e) { try { this.startX = this.thumbnailPictureBox.Image.Width * e.X / this.thumbnailPictureBox.Width; this.startY = this.thumbnailPictureBox.Image.Height * e.Y / this.thumbnailPictureBox.Height; UpdatePictureBox(); } catch { } } #endregion #region 축소/확대 트랙바 스크롤시 처리하기 - zoomTrackBar_Scroll(sender, e) /// <summary> /// 축소/확대 트랙바 스크롤시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void zoomTrackBar_Scroll(object sender, EventArgs e) { SetZoom(); } #endregion #region 초점 거리 텍스트 박스 텍스트 변경시 처리하기 - foscalLengthTextBox_TextChanged(sender, e) /// <summary> /// 초점 거리 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void foscalLengthTextBox_TextChanged(object sender, EventArgs e) { string equivalenceString = string.Empty; try { double equivalence = Math.Pow((Math.Pow(Convert.ToSingle(this.sensorWidthTextBox.Text), 2) + Math.Pow(Convert.ToSingle(this.sensorHeightTextBox.Text), 2)), 0.5); equivalence = Math.Round((43.2666153 / equivalence) * Convert.ToSingle(this.foscalLengthTextBox.Text)); equivalenceString = equivalence.ToString(); } catch { } this.equivalentTextBox.Text = equivalenceString; } #endregion #region 카메라 공장 텍스트 박스 텍스트 변경시 처리하기 - cameraFactoryTextBox_TextChanged(sender, e) /// <summary> /// 카메라 공장 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void cameraFactoryTextBox_TextChanged(object sender, EventArgs e) { SearchCamera(); } #endregion #region 객체 크기 텍스트 박스 클릭시 처리하기 - objectSizeTextBox_Click(sender, e) /// <summary> /// 객체 크기 텍스트 박스 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void objectSizeTextBox_Click(object sender, EventArgs e) { this.objectSizeTextBox.SelectionStart = 0; this.objectSizeTextBox.SelectionLength = this.objectSizeTextBox.Text.Length; } #endregion #region 객체 크기 텍스트 박스 텍스트 변경시 처리하기 - objectSizeTextBox_TextChanged(sender, e) /// <summary> /// 객체 크기 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void objectSizeTextBox_TextChanged(object sender, EventArgs e) { if(this.objectSizeTextBox.Selected) { try { SetFindButton(true); this.objectSize = Convert.ToDouble(this.objectSizeTextBox.Text) * this.objectSizeScale; SetObjectDistance(); } catch { } } } #endregion #region 객체 크기 스케일 콤보 박스 텍스트 변경시 처리하기 - objectSizeScaleComboBox_TextChanged(sender, e) /// <summary> /// 객체 크기 스케일 콤보 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void objectSizeScaleComboBox_TextChanged(object sender, EventArgs e) { this.objectSizeScale = GetSize(this.objectSizeScaleComboBox.Text); this.objectSizeTextBox.Text = Convert.ToString(Math.Round(this.objectSize / this.objectSizeScale, 2)); } #endregion #region 객체 크기 스케일 콤보 박스 드롭 다운 닫은 경우 처리하기 - objectSizeScaleComboBox_DropDownClosed(sender, e) /// <summary> /// 객체 크기 스케일 콤보 박스 드롭 다운 닫은 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void objectSizeScaleComboBox_DropDownClosed(object sender, EventArgs e) { this.zoomTrackBar.Select(); } #endregion #region 거리 찾기 버튼 클릭시 처리하기 - findDistanceButton_Click(sender, e) /// <summary> /// 거리 찾기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void findDistanceButton_Click(object sender, EventArgs e) { SetFindButton(true); } #endregion #region 크기 찾기 버튼 클릭시 처리하기 - findSizeButton_Click(sender, e) /// <summary> /// 크기 찾기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void findSizeButton_Click(object sender, EventArgs e) { SetFindButton(false ); } #endregion #region 객체 거리 텍스트 박스 클릭시 처리하기 - objectDistanceTextBox_Click(sender, e) /// <summary> /// 객체 거리 텍스트 박스 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void objectDistanceTextBox_Click(object sender, EventArgs e) { this.objectDistanceTextBox.SelectionStart = 0; this.objectDistanceTextBox.SelectionLength = this.objectDistanceTextBox.Text.Length; } #endregion #region 객체 거리 텍스트 박스 텍스트 변경시 처리하기 - objectDistanceTextBox_TextChanged(sender, e) /// <summary> /// 객체 거리 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void objectDistanceTextBox_TextChanged(object sender, EventArgs e) { if(this.objectDistanceTextBox.Selected) { try { SetFindButton(false); this.objectDistance = Convert.ToDouble(this.objectDistanceTextBox.Text) * this.objectDistanceScale; SetObjectSize(); } catch { } } } #endregion #region 객체 거리 스케일 콤보 박스 텍스트 변경시 처리하기 - objectDistanceSacleComboBox_TextChanged(sender, e) /// <summary> /// 객체 거리 스케일 콤보 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void objectDistanceSacleComboBox_TextChanged(object sender, EventArgs e) { this.objectDistanceScale = GetSize(this.objectDistanceSacleComboBox.Text); this.objectDistanceTextBox.Text = Convert.ToString(Math.Round(this.objectDistance / this.objectDistanceScale, 2)); } #endregion #region 객체 거리 스케일 콤보 박스 드롭 다운 닫은 경우 처리하기 - objectDistanceSacleComboBox_DropDownClosed(sender, e) /// <summary> /// 객체 거리 스케일 콤보 박스 드롭 다운 닫은 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void objectDistanceSacleComboBox_DropDownClosed(object sender, EventArgs e) { this.zoomTrackBar.Select(); } #endregion #region 이미지 이동 버튼 클릭시 처리하기 - moveImageButton_Click(sender, e) /// <summary> /// 이미지 이동 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void moveImageButton_Click(object sender, EventArgs e) { this.moveImageButton.Checked = true; this.measureButton.Checked = false; this.mainPictureBox.Cursor = Cursors.Hand; } #endregion #region 측정 버튼 클릭시 처리하기 - measureButton_Click(sender, e) /// <summary> /// 측정 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void measureButton_Click(object sender, EventArgs e) { this.moveImageButton.Checked = false; this.measureButton.Checked = true; this.mainPictureBox.Cursor = Cursors.Cross; } #endregion #region 메인 픽처 박스 마우스 DOWN 처리하기 - mainPictureBox_MouseDown(sender, e) /// <summary> /// 메인 픽처 박스 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void mainPictureBox_MouseDown(object sender, MouseEventArgs e) { if(this.moveImageButton.Checked) { this.moveX = e.X; this.moveY = e.Y; } else { this.measureStartX = e.X; this.measureStartY = e.Y; } } #endregion #region 메인 픽처 박스 마우스 이동시 처리하기 - mainPictureBox_MouseMove(sender, e) /// <summary> /// 메인 픽처 박스 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void mainPictureBox_MouseMove(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Left) { if(this.moveImageButton.Checked) { this.startX -= e.X - this.moveX; if(this.startX < 0) { this.startX = 0; } this.startY -= e.Y - this.moveY; if(this.startY < 0) { this.startY = 0; } this.moveX = e.X; this.moveY = e.Y; UpdatePictureBox(); } else { try { double differenceX = Math.Abs(this.measureStartX - e.X) * this.zoom * Convert.ToSingle(this.horizontal ? this.sensorWidthTextBox.Text : this.sensorHeightTextBox.Text) / Convert.ToSingle(this.imageWidthTextBox.Text ); double differenceY = Math.Abs(this.measureStartY - e.Y) * this.zoom * Convert.ToSingle(this.horizontal ? this.sensorHeightTextBox.Text : this.sensorWidthTextBox.Text ) / Convert.ToSingle(this.imageHeightTextBox.Text); this.measure = Math.Pow((Math.Pow(differenceX, 2) + Math.Pow(differenceY, 2)), 0.5); this.sizeOnSensorLabel.Text = "센서상 크기 (mm) : " + this.measure.ToString(); this.mainPictureBox.Refresh(); Graphics graphics = mainPictureBox.CreateGraphics(); graphics.ResetClip(); graphics.DrawLine(new Pen(Color.Red), this.measureStartX, this.measureStartY, e.X, e.Y); if(this.findDistanceButton.Checked) { SetObjectDistance(); } else { SetObjectSize(); } } catch { } } } try { float fileX = this.startX + e.X * this.zoom; float fileY = this.startY + e.Y * this.zoom; float sensorX = fileX * Convert.ToSingle(this.horizontal ? this.sensorWidthTextBox.Text : this.sensorHeightTextBox.Text) / Convert.ToSingle(this.imageWidthTextBox.Text ); float sensorY = fileY * Convert.ToSingle(this.horizontal ? this.sensorHeightTextBox.Text : this.sensorWidthTextBox.Text ) / Convert.ToSingle(this.imageHeightTextBox.Text); this.locationOnFileLabel.Text = "파일상 위치 (pixel) X :" + fileX.ToString() + " Y : " + fileY.ToString(); this.locationOnSenserLabel.Text= "센서상 위치 (mm) X : " + sensorX.ToString() + " Y : " + sensorY.ToString(); } catch { } } #endregion #region 카메라 메뉴 항목 클릭시 처리하기 - cameraMenuItem_Click(sender, e) /// <summary> /// 카메라 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void cameraMenuItem_Click(object sender, EventArgs e) { this.cameraFactoryTextBox.Text = ((ToolStripItem)sender).OwnerItem.Text; this.cameraFactoryTextBox.Text += "\r\n" + ((ToolStripItem)sender).OwnerItem.Text + " " + ((ToolStripItem)sender).Text; } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 카메라 메뉴 항목 구축하기 - BuildCameraMenuItem() /// <summary> /// 카메라 메뉴 항목 구축하기 /// </summary> private void BuildCameraMenuItem() { this.cameraInfoList.Clear(); this.cameraMenuItem.DropDownItems.Clear(); string cameraListFilePath = Path.Combine(Application.StartupPath, @"DATA\CameraList.info"); if(File.Exists(cameraListFilePath)) { string content = File.ReadAllText(cameraListFilePath); string[] lineArray = content.Split('\n'); CultureInfo cultureInfo = new CultureInfo("en-US"); foreach(string line in lineArray) { if(string.IsNullOrWhiteSpace(line)) { continue; } if(line.StartsWith("//")) { continue; } string[] tokenArray = line.Split(new string[] { "," }, StringSplitOptions.None); try { CameraInfo cameraInfo = new CameraInfo(); cameraInfo.Factory = tokenArray[0].Trim(); cameraInfo.Model = tokenArray[1].Trim(); cameraInfo.AliasName = tokenArray[2].Trim(); cameraInfo.SensorType = tokenArray[3].Trim(); cameraInfo.SensorWidth = Single.Parse(tokenArray[4], cultureInfo); cameraInfo.SensorHeight = Single.Parse(tokenArray[5], cultureInfo); this.cameraInfoList.Add(cameraInfo); bool newFactory = true; foreach(ToolStripMenuItem menuItem in this.cameraMenuItem.DropDownItems) { if(menuItem.Text.Contains(cameraInfo.Factory)) { newFactory = false; break; } } if(newFactory) { this.cameraMenuItem.DropDownItems.Add(cameraInfo.Factory); } foreach(ToolStripMenuItem menuItem in this.cameraMenuItem.DropDownItems) { if(menuItem.Text.Contains(cameraInfo.Factory)) { menuItem.DropDownItems.Add(cameraInfo.Model + (cameraInfo.AliasName == "" ? "" : " - " + cameraInfo.AliasName ), null, cameraMenuItem_Click); } } } catch { } } } } #endregion #region 픽처 박스 업데이트 하기 - UpdatePictureBox() /// <summary> /// 픽처 박스 업데이트 하기 /// </summary> private void UpdatePictureBox() { try { this.bitmap = new Bitmap(this.mainPictureBox.Width, this.mainPictureBox.Height); Graphics bitmapGraphics = Graphics.FromImage(this.bitmap); bitmapGraphics.SmoothingMode = SmoothingMode.AntiAlias; bitmapGraphics.DrawImage ( this.thumbnailPictureBox.Image, new Rectangle(0, 0, this.mainPictureBox.Width, this.mainPictureBox.Height), new Rectangle(this.startX, this.startY, (int)(this.mainPictureBox.Width * this.zoom), (int)(this.mainPictureBox.Height * this.zoom)), GraphicsUnit.Pixel ); this.mainPictureBox.Image = this.bitmap; } catch { } } #endregion #region 카메라 찾기 - SearchCamera() /// <summary> /// 카메라 찾기 /// </summary> private void SearchCamera() { this.sensorTypeTextBox.Text = string.Empty; this.sensorHeightTextBox.Text = string.Empty; this.sensorWidthTextBox.Text = string.Empty; this.cameraPictureBox.Image = null; this.toolTip.SetToolTip(this.cameraPictureBox, string.Empty); string searchControl = "MRK"; string camereName = this.cameraFactoryTextBox.Text.Replace(" ", "").Replace("-", "").ToLower() + searchControl; bool cameraFound = false; byte cameraLoop = 0; do { foreach(CameraInfo cameraInfo in this.cameraInfoList) { if(camereName.Contains(cameraInfo.Factory.Replace(" ", "").ToLower()) && !cameraFound) { if(camereName.Contains(cameraInfo.Model.Replace(" ", "").Replace("-", "").ToLower() + searchControl) || (camereName.Contains(cameraInfo.AliasName.Replace(" ", "").Replace("-", "").ToLower() + searchControl) && cameraInfo.AliasName != "")) { this.sensorTypeTextBox.Text = cameraInfo.SensorType; this.sensorWidthTextBox.Text = cameraInfo.SensorWidth.ToString(); this.sensorHeightTextBox.Text = cameraInfo.SensorHeight.ToString(); this.toolTip.SetToolTip(this.cameraPictureBox, cameraInfo.Factory + " " + cameraInfo.Model); string cameraImageFilePath = Application.StartupPath + "\\Data\\" + cameraInfo.Factory + "_" + cameraInfo.Model.Replace(" ", "").Replace("-", "") + ".gif"; if(File.Exists(cameraImageFilePath)) { this.cameraPictureBox.Image = Image.FromFile(cameraImageFilePath); } cameraFound = true; } } } searchControl = string.Empty; cameraLoop++; } while(!cameraFound && cameraLoop < 2); } #endregion #region 축소/확대 설정하기 - SetZoom() /// <summary> /// 축소/확대 설정하기 /// </summary> private void SetZoom() { this.zoom = 50 / (float)zoomTrackBar.Value; this.toolTip.SetToolTip(zoomTrackBar ,"줌 배율 : " + Convert.ToString((100 / this.zoom)) + "%"); UpdatePictureBox(); } #endregion #region 부호 없는 정수 구하기 - GetUnsignedInteger(sourceArray) /// <summary> /// 부호 없는 정수 구하기 /// </summary> /// <param name="sourceArray">소스 배열</param> /// <returns>부호 없는 정수</returns> private uint GetUnsignedInteger(byte[] sourceArray) { if(sourceArray.Length != 4) { return 0; } else { return Convert.ToUInt32(sourceArray[3] << 24 | sourceArray[2] << 16 | sourceArray[1] << 8 | sourceArray[0]); } } #endregion #region 객체 거리 설정하기 - SetObjectDistance() /// <summary> /// 객체 거리 설정하기 /// </summary> private void SetObjectDistance() { this.objectDistanceTextBox.Text = string.Empty; try { this.objectDistance = this.objectSize / (this.measure / Convert.ToDouble(this.foscalLengthTextBox.Text)); this.objectDistanceTextBox.Text = Convert.ToString(Math.Round(this.objectDistance / this.objectDistanceScale, 2)); } catch { } } #endregion #region 객체 크기 설정하기 - SetObjectSize() /// <summary> /// 객체 크기 설정하기 /// </summary> private void SetObjectSize() { this.objectSizeTextBox.Text = string.Empty; try { this.objectSize = this.objectDistance * (this.measure / Convert.ToDouble(this.foscalLengthTextBox.Text)); this.objectSizeTextBox.Text = Convert.ToString(Math.Round(this.objectSize / this.objectSizeScale, 2)); } catch { } } #endregion #region 찾기 버튼 설정하기 - SetFindButton(isChecked) /// <summary> /// 찾기 버튼 설정하기 /// </summary> /// <param name="isChecked">체크 여부</param> private void SetFindButton(bool isChecked) { this.findDistanceButton.Checked = isChecked; this.findSizeButton.Checked = !isChecked; if(isChecked) { SetObjectDistance(); } else { SetObjectSize(); } } #endregion #region 크기 구하기 - GetSize(sizeUnit) /// <summary> /// 크기 구하기 /// </summary> /// <param name="sizeUnit">크기 단위</param> /// <returns>크기</returns> private double GetSize(string sizeUnit) { switch(sizeUnit.Trim().ToLower()) { case "mm" : return .001; case "cm" : return .01; case "m" : return 1; case "km" : return 1000; case "inch" : return .0254; case "foot" : return .3048; case "mile" : return 1609.344; } return 0; } #endregion } } |