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