Form1.cs 6.5 KB

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