scan.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // scan.cs
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.ComponentModel;
  30. using System.Drawing;
  31. using System.Drawing.Drawing2D;
  32. using System.Text;
  33. using System.Windows.Forms;
  34. namespace Samples {
  35. public partial class scan: Form {
  36. private Region region;
  37. private Matrix matrix;
  38. private RectangleF[] scans;
  39. public scan ()
  40. {
  41. InitializeComponent ();
  42. object[] shapes = Samples.Common.Shapes.GetList ();
  43. shapeComboBox.Items.AddRange (shapes);
  44. object[] matrices = Samples.Common.Matrices.GetList ();
  45. matrixComboBox.Items.AddRange (matrices);
  46. }
  47. private void Form1_Paint (object sender, PaintEventArgs e)
  48. {
  49. if (region != null)
  50. e.Graphics.FillRegion (Brushes.Blue, region);
  51. if (scansCheckBox.Checked && (scans != null) && (scans.Length > 0)) {
  52. e.Graphics.DrawRectangles (Pens.Red, scans);
  53. }
  54. }
  55. private GraphicsPath GetShape (ComboBox cb)
  56. {
  57. return Samples.Common.Shapes.GetShape (cb.SelectedIndex);
  58. }
  59. private Matrix Matrix {
  60. get {
  61. if (matrix == null)
  62. matrix = new Matrix ();
  63. return matrix;
  64. }
  65. }
  66. private void OnShapeChange (object sender, EventArgs e)
  67. {
  68. GraphicsPath path = GetShape (shapeComboBox);
  69. if (path != null) {
  70. if (region != null) {
  71. region.Dispose ();
  72. }
  73. region = new Region (path);
  74. scans = region.GetRegionScans (Matrix);
  75. infoLabel.Text = System.String.Format ("{0} rectangles to re-create the shape.", scans.Length);
  76. StringBuilder sb = new StringBuilder ();
  77. for (int i = 0; i < scans.Length; i++) {
  78. sb.AppendFormat ("{0}: x {1}, y {2}, w {3}, h {4}{5}", i,
  79. scans[i].X, scans[i].Y, scans[i].Width, scans[i].Height,
  80. Environment.NewLine);
  81. }
  82. scansTextBox.Text = sb.ToString ();
  83. }
  84. UpdateUI ();
  85. }
  86. private void OnMatrixChange (object sender, EventArgs e)
  87. {
  88. if (matrix != null) {
  89. matrix.Dispose ();
  90. }
  91. matrix = Samples.Common.Matrices.GetMatrix (matrixComboBox.SelectedIndex);
  92. OnShapeChange (sender, e);
  93. }
  94. private void UpdateUI ()
  95. {
  96. Invalidate ();
  97. Update ();
  98. }
  99. private void OnDisplayScans (object sender, EventArgs e)
  100. {
  101. UpdateUI ();
  102. }
  103. [STAThread]
  104. static void Main ()
  105. {
  106. Application.EnableVisualStyles ();
  107. Application.SetCompatibleTextRenderingDefault (false);
  108. Application.Run (new scan ());
  109. }
  110. }
  111. }