Form1.cs 12 KB

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