Form1.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  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. /**********************************/
  22. /*** CLASS VARIABLES START HERE ***/
  23. /**********************************/
  24. //Different Program States
  25. public enum ProgramState
  26. {
  27. Idle,
  28. Draw,
  29. Delete
  30. }
  31. //Current Program State
  32. private ProgramState currentState;
  33. //Dialog to select a file.
  34. OpenFileDialog openFileDialogLeft = new OpenFileDialog();
  35. //Image loaded on the left
  36. Image leftImage = null;
  37. //Image on the right
  38. Image rightImage = null;
  39. //Current Line being Drawn
  40. List<Point> currentLine;
  41. //All Lines in the current session
  42. List<Tuple<bool,Line>> lineList = new List<Tuple<bool, Line>>();
  43. //Whether the Mouse is currently pressed in the rightPictureBox
  44. bool mousePressed = false;
  45. //The Position of the Cursor in the right picture box
  46. Point currentCursorPosition;
  47. //The Previous Cursor Position in the right picture box
  48. Point previousCursorPosition;
  49. //Queue for the cursorPositions
  50. Queue<Point> cursorPositions = new Queue<Point>();
  51. //The graphic representation of the right image
  52. Graphics graph = null;
  53. //Deletion Matrixes for checking postions of lines in the image
  54. bool[,] isFilledMatrix;
  55. HashSet<int>[,] linesMatrix;
  56. //Size of deletion area
  57. uint deletionSize = 2;
  58. //Dialog to save a file
  59. SaveFileDialog saveFileDialogRight = new SaveFileDialog();
  60. /******************************************/
  61. /*** FORM SPECIFIC FUNCTIONS START HERE ***/
  62. /******************************************/
  63. private void Form1_Load(object sender, EventArgs e)
  64. {
  65. currentState = ProgramState.Idle;
  66. this.DoubleBuffered = true;
  67. }
  68. //Resize Function connected to the form resize event, will refresh the form when it is resized
  69. private void Form1_Resize(object sender, System.EventArgs e)
  70. {
  71. this.Refresh();
  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)|*.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. }
  85. }
  86. //Save button, will open an SaveFileDialog
  87. private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  88. {
  89. /*if (rightImage != null)
  90. {
  91. saveFileDialogRight.Filter = "Image|*.jpg;*.png;*.jpeg|" + "All files (*.*)|*.*";
  92. ImageFormat format = ImageFormat.Jpeg;
  93. if (saveFileDialogRight.ShowDialog() == DialogResult.OK)
  94. {
  95. switch (saveFileDialogRight.Filter)
  96. {
  97. case ".png":
  98. format = ImageFormat.Png;
  99. break;
  100. case ".bmp":
  101. format = ImageFormat.Bmp;
  102. break;
  103. }
  104. pictureBoxRight.Image.Save(saveFileDialogRight.FileName, format);
  105. }
  106. }
  107. else
  108. {
  109. MessageBox.Show("The right picture box can't be empty");
  110. }
  111. */
  112. if (rightImage != null)
  113. {
  114. saveFileDialogRight.Filter = "Image|*.jpg;*.png;*.jpeg|" + "All files (*.*)|*.*";
  115. ImageFormat format = ImageFormat.Jpeg;
  116. if (saveFileDialogRight.ShowDialog() == DialogResult.OK)
  117. {
  118. switch (saveFileDialogRight.Filter)
  119. {
  120. case ".png":
  121. format = ImageFormat.Png;
  122. break;
  123. case ".bmp":
  124. format = ImageFormat.Bmp;
  125. break;
  126. }
  127. pictureBoxRight.Image.Save(saveFileDialogRight.FileName, format);
  128. }
  129. }
  130. else
  131. {
  132. MessageBox.Show("The right picture box can't be empty");
  133. }
  134. }
  135. //Changes the state of the program to drawing
  136. private void drawButton_Click(object sender, EventArgs e)
  137. {
  138. if(rightImage != null)
  139. {
  140. if (currentState.Equals(ProgramState.Draw))
  141. {
  142. ChangeState(ProgramState.Idle);
  143. }
  144. else
  145. {
  146. ChangeState(ProgramState.Draw);
  147. }
  148. }
  149. }
  150. //Changes the state of the program to deletion
  151. private void deleteButton_Click(object sender, EventArgs e)
  152. {
  153. if (rightImage != null)
  154. {
  155. if (currentState.Equals(ProgramState.Delete))
  156. {
  157. ChangeState(ProgramState.Idle);
  158. }
  159. else
  160. {
  161. ChangeState(ProgramState.Delete);
  162. }
  163. }
  164. }
  165. //get current Mouse positon within the right picture box
  166. private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
  167. {
  168. currentCursorPosition = ConvertCoordinates(new Point(e.X, e.Y));
  169. }
  170. //hold left mouse button to draw.
  171. private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
  172. {
  173. mousePressed = true;
  174. if (currentState.Equals(ProgramState.Draw))
  175. {
  176. currentLine = new List<Point>();
  177. }
  178. }
  179. //when the picture box is clicked, add a point to the current line. Occurs f.ex. when using drawing tablets
  180. private void pictureBoxRight_Click(object sender, EventArgs e)
  181. {
  182. if (currentState.Equals(ProgramState.Draw))
  183. {
  184. List<Point> singlePoint = new List<Point> { currentCursorPosition };
  185. Line singlePointLine = new Line(singlePoint, lineList.Count);
  186. singlePointLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  187. singlePointLine.DrawLine(graph);
  188. pictureBoxRight.Image = rightImage;
  189. }
  190. }
  191. //Lift left mouse button to stop drawing and add a new Line.
  192. private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
  193. {
  194. mousePressed = false;
  195. if (currentState.Equals(ProgramState.Draw) && currentLine.Count > 0)
  196. {
  197. Line newLine = new Line(currentLine, lineList.Count);
  198. lineList.Add(new Tuple<bool, Line>(true, newLine));
  199. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  200. }
  201. }
  202. //Button to create a new Canvas. Will create an empty image
  203. //which is the size of the left image, if there is one.
  204. //If there is no image loaded the canvas will be the size of the right picture box
  205. private void canvasButton_Click(object sender, EventArgs e)
  206. {
  207. DrawEmptyCanvas();
  208. //The following lines cannot be in DrawEmptyCanvas()
  209. isFilledMatrix = new bool[rightImage.Width, rightImage.Height];
  210. linesMatrix = new HashSet<int>[rightImage.Width, rightImage.Height];
  211. }
  212. //add a Point on every tick to the Drawpath
  213. private void mouseTimer_Tick(object sender, EventArgs e)
  214. {
  215. cursorPositions.Enqueue(currentCursorPosition);
  216. previousCursorPosition = cursorPositions.Dequeue();
  217. if (currentState.Equals(ProgramState.Draw) && mousePressed)
  218. {
  219. currentLine.Add(currentCursorPosition);
  220. Line drawline = new Line(currentLine);
  221. drawline.DrawLine(graph);
  222. pictureBoxRight.Image = rightImage;
  223. }
  224. if (currentState.Equals(ProgramState.Delete) && mousePressed)
  225. {
  226. List<Point> uncheckedPoints = Line.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
  227. foreach (Point currPoint in uncheckedPoints)
  228. {
  229. HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionSize);
  230. if (linesToDelete.Count > 0)
  231. {
  232. foreach (int lineID in linesToDelete)
  233. {
  234. lineList[lineID] = new Tuple<bool, Line>(false, lineList[lineID].Item2);
  235. }
  236. RepopulateDeletionMatrixes();
  237. RedrawRightImage();
  238. }
  239. }
  240. }
  241. }
  242. /***********************************/
  243. /*** HELPER FUNCTIONS START HERE ***/
  244. /***********************************/
  245. /// <summary>
  246. /// Creates an empty Canvas
  247. /// </summary>
  248. private void DrawEmptyCanvas()
  249. {
  250. if (leftImage == null)
  251. {
  252. rightImage = new Bitmap(pictureBoxRight.Width, pictureBoxRight.Height);
  253. graph = Graphics.FromImage(rightImage);
  254. graph.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
  255. pictureBoxRight.Image = rightImage;
  256. }
  257. else
  258. {
  259. rightImage = new Bitmap(leftImage.Width, leftImage.Height);
  260. graph = Graphics.FromImage(rightImage);
  261. graph.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
  262. pictureBoxRight.Image = rightImage;
  263. }
  264. this.Refresh();
  265. pictureBoxRight.Refresh();
  266. }
  267. /// <summary>
  268. /// Redraws all lines in lineList, for which their associated boolean value equals true.
  269. /// </summary>
  270. private void RedrawRightImage()
  271. {
  272. DrawEmptyCanvas();
  273. foreach (Tuple<bool, Line> lineBoolTuple in lineList)
  274. {
  275. if (lineBoolTuple.Item1)
  276. {
  277. lineBoolTuple.Item2.DrawLine(graph);
  278. }
  279. }
  280. pictureBoxRight.Refresh();
  281. }
  282. /// <summary>
  283. /// A helper function which handles tasks associated witch changing states,
  284. /// such as checking and unchecking buttons and changing the state.
  285. /// </summary>
  286. /// <param name="newState">The new state of the program</param>
  287. private void ChangeState(ProgramState newState)
  288. {
  289. switch (currentState)
  290. {
  291. case ProgramState.Draw:
  292. drawButton.CheckState = CheckState.Unchecked;
  293. mouseTimer.Enabled = false;
  294. break;
  295. case ProgramState.Delete:
  296. deleteButton.CheckState = CheckState.Unchecked;
  297. mouseTimer.Enabled = false;
  298. break;
  299. default:
  300. break;
  301. }
  302. switch (newState)
  303. {
  304. case ProgramState.Draw:
  305. drawButton.CheckState = CheckState.Checked;
  306. mouseTimer.Enabled = true;
  307. break;
  308. case ProgramState.Delete:
  309. deleteButton.CheckState = CheckState.Checked;
  310. mouseTimer.Enabled = true;
  311. break;
  312. default:
  313. break;
  314. }
  315. currentState = newState;
  316. pictureBoxRight.Refresh();
  317. }
  318. /// <summary>
  319. /// A function that calculates the coordinates of a point on a zoomed in image.
  320. /// </summary>
  321. /// <param name="">The position of the mouse cursor</param>
  322. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  323. private Point ConvertCoordinates(Point cursorPosition)
  324. {
  325. Point realCoordinates = new Point(5,3);
  326. if(pictureBoxRight.Image == null)
  327. {
  328. return cursorPosition;
  329. }
  330. int widthImage = pictureBoxRight.Image.Width;
  331. int heightImage = pictureBoxRight.Image.Height;
  332. int widthBox = pictureBoxRight.Width;
  333. int heightBox = pictureBoxRight.Height;
  334. float imageRatio = (float)widthImage / (float)heightImage;
  335. float containerRatio = (float)widthBox / (float)heightBox;
  336. if (imageRatio >= containerRatio)
  337. {
  338. //Image is wider than it is high
  339. float zoomFactor = (float)widthImage / (float)widthBox;
  340. float scaledHeight = heightImage / zoomFactor;
  341. float filler = (heightBox - scaledHeight) / 2;
  342. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  343. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  344. }
  345. else
  346. {
  347. //Image is higher than it is wide
  348. float zoomFactor = (float)heightImage / (float)heightBox;
  349. float scaledWidth = widthImage / zoomFactor;
  350. float filler = (widthBox - scaledWidth) / 2;
  351. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  352. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  353. }
  354. return realCoordinates;
  355. }
  356. /// <summary>
  357. /// A function that populates the matrixes needed for deletion detection with line data.
  358. /// </summary>
  359. private void RepopulateDeletionMatrixes()
  360. {
  361. if(rightImage != null)
  362. {
  363. isFilledMatrix = new bool[rightImage.Width,rightImage.Height];
  364. linesMatrix = new HashSet<int>[rightImage.Width, rightImage.Height];
  365. foreach(Tuple<bool,Line> lineTuple in lineList)
  366. {
  367. if (lineTuple.Item1)
  368. {
  369. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  370. }
  371. }
  372. }
  373. }
  374. /// <summary>
  375. /// A function that checks the deletion matrixes at a certain point
  376. /// and returns all Line ids at that point and in a square around it in a certain range.
  377. /// </summary>
  378. /// <param name="p">The point around which to check.</param>
  379. /// <param name="range">The range around the point. If range is 0, only the point is checked.</param>
  380. /// <returns>A List of all lines.</returns>
  381. private HashSet<int> CheckDeletionMatrixesAroundPoint(Point p, uint range)
  382. {
  383. HashSet<int> returnSet = new HashSet<int>();
  384. if (p.X >= 0 && p.Y >= 0 && p.X < rightImage.Width && p.Y < rightImage.Height)
  385. {
  386. if (isFilledMatrix[p.X, p.Y])
  387. {
  388. returnSet.UnionWith(linesMatrix[p.X, p.Y]);
  389. }
  390. }
  391. for (int x_mod = (int)range*(-1); x_mod < range; x_mod++)
  392. {
  393. for (int y_mod = (int)range * (-1); y_mod < range; y_mod++)
  394. {
  395. if (p.X + x_mod >= 0 && p.Y + y_mod >= 0 && p.X + x_mod < rightImage.Width && p.Y + y_mod < rightImage.Height)
  396. {
  397. if (isFilledMatrix[p.X + x_mod, p.Y + y_mod])
  398. {
  399. returnSet.UnionWith(linesMatrix[p.X + x_mod, p.Y + y_mod]);
  400. }
  401. }
  402. }
  403. }
  404. return returnSet;
  405. }
  406. }
  407. }