Form1.cs 12 KB

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