Form1.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. Console.WriteLine(currentCursorPosition.ToString());
  114. }
  115. //hold left mouse button to draw.
  116. private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
  117. {
  118. mousePressed = true;
  119. if (currentState.Equals(ProgramState.Draw))
  120. {
  121. currentLine = new List<Point>();
  122. }
  123. }
  124. //Lift left mouse button to stop drawing and add a new Line.
  125. private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
  126. {
  127. mousePressed = false;
  128. if (currentState.Equals(ProgramState.Draw))
  129. {
  130. Line newLine = new Line(currentLine, lineList.Count);
  131. lineList.Add(new Tuple<bool, Line>(true, newLine));
  132. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  133. }
  134. }
  135. //Button to create a new Canvas. Will create an empty image
  136. //which is the size of the left image, if there is one.
  137. //If there is no image loaded the canvas will be the size of the right picture box
  138. private void canvasButton_Click(object sender, EventArgs e)
  139. {
  140. DrawEmptyCanvas();
  141. }
  142. //add a Point on every tick to the Drawpath
  143. private void mouseTimer_Tick(object sender, EventArgs e)
  144. {
  145. if (currentState.Equals(ProgramState.Draw) && mousePressed)
  146. {
  147. currentLine.Add(currentCursorPosition);
  148. Line drawline = new Line(currentLine);
  149. drawline.DrawLine(graph);
  150. pictureBoxRight.Image = rightImage;
  151. }
  152. if (currentState.Equals(ProgramState.Delete) && mousePressed)
  153. {
  154. if (currentCursorPosition.X < rightImage.Width - 1 && currentCursorPosition.Y < rightImage.Height- 1
  155. && currentCursorPosition.X >= 0 && currentCursorPosition.Y >= 0)
  156. {
  157. if (isFilledMatrix[currentCursorPosition.X, currentCursorPosition.Y])
  158. {
  159. foreach (int lineid in linesMatrix[currentCursorPosition.X, currentCursorPosition.Y])
  160. {
  161. lineList[lineid] = new Tuple<bool, Line>(false, lineList[lineid].Item2);
  162. }
  163. RepopulateDeletionMatrixes();
  164. RedrawRightImage();
  165. }
  166. }
  167. }
  168. }
  169. /***********************************/
  170. /*** HELPER FUNCTIONS START HERE ***/
  171. /***********************************/
  172. /// <summary>
  173. /// Creates an empty Canvas
  174. /// </summary>
  175. private void DrawEmptyCanvas()
  176. {
  177. if (leftImage == null)
  178. {
  179. rightImage = new Bitmap(pictureBoxRight.Width, pictureBoxRight.Height);
  180. graph = Graphics.FromImage(rightImage);
  181. graph.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
  182. pictureBoxRight.Image = rightImage;
  183. }
  184. else
  185. {
  186. rightImage = new Bitmap(leftImage.Width, leftImage.Height);
  187. graph = Graphics.FromImage(rightImage);
  188. graph.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
  189. pictureBoxRight.Image = rightImage;
  190. }
  191. isFilledMatrix = new bool[rightImage.Width, rightImage.Height];
  192. linesMatrix = new List<int>[rightImage.Width, rightImage.Height];
  193. this.Refresh();
  194. pictureBoxRight.Refresh();
  195. }
  196. /// <summary>
  197. /// Redraws all lines in lineList, for which their associated boolean value equals true.
  198. /// </summary>
  199. private void RedrawRightImage()
  200. {
  201. DrawEmptyCanvas();
  202. foreach (Tuple<bool, Line> lineBoolTuple in lineList)
  203. {
  204. if (lineBoolTuple.Item1)
  205. {
  206. lineBoolTuple.Item2.DrawLine(graph);
  207. }
  208. }
  209. pictureBoxRight.Refresh();
  210. }
  211. /// <summary>
  212. /// A helper function which handles tasks associated witch changing states,
  213. /// such as checking and unchecking buttons and changing the state.
  214. /// </summary>
  215. /// <param name="newState">The new state of the program</param>
  216. private void ChangeState(ProgramState newState)
  217. {
  218. switch (currentState)
  219. {
  220. case ProgramState.Draw:
  221. drawButton.CheckState = CheckState.Unchecked;
  222. mouseTimer.Enabled = false;
  223. break;
  224. case ProgramState.Delete:
  225. deleteButton.CheckState = CheckState.Unchecked;
  226. mouseTimer.Enabled = false;
  227. break;
  228. default:
  229. break;
  230. }
  231. switch (newState)
  232. {
  233. case ProgramState.Draw:
  234. drawButton.CheckState = CheckState.Checked;
  235. mouseTimer.Enabled = true;
  236. break;
  237. case ProgramState.Delete:
  238. deleteButton.CheckState = CheckState.Checked;
  239. mouseTimer.Enabled = true;
  240. break;
  241. default:
  242. break;
  243. }
  244. currentState = newState;
  245. pictureBoxRight.Refresh();
  246. }
  247. /// <summary>
  248. /// A function that calculates the coordinates of a point on a zoomed in image.
  249. /// </summary>
  250. /// <param name="">The position of the mouse cursor</param>
  251. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  252. private Point ConvertCoordinates(Point cursorPosition)
  253. {
  254. Point realCoordinates = new Point(5,3);
  255. if(pictureBoxRight.Image == null)
  256. {
  257. return cursorPosition;
  258. }
  259. int widthImage = pictureBoxRight.Image.Width;
  260. int heightImage = pictureBoxRight.Image.Height;
  261. int widthBox = pictureBoxRight.Width;
  262. int heightBox = pictureBoxRight.Height;
  263. float imageRatio = (float)widthImage / (float)heightImage;
  264. float containerRatio = (float)widthBox / (float)heightBox;
  265. if (imageRatio >= containerRatio)
  266. {
  267. //Image is wider than it is high
  268. float zoomFactor = (float)widthImage / (float)widthBox;
  269. float scaledHeight = heightImage / zoomFactor;
  270. float filler = (heightBox - scaledHeight) / 2;
  271. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  272. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  273. }
  274. else
  275. {
  276. //Image is higher than it is wide
  277. float zoomFactor = (float)heightImage / (float)heightBox;
  278. float scaledWidth = widthImage / zoomFactor;
  279. float filler = (widthBox - scaledWidth) / 2;
  280. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  281. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  282. }
  283. return realCoordinates;
  284. }
  285. /// <summary>
  286. /// A function that populates the matrixes needed for deletion detection with line data.
  287. /// </summary>
  288. private void RepopulateDeletionMatrixes()
  289. {
  290. if(rightImage != null)
  291. {
  292. isFilledMatrix = new bool[rightImage.Width,rightImage.Height];
  293. linesMatrix = new List<int>[rightImage.Width, rightImage.Height];
  294. foreach(Tuple<bool,Line> lineTuple in lineList)
  295. {
  296. if (lineTuple.Item1)
  297. {
  298. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  299. }
  300. }
  301. }
  302. }
  303. }
  304. }