hering.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // hering.cs
  3. // Creates image for Hering illusion.
  4. // Converted to C# from Xr demo application.
  5. //
  6. // Author:
  7. // Alexandre Pigolkine([email protected])
  8. //
  9. //
  10. // Copyright (C) Ximian, Inc. http://www.ximian.com
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Drawing;
  35. using System.Drawing.Imaging;
  36. using System.Drawing.Drawing2D;
  37. namespace MonoSamples.System.Drawing
  38. {
  39. public class Hering
  40. {
  41. public static void Main (String[] args) {
  42. float width = 400.0F;
  43. float height = 800.0F;
  44. Bitmap bmp = new Bitmap ((int) width, (int) height);
  45. Graphics gr = Graphics.FromImage (bmp);
  46. gr.Clear (Color.White);
  47. int LINES = 32;
  48. float MAX_THETA = (.80F * 90.0F);
  49. float THETA = (2 * MAX_THETA / (LINES-1));
  50. GraphicsState oldState = gr.Save ();
  51. Pen blackPen = new Pen (Color.Black, 2.0F);
  52. gr.TranslateTransform (width / 2.0F, height / 2.0F);
  53. gr.RotateTransform (MAX_THETA);
  54. for ( int i = 0; i < LINES; i++) {
  55. gr.DrawLine (blackPen, -2.0F * width, 0.0F, 2.0F * width, 0.0F);
  56. gr.RotateTransform (-THETA);
  57. }
  58. gr.Restore (oldState);
  59. Pen redPen = new Pen (Color.Red, 6F);
  60. gr.DrawLine (redPen, width / 4F, 0F, width / 4F, height);
  61. gr.DrawLine (redPen, 3F * width / 4F, 0F, 3F * width / 4F, height);
  62. /* save image in all the formats */
  63. bmp.Save ("hering.png", ImageFormat.Png);
  64. Console.WriteLine ("output file hering.png");
  65. bmp.Save ("hering.jpg", ImageFormat.Jpeg);
  66. Console.WriteLine ("output file hering.jpg");
  67. bmp.Save ("hering.bmp", ImageFormat.Bmp);
  68. Console.WriteLine ("output file hering.bmp");
  69. }
  70. }
  71. }