Form1.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. //using SvgNet;
  11. // This is the code for your desktop app.
  12. // Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.
  13. namespace SketchAssistant
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. //Dialog to select a file.
  22. OpenFileDialog openFileDialogLeft = new OpenFileDialog();
  23. //Image loaded on the left
  24. Image leftImage = null;
  25. private void Form1_Load(object sender, EventArgs e)
  26. {
  27. this.DoubleBuffered = true;
  28. //Connect the Paint event of the left picture box to the event handler method.
  29. pictureBoxLeft.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxLeft_Paint);
  30. }
  31. private void pictureBoxLeft_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  32. {
  33. //Draw the left image
  34. if(leftImage != null)
  35. {
  36. pictureBoxLeft.Image = leftImage;
  37. }
  38. }
  39. //TODO: Remove this placeholder when real buttons are in place
  40. private void toolStripLabel1_Click(object sender, EventArgs e)
  41. {
  42. }
  43. // A Table Layout with one row and two columns.
  44. // Columns are 50% so that the window is evenly split.
  45. // The size is manually set relative to the window size.
  46. // TODO: Maybe change this to automatically be the size of a parent container...
  47. private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
  48. {
  49. }
  50. private void fileToolStripMenuItem_Click(object sender, EventArgs e)
  51. {
  52. }
  53. //Load button, will open an OpenFileDialog
  54. private void loadToolStripMenuItem_Click(object sender, EventArgs e)
  55. {
  56. openFileDialogLeft.Filter = "Image|*.jpg;*.png;*.jpeg";
  57. if(openFileDialogLeft.ShowDialog() == DialogResult.OK)
  58. {
  59. toolStripLoadStatus.Text = openFileDialogLeft.SafeFileName;
  60. leftImage = Image.FromFile(openFileDialogLeft.FileName);
  61. //The following line is needed, as else on first image load
  62. //the image will only be shown after resizing the window.
  63. this.Refresh();
  64. }
  65. }
  66. private void toolStripStatusLabel1_Click(object sender, EventArgs e)
  67. {
  68. }
  69. private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  70. {
  71. }
  72. //Timer that refreshes the picture box, so that it will always show the right contents in the right size
  73. private void timer1_Tick(object sender, EventArgs e)
  74. {
  75. pictureBoxLeft.Update();
  76. }
  77. private void pictureBoxLeft_Click(object sender, EventArgs e)
  78. {
  79. }
  80. //Beginn userstory4
  81. Bitmap skizze = null;
  82. Graphics graph = null;
  83. int x = 0;
  84. int y = 0;
  85. PointF[] points = new PointF[10]; //array Mousepositons
  86. int i = 0;
  87. PointF first;
  88. PointF second;
  89. bool clicked = false; //Button "Paint" is clicked or not
  90. PointF p;// = new PointF(x, y);
  91. bool mousedown = false;
  92. Pen pen = new Pen(Color.Black);
  93. //Create an image relative to the mouse positions, which the method gets from pictureBoxRight_MouseMove
  94. public void addPath(PointF p)
  95. {
  96. points[i] = p;
  97. graph = Graphics.FromImage(skizze);
  98. first = points[0];
  99. if (i == 1)
  100. {
  101. second = points[1];
  102. graph.DrawLine(pen, first, second);
  103. points[0] = second;
  104. i = 0;
  105. }
  106. }
  107. // creates an empty image and prepares rightPictureBox for drawing
  108. private void painttoolStripMenuItem_Click(object sender, EventArgs e)
  109. {
  110. skizze = new Bitmap(500, 800);
  111. graph = Graphics.FromImage(skizze);
  112. graph.FillRectangle(Brushes.White, 0, 0, 500, 800);
  113. pictureBoxRight.Image = skizze;
  114. timer2.Enabled = !clicked;
  115. clicked = !clicked;
  116. }
  117. //add a Point on every tick to the Drawpath
  118. private void timer2_Tick(object sender, EventArgs e)
  119. {
  120. timer2.Interval = 100;
  121. if (clicked && mousedown)
  122. {
  123. addPath(p);
  124. pictureBoxRight.Image = skizze;
  125. i++;
  126. }
  127. }
  128. //get current Mouse positon
  129. private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
  130. {
  131. x = e.X;
  132. y = e.Y;
  133. p = new PointF(x, y);
  134. }
  135. //hold left mouse button to draw.
  136. private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
  137. {
  138. mousedown = true;
  139. }
  140. //Lift left mouse button to stop drawing.
  141. private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
  142. {
  143. mousedown = false;
  144. }
  145. //Ende userstory4
  146. }
  147. }