Form1.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. /**********************************/
  21. /*** CLASS VARIABLES START HERE ***/
  22. /**********************************/
  23. //Different Program States
  24. public enum ProgramState
  25. {
  26. Idle,
  27. Draw,
  28. Delete
  29. }
  30. //Current Program State
  31. private ProgramState currentState;
  32. //Dialog to select a file.
  33. OpenFileDialog openFileDialogLeft = new OpenFileDialog();
  34. //Image loaded on the left
  35. Image leftImage = null;
  36. //Image on the right
  37. Image rightImage = null;
  38. //Current Line being Drawn
  39. List<Point> currentLine;
  40. //All Lines in the current session
  41. List<Tuple<bool,Line>> lineList = new List<Tuple<bool, Line>>();
  42. //Whether the Mouse is currently pressed in the rightPictureBox
  43. bool mousePressed = false;
  44. //The Position of the Cursor in the right picture box
  45. Point currentCursorPosition;
  46. //The Previous Cursor Position in the right picture box
  47. Point previousCursorPosition;
  48. //The graphic representation of the right image
  49. Graphics graph = null;
  50. //Deletion Matrixes for checking postions of lines in the image
  51. bool[,] isFilledMatrix;
  52. List<int>[,] linesMatrix;
  53. //Size of deletion area
  54. int deletionSize = 10;
  55. //List with all cursorPositions
  56. List<Point> cursorPositions = new List<Point>();
  57. /******************************************/
  58. /*** FORM SPECIFIC FUNCTIONS START HERE ***/
  59. /******************************************/
  60. private void Form1_Load(object sender, EventArgs e)
  61. {
  62. currentState = ProgramState.Idle;
  63. this.DoubleBuffered = true;
  64. }
  65. //Resize Function connected to the form resize event, will refresh the form when it is resized
  66. private void Form1_Resize(object sender, System.EventArgs e)
  67. {
  68. this.Refresh();
  69. }
  70. //Load button, will open an OpenFileDialog
  71. private void loadToolStripMenuItem_Click(object sender, EventArgs e)
  72. {
  73. openFileDialogLeft.Filter = "Image|*.jpg;*.png;*.jpeg";
  74. if(openFileDialogLeft.ShowDialog() == DialogResult.OK)
  75. {
  76. toolStripLoadStatus.Text = openFileDialogLeft.SafeFileName;
  77. leftImage = Image.FromFile(openFileDialogLeft.FileName);
  78. pictureBoxLeft.Image = leftImage;
  79. //Refresh the left image box when the content is changed
  80. this.Refresh();
  81. }
  82. }
  83. //Changes the state of the program to drawing
  84. private void drawButton_Click(object sender, EventArgs e)
  85. {
  86. if(rightImage != null)
  87. {
  88. if (currentState.Equals(ProgramState.Draw))
  89. {
  90. ChangeState(ProgramState.Idle);
  91. }
  92. else
  93. {
  94. ChangeState(ProgramState.Draw);
  95. }
  96. }
  97. }
  98. //Changes the state of the program to deletion
  99. private void deleteButton_Click(object sender, EventArgs e)
  100. {
  101. if (rightImage != null)
  102. {
  103. if (currentState.Equals(ProgramState.Delete))
  104. {
  105. ChangeState(ProgramState.Idle);
  106. }
  107. else
  108. {
  109. ChangeState(ProgramState.Delete);
  110. }
  111. }
  112. }
  113. //get current Mouse positon within the right picture box
  114. private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
  115. {
  116. currentCursorPosition = ConvertCoordinates(new Point(e.X, e.Y));
  117. }
  118. //hold left mouse button to draw.
  119. private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
  120. {
  121. mousePressed = true;
  122. if (currentState.Equals(ProgramState.Draw))
  123. {
  124. currentLine = new List<Point>();
  125. }
  126. }
  127. //Lift left mouse button to stop drawing and add a new Line.
  128. private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
  129. {
  130. mousePressed = false;
  131. if (currentState.Equals(ProgramState.Draw))
  132. {
  133. Line newLine = new Line(currentLine, lineList.Count);
  134. lineList.Add(new Tuple<bool, Line>(true, newLine));
  135. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  136. }
  137. }
  138. //Button to create a new Canvas. Will create an empty image
  139. //which is the size of the left image, if there is one.
  140. //If there is no image loaded the canvas will be the size of the right picture box
  141. private void canvasButton_Click(object sender, EventArgs e)
  142. {
  143. DrawEmptyCanvas();
  144. //The following lines cannot be in DrawEmptyCanvas()
  145. isFilledMatrix = new bool[rightImage.Width, rightImage.Height];
  146. linesMatrix = new List<int>[rightImage.Width, rightImage.Height];
  147. }
  148. //add a Point on every tick to the Drawpath
  149. private void mouseTimer_Tick(object sender, EventArgs e)
  150. {
  151. if (currentState.Equals(ProgramState.Draw) && mousePressed)
  152. {
  153. currentLine.Add(currentCursorPosition);
  154. Line drawline = new Line(currentLine);
  155. drawline.DrawLine(graph);
  156. pictureBoxRight.Image = rightImage;
  157. }
  158. if (currentState.Equals(ProgramState.Delete) && mousePressed)
  159. {
  160. if (currentCursorPosition.X < rightImage.Width - 1 && currentCursorPosition.Y < rightImage.Height- 1
  161. && currentCursorPosition.X >= 0 && currentCursorPosition.Y >= 0)
  162. {
  163. cursorPositions.Add(currentCursorPosition);
  164. if (cursorPositions.Count > 1)
  165. {
  166. previousCursorPosition = cursorPositions[cursorPositions.Count - 2];
  167. if (isFilledMatrix[currentCursorPosition.X, currentCursorPosition.Y])
  168. {
  169. foreach (int lineid in linesMatrix[currentCursorPosition.X, currentCursorPosition.Y])
  170. {
  171. lineList[lineid] = new Tuple<bool, Line>(false, lineList[lineid].Item2);
  172. }
  173. RepopulateDeletionMatrixes();
  174. RedrawRightImage();
  175. }
  176. }
  177. }
  178. }
  179. }
  180. /***********************************/
  181. /*** HELPER FUNCTIONS START HERE ***/
  182. /***********************************/
  183. /// <summary>
  184. /// Creates an empty Canvas
  185. /// </summary>
  186. private void DrawEmptyCanvas()
  187. {
  188. if (leftImage == null)
  189. {
  190. rightImage = new Bitmap(pictureBoxRight.Width, pictureBoxRight.Height);
  191. graph = Graphics.FromImage(rightImage);
  192. graph.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
  193. pictureBoxRight.Image = rightImage;
  194. }
  195. else
  196. {
  197. rightImage = new Bitmap(leftImage.Width, leftImage.Height);
  198. graph = Graphics.FromImage(rightImage);
  199. graph.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
  200. pictureBoxRight.Image = rightImage;
  201. }
  202. this.Refresh();
  203. pictureBoxRight.Refresh();
  204. }
  205. /// <summary>
  206. /// Redraws all lines in lineList, for which their associated boolean value equals true.
  207. /// </summary>
  208. private void RedrawRightImage()
  209. {
  210. DrawEmptyCanvas();
  211. foreach (Tuple<bool, Line> lineBoolTuple in lineList)
  212. {
  213. if (lineBoolTuple.Item1)
  214. {
  215. lineBoolTuple.Item2.DrawLine(graph);
  216. }
  217. }
  218. pictureBoxRight.Refresh();
  219. }
  220. /// <summary>
  221. /// A helper function which handles tasks associated witch changing states,
  222. /// such as checking and unchecking buttons and changing the state.
  223. /// </summary>
  224. /// <param name="newState">The new state of the program</param>
  225. private void ChangeState(ProgramState newState)
  226. {
  227. switch (currentState)
  228. {
  229. case ProgramState.Draw:
  230. drawButton.CheckState = CheckState.Unchecked;
  231. mouseTimer.Enabled = false;
  232. break;
  233. case ProgramState.Delete:
  234. deleteButton.CheckState = CheckState.Unchecked;
  235. mouseTimer.Enabled = false;
  236. break;
  237. default:
  238. break;
  239. }
  240. switch (newState)
  241. {
  242. case ProgramState.Draw:
  243. drawButton.CheckState = CheckState.Checked;
  244. mouseTimer.Enabled = true;
  245. break;
  246. case ProgramState.Delete:
  247. deleteButton.CheckState = CheckState.Checked;
  248. mouseTimer.Enabled = true;
  249. break;
  250. default:
  251. break;
  252. }
  253. currentState = newState;
  254. pictureBoxRight.Refresh();
  255. }
  256. /// <summary>
  257. /// A function that calculates the coordinates of a point on a zoomed in image.
  258. /// </summary>
  259. /// <param name="">The position of the mouse cursor</param>
  260. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  261. private Point ConvertCoordinates(Point cursorPosition)
  262. {
  263. Point realCoordinates = new Point(5,3);
  264. if(pictureBoxRight.Image == null)
  265. {
  266. return cursorPosition;
  267. }
  268. int widthImage = pictureBoxRight.Image.Width;
  269. int heightImage = pictureBoxRight.Image.Height;
  270. int widthBox = pictureBoxRight.Width;
  271. int heightBox = pictureBoxRight.Height;
  272. float imageRatio = (float)widthImage / (float)heightImage;
  273. float containerRatio = (float)widthBox / (float)heightBox;
  274. if (imageRatio >= containerRatio)
  275. {
  276. //Image is wider than it is high
  277. float zoomFactor = (float)widthImage / (float)widthBox;
  278. float scaledHeight = heightImage / zoomFactor;
  279. float filler = (heightBox - scaledHeight) / 2;
  280. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  281. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  282. }
  283. else
  284. {
  285. //Image is higher than it is wide
  286. float zoomFactor = (float)heightImage / (float)heightBox;
  287. float scaledWidth = widthImage / zoomFactor;
  288. float filler = (widthBox - scaledWidth) / 2;
  289. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  290. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  291. }
  292. return realCoordinates;
  293. }
  294. /// <summary>
  295. /// A function that populates the matrixes needed for deletion detection with line data.
  296. /// </summary>
  297. private void RepopulateDeletionMatrixes()
  298. {
  299. if(rightImage != null)
  300. {
  301. isFilledMatrix = new bool[rightImage.Width,rightImage.Height];
  302. linesMatrix = new List<int>[rightImage.Width, rightImage.Height];
  303. foreach(Tuple<bool,Line> lineTuple in lineList)
  304. {
  305. if (lineTuple.Item1)
  306. {
  307. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  308. }
  309. }
  310. }
  311. }
  312. }
  313. }