Form1.cs 9.7 KB

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