Form1.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. //Different Program States
  21. public enum ProgramState
  22. {
  23. Idle,
  24. Draw
  25. }
  26. //Current Program State
  27. private ProgramState currentState;
  28. //Dialog to select a file.
  29. OpenFileDialog openFileDialogLeft = new OpenFileDialog();
  30. //Image loaded on the left
  31. Image leftImage = null;
  32. //Image on the right
  33. Image rightImage = null;
  34. private void Form1_Load(object sender, EventArgs e)
  35. {
  36. currentState = ProgramState.Idle;
  37. this.DoubleBuffered = true;
  38. //Connect the Paint event of the left picture box to the event handler method.
  39. pictureBoxLeft.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxLeft_Paint);
  40. pictureBoxRight.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxRight_Paint);
  41. }
  42. //Resize Function connected to the form resize event, will refresh the form when it is resized
  43. private void Form1_Resize(object sender, System.EventArgs e)
  44. {
  45. this.Refresh();
  46. }
  47. private void pictureBoxLeft_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  48. {
  49. //Draw the left image
  50. if(leftImage != null)
  51. {
  52. //pictureBoxLeft.Image = leftImage;
  53. }
  54. }
  55. private void pictureBoxRight_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  56. {
  57. //Draw the right image
  58. if (rightImage != null)
  59. {
  60. //pictureBoxRight.Image = rightImage;
  61. }
  62. }
  63. // A Table Layout with one row and two columns.
  64. // Columns are 50% so that the window is evenly split.
  65. // The size is manually set relative to the window size.
  66. // TODO: Maybe change this to automatically be the size of a parent container...
  67. private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
  68. {
  69. }
  70. private void fileToolStripMenuItem_Click(object sender, EventArgs e)
  71. {
  72. }
  73. //Load button, will open an OpenFileDialog
  74. private void loadToolStripMenuItem_Click(object sender, EventArgs e)
  75. {
  76. openFileDialogLeft.Filter = "Image|*.jpg;*.png;*.jpeg";
  77. if(openFileDialogLeft.ShowDialog() == DialogResult.OK)
  78. {
  79. toolStripLoadStatus.Text = openFileDialogLeft.SafeFileName;
  80. leftImage = Image.FromFile(openFileDialogLeft.FileName);
  81. pictureBoxLeft.Image = leftImage;
  82. //Refresh the left image box when the content is changed
  83. this.Refresh();
  84. //pictureBoxLeft.Refresh();
  85. }
  86. }
  87. private void toolStripStatusLabel1_Click(object sender, EventArgs e)
  88. {
  89. }
  90. private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  91. {
  92. }
  93. private void pictureBoxLeft_Click(object sender, EventArgs e)
  94. {
  95. }
  96. //Beginn userstory4
  97. //Bitmap skizze = null;
  98. Graphics graph = null;
  99. Point[] points = new Point[10]; //array Mousepositons
  100. int i = 0;
  101. Point p;// = new PointF(x, y);
  102. bool mousedown = false;
  103. List<Point> currentLine = new List<Point>();
  104. //Create an image relative to the mouse positions, which the method gets from pictureBoxRight_MouseMove
  105. public void addPath(Point p)
  106. {
  107. Pen pen = new Pen(Color.White);
  108. Point first;
  109. Point second;
  110. points[i] = p;
  111. graph = Graphics.FromImage(rightImage);
  112. first = points[0];
  113. currentLine.Add(p);
  114. if (i == 1)
  115. {
  116. second = points[1];
  117. graph.DrawLine(pen, first, second);
  118. points[0] = second;
  119. i = 0;
  120. }
  121. }
  122. //Changes The State of the Program to drawing
  123. private void drawButton_Click(object sender, EventArgs e)
  124. {
  125. if (currentState.Equals(ProgramState.Draw))
  126. {
  127. changeState(ProgramState.Idle);
  128. }
  129. else
  130. {
  131. changeState(ProgramState.Draw);
  132. }
  133. }
  134. //get current Mouse positon
  135. private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
  136. {
  137. p = convertCoordinates(new Point(e.X, e.Y));
  138. }
  139. //hold left mouse button to draw.
  140. private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
  141. {
  142. mousedown = true;
  143. }
  144. //Lift left mouse button to stop drawing.
  145. private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
  146. {
  147. mousedown = false;
  148. Line linecs = new Line(currentLine);
  149. linecs.DrawLine(graph);
  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 canvasButton_Click(object sender, EventArgs e)
  156. {
  157. if (leftImage == null)
  158. {
  159. rightImage = new Bitmap(pictureBoxRight.Width, pictureBoxRight.Height);
  160. graph = Graphics.FromImage(rightImage);
  161. graph.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
  162. pictureBoxRight.Image = rightImage;
  163. }
  164. else
  165. {
  166. rightImage = new Bitmap(leftImage.Width, leftImage.Height);
  167. graph = Graphics.FromImage(rightImage);
  168. graph.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
  169. pictureBoxRight.Image = rightImage;
  170. }
  171. this.Refresh();
  172. pictureBoxRight.Refresh();
  173. }
  174. //add a Point on every tick to the Drawpath
  175. private void drawTimer_Tick(object sender, EventArgs e)
  176. {
  177. if (currentState.Equals(ProgramState.Draw) && mousedown)
  178. {
  179. addPath(p);
  180. pictureBoxRight.Image = rightImage;
  181. i++;
  182. }
  183. if (!mousedown)
  184. {
  185. points[0] = p;
  186. }
  187. }
  188. /// <summary>
  189. /// A helper function which handles tasks associated witch changing states,
  190. /// such as checking and unchecking buttons and changing the state.
  191. /// </summary>
  192. /// <param name="newState">The new state of the program</param>
  193. private void changeState(ProgramState newState)
  194. {
  195. switch (currentState)
  196. {
  197. case ProgramState.Draw:
  198. drawButton.CheckState = CheckState.Unchecked;
  199. drawTimer.Enabled = false;
  200. break;
  201. default:
  202. break;
  203. }
  204. switch (newState)
  205. {
  206. case ProgramState.Draw:
  207. drawButton.CheckState = CheckState.Checked;
  208. drawTimer.Enabled = true;
  209. break;
  210. default:
  211. break;
  212. }
  213. currentState = newState;
  214. }
  215. /// <summary>
  216. /// A function that calculates the coordinates of a point on a zoomed in image.
  217. /// </summary>
  218. /// <param name="">The position of the mouse cursor</param>
  219. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  220. private Point convertCoordinates(Point cursorPosition)
  221. {
  222. Point realCoordinates = new Point(5,3);
  223. if(pictureBoxRight.Image == null)
  224. {
  225. return cursorPosition;
  226. }
  227. int widthImage = pictureBoxRight.Image.Width;
  228. int heightImage = pictureBoxRight.Image.Height;
  229. int widthBox = pictureBoxRight.Width;
  230. int heightBox = pictureBoxRight.Height;
  231. float imageRatio = (float)widthImage / (float)heightImage;
  232. float containerRatio = (float)widthBox / (float)heightBox;
  233. if (imageRatio >= containerRatio)
  234. {
  235. //Image is wider than it is high
  236. float zoomFactor = (float)widthImage / (float)widthBox;
  237. float scaledHeight = heightImage / zoomFactor;
  238. float filler = (heightBox - scaledHeight) / 2;
  239. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  240. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  241. }
  242. else
  243. {
  244. //Image is higher than it is wide
  245. float zoomFactor = (float)heightImage / (float)heightBox;
  246. float scaledWidth = widthImage / zoomFactor;
  247. float filler = (widthBox - scaledWidth) / 2;
  248. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  249. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  250. }
  251. return realCoordinates;
  252. }
  253. }
  254. }