Form1.cs 9.1 KB

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