Form1.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. }
  29. //Current Program State
  30. private ProgramState currentState;
  31. //Dialog to select a file.
  32. OpenFileDialog openFileDialogLeft = new OpenFileDialog();
  33. //Image loaded on the left
  34. Image leftImage = null;
  35. //Image on the right
  36. Image rightImage = null;
  37. //Current Line being Drawn
  38. List<Point> currentLine;
  39. //All Lines in the current session
  40. List<Tuple<bool,Line>> lineList = new List<Tuple<bool, Line>>();
  41. //Whether the Mouse is currently pressed in the rightPictureBox
  42. bool mousePressed = false;
  43. //The Position of the Cursor in the right picture box
  44. Point currentCursorPosition;
  45. //The graphic representation of the right image
  46. Graphics graph = null;
  47. /******************************************/
  48. /*** FORM SPECIFIC FUNCTIONS START HERE ***/
  49. /******************************************/
  50. private void Form1_Load(object sender, EventArgs e)
  51. {
  52. currentState = ProgramState.Idle;
  53. this.DoubleBuffered = true;
  54. }
  55. //Resize Function connected to the form resize event, will refresh the form when it is resized
  56. private void Form1_Resize(object sender, System.EventArgs e)
  57. {
  58. this.Refresh();
  59. }
  60. //Load button, will open an OpenFileDialog
  61. private void loadToolStripMenuItem_Click(object sender, EventArgs e)
  62. {
  63. openFileDialogLeft.Filter = "Image|*.jpg;*.png;*.jpeg";
  64. if(openFileDialogLeft.ShowDialog() == DialogResult.OK)
  65. {
  66. toolStripLoadStatus.Text = openFileDialogLeft.SafeFileName;
  67. leftImage = Image.FromFile(openFileDialogLeft.FileName);
  68. pictureBoxLeft.Image = leftImage;
  69. //Refresh the left image box when the content is changed
  70. this.Refresh();
  71. }
  72. }
  73. //Changes The State of the Program to drawing
  74. private void drawButton_Click(object sender, EventArgs e)
  75. {
  76. if(rightImage != null)
  77. {
  78. if (currentState.Equals(ProgramState.Draw))
  79. {
  80. ChangeState(ProgramState.Idle);
  81. }
  82. else
  83. {
  84. ChangeState(ProgramState.Draw);
  85. }
  86. }
  87. }
  88. //get current Mouse positon within the right picture box
  89. private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
  90. {
  91. currentCursorPosition = ConvertCoordinates(new Point(e.X, e.Y));
  92. }
  93. //hold left mouse button to draw.
  94. private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
  95. {
  96. mousePressed = true;
  97. if (currentState.Equals(ProgramState.Draw))
  98. {
  99. currentLine = new List<Point>();
  100. }
  101. }
  102. //Lift left mouse button to stop drawing.
  103. private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
  104. {
  105. mousePressed = false;
  106. if (currentState.Equals(ProgramState.Draw))
  107. {
  108. lineList.Add(new Tuple<bool, Line>(true, new Line(currentLine)));
  109. }
  110. }
  111. //Button to create a new Canvas. Will create an empty image
  112. //which is the size of the left image, if there is one.
  113. //If there is no image loaded the canvas will be the size of the right picture box
  114. private void canvasButton_Click(object sender, EventArgs e)
  115. {
  116. DrawEmptyCanvas();
  117. }
  118. //add a Point on every tick to the Drawpath
  119. private void drawTimer_Tick(object sender, EventArgs e)
  120. {
  121. if (currentState.Equals(ProgramState.Draw) && mousePressed)
  122. {
  123. currentLine.Add(currentCursorPosition);
  124. Line drawline = new Line(currentLine);
  125. drawline.DrawLine(graph);
  126. pictureBoxRight.Image = rightImage;
  127. }
  128. }
  129. /***********************************/
  130. /*** HELPER FUNCTIONS START HERE ***/
  131. /***********************************/
  132. /// <summary>
  133. /// Creates an empty Canvas
  134. /// </summary>
  135. private void DrawEmptyCanvas()
  136. {
  137. if (leftImage == null)
  138. {
  139. rightImage = new Bitmap(pictureBoxRight.Width, pictureBoxRight.Height);
  140. graph = Graphics.FromImage(rightImage);
  141. graph.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
  142. pictureBoxRight.Image = rightImage;
  143. }
  144. else
  145. {
  146. rightImage = new Bitmap(leftImage.Width, leftImage.Height);
  147. graph = Graphics.FromImage(rightImage);
  148. graph.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
  149. pictureBoxRight.Image = rightImage;
  150. }
  151. this.Refresh();
  152. pictureBoxRight.Refresh();
  153. }
  154. /// <summary>
  155. /// Redraws all lines in lineList, for which their associated boolean value equals true.
  156. /// </summary>
  157. private void RedrawRightImage()
  158. {
  159. DrawEmptyCanvas();
  160. foreach (Tuple<bool, Line> lineBoolTuple in lineList)
  161. {
  162. if (lineBoolTuple.Item1)
  163. {
  164. lineBoolTuple.Item2.DrawLine(graph);
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// A helper function which handles tasks associated witch changing states,
  170. /// such as checking and unchecking buttons and changing the state.
  171. /// </summary>
  172. /// <param name="newState">The new state of the program</param>
  173. private void ChangeState(ProgramState newState)
  174. {
  175. switch (currentState)
  176. {
  177. case ProgramState.Draw:
  178. drawButton.CheckState = CheckState.Unchecked;
  179. drawTimer.Enabled = false;
  180. break;
  181. default:
  182. break;
  183. }
  184. switch (newState)
  185. {
  186. case ProgramState.Draw:
  187. drawButton.CheckState = CheckState.Checked;
  188. drawTimer.Enabled = true;
  189. break;
  190. default:
  191. break;
  192. }
  193. currentState = newState;
  194. pictureBoxRight.Refresh();
  195. }
  196. /// <summary>
  197. /// A function that calculates the coordinates of a point on a zoomed in image.
  198. /// </summary>
  199. /// <param name="">The position of the mouse cursor</param>
  200. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  201. private Point ConvertCoordinates(Point cursorPosition)
  202. {
  203. Point realCoordinates = new Point(5,3);
  204. if(pictureBoxRight.Image == null)
  205. {
  206. return cursorPosition;
  207. }
  208. int widthImage = pictureBoxRight.Image.Width;
  209. int heightImage = pictureBoxRight.Image.Height;
  210. int widthBox = pictureBoxRight.Width;
  211. int heightBox = pictureBoxRight.Height;
  212. float imageRatio = (float)widthImage / (float)heightImage;
  213. float containerRatio = (float)widthBox / (float)heightBox;
  214. if (imageRatio >= containerRatio)
  215. {
  216. //Image is wider than it is high
  217. float zoomFactor = (float)widthImage / (float)widthBox;
  218. float scaledHeight = heightImage / zoomFactor;
  219. float filler = (heightBox - scaledHeight) / 2;
  220. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  221. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  222. }
  223. else
  224. {
  225. //Image is higher than it is wide
  226. float zoomFactor = (float)heightImage / (float)heightBox;
  227. float scaledWidth = widthImage / zoomFactor;
  228. float filler = (widthBox - scaledWidth) / 2;
  229. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  230. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  231. }
  232. return realCoordinates;
  233. }
  234. }
  235. }