Maxim_Leo

Untitled

Jun 21st, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using Microsoft.Win32;
  16. using System.IO;
  17. using System.Diagnostics;
  18.  
  19.  
  20. namespace VectorGraphicEditor
  21. {
  22.  
  23. public partial class MainWindow : Window
  24. {
  25.  
  26. Pen[] pen;
  27. List<List<Line>> freelines;
  28. int point_index = 0;
  29. Line line_cur = null;
  30. String mode = "line";
  31. Point currentPoint = new Point();
  32. int cnt = 0, l_cnt = 0;
  33. double x1, y1;
  34.  
  35. private Point startPoint;
  36. private Rectangle rectangle;
  37. private Ellipse circle;
  38. public MainWindow()
  39. {
  40. freelines = new List<List<Line>>(100);
  41. for (int i = 0; i < 100; i++)
  42. freelines.Add(new List<Line>(100));
  43. x1 = y1 = 0;
  44. InitializeComponent();
  45. pen = new Pen[100];
  46. for (int i = 0; i < 100; i++)
  47. pen[i] = new Pen(SystemColors.WindowFrameBrush, 2f);
  48. paintSurface.Background = new SolidColorBrush(Colors.White);
  49.  
  50.  
  51. }
  52.  
  53. private void paintSurface_MouseUp(object sender, MouseButtonEventArgs e)
  54. {
  55.  
  56.  
  57. circle = null;
  58. rectangle = null;
  59.  
  60. if(mode=="pen") l_cnt++;
  61. else cnt++;
  62.  
  63.  
  64. }
  65. private void paintSurface_MouseDown(object sender, MouseButtonEventArgs e)
  66. {
  67.  
  68. if (mode == "fill")
  69. {
  70. foreach (Rectangle rec in paintSurface.Children.OfType<Rectangle>())
  71. {
  72.  
  73. if ((e.GetPosition(paintSurface).X > Canvas.GetLeft(rec) && e.GetPosition(paintSurface).X < Canvas.GetRight(rec)) && (e.GetPosition(paintSurface).Y > Canvas.GetBottom(rec) && e.GetPosition(paintSurface).Y < Canvas.GetTop(rec)))
  74. {
  75. rec.Fill = pen[cnt].Brush;
  76. }
  77. }
  78. }
  79.  
  80. if (mode == "rectangle")
  81. {
  82. startPoint = e.GetPosition(paintSurface);
  83.  
  84. rectangle = new Rectangle
  85. {
  86. Stroke = pen[cnt].Brush,
  87. StrokeThickness = pen[cnt].Thickness
  88. };
  89. Canvas.SetLeft(rectangle, startPoint.X);
  90. Canvas.SetTop(rectangle, startPoint.Y);
  91. paintSurface.Children.Add(rectangle);
  92.  
  93. }
  94. else if (mode == "circle")
  95. {
  96. startPoint = e.GetPosition(paintSurface);
  97.  
  98. circle = new Ellipse
  99. {
  100. Stroke = pen[cnt].Brush,
  101. StrokeThickness = pen[cnt].Thickness
  102. };
  103. Canvas.SetLeft(circle, startPoint.X);
  104. Canvas.SetTop(circle, startPoint.Y);
  105. paintSurface.Children.Add(circle);
  106.  
  107. }
  108.  
  109.  
  110. else if (mode=="pen")
  111. {
  112.  
  113. x1 = e.GetPosition(this).X;
  114. y1 = e.GetPosition(this).Y;
  115. }
  116. else
  117. {
  118. double marker_x = -1, marker_y = -1;
  119.  
  120. if (e.LeftButton == MouseButtonState.Pressed)
  121. currentPoint = e.GetPosition(this);
  122. Line line;
  123. foreach (Line line1 in paintSurface.Children.OfType<Line>())
  124. {
  125. line = line1;
  126. if (Math.Abs(line.X1 - e.GetPosition(this).X) < 5 && Math.Abs(e.GetPosition(this).Y - 106 - line.Y1) < 5)
  127. {
  128. point_index = 0;
  129. marker_x = line.X1;
  130. marker_y = line.Y1;
  131. line_cur = line;
  132. }
  133. else if (Math.Abs(line.X2 - e.GetPosition(this).X) < 5 && Math.Abs(e.GetPosition(this).Y - 106 - line.Y2) < 5)
  134. {
  135. point_index = 1;
  136. marker_x = line.X2;
  137. marker_y = line.Y2;
  138. line_cur = line;
  139. }
  140. }
  141.  
  142. }
  143.  
  144. }
  145. private void paintSurface_MouseMove(object sender, MouseEventArgs e)
  146. {
  147. if (mode == "pen")
  148. {
  149. if (e.LeftButton == MouseButtonState.Pressed)
  150. {
  151.  
  152. Line line_ = new Line();
  153. line_.X1 = e.GetPosition(this).X;
  154. line_.Y1 = e.GetPosition(this).Y - 106;
  155. line_.X2 = x1;
  156. line_.Y2 = y1 - 106;
  157. line_.StrokeThickness = pen[cnt].Thickness;
  158. line_.Stroke = pen[cnt].Brush;
  159. line_.Fill = pen[cnt].Brush;
  160.  
  161. paintSurface.Children.Add(line_);
  162.  
  163. freelines[l_cnt].Add(line_);
  164.  
  165. x1 = e.GetPosition(this).X;
  166. y1 = e.GetPosition(this).Y;
  167. }
  168.  
  169.  
  170.  
  171. }
  172. else
  173. {
  174. double marker_x = -1, marker_y = -1;
  175. if (e.LeftButton == MouseButtonState.Pressed)
  176. {
  177. Line line = null;
  178. if (line_cur != null)
  179. {
  180. if (point_index == 0)
  181. {
  182. line_cur.X1 = e.GetPosition(this).X;
  183. line_cur.Y1 = e.GetPosition(this).Y - 106;
  184. marker_x = line_cur.X1;
  185. marker_y = line_cur.Y1;
  186. Console.WriteLine("Правлю точку 0");
  187. }
  188. else
  189. {
  190. line_cur.X2 = e.GetPosition(this).X;
  191. line_cur.Y2 = e.GetPosition(this).Y - 106;
  192. marker_x = line_cur.X2;
  193. marker_y = line_cur.Y2;
  194. Console.WriteLine("Правлю точку 1");
  195. }
  196.  
  197.  
  198. for (int i = 0; i < 10; i++)
  199. foreach (System.Windows.Shapes.Rectangle rect1 in paintSurface.Children.OfType<System.Windows.Shapes.Rectangle>())
  200. {
  201. if (rect1.Width == 10)
  202. {
  203. paintSurface.Children.Remove(rect1);
  204. break;
  205. }
  206.  
  207. }
  208.  
  209. System.Windows.Shapes.Rectangle rect;
  210. rect = new System.Windows.Shapes.Rectangle();
  211. rect.Stroke = new SolidColorBrush(Colors.Red);
  212. rect.Fill = new SolidColorBrush(Colors.Transparent);
  213. rect.Width = 10;
  214. rect.Height = 10;
  215. Canvas.SetLeft(rect, marker_x - 5);
  216. Canvas.SetTop(rect, marker_y - 5);
  217. paintSurface.Children.Add(rect);
  218. return;
  219. }
  220. else
  221. {
  222. foreach (Line line1 in paintSurface.Children.OfType<Line>())
  223. {
  224. line = line1;
  225. if (line.Name == "line_" + cnt)
  226. {
  227. break;
  228. }
  229. else line = null;
  230. }
  231. }
  232. if (line_cur == null)
  233. {
  234. if (line == null)
  235. {
  236. if (mode == "rectangle")
  237. {
  238. if (e.LeftButton == MouseButtonState.Released || rectangle == null)
  239. return;
  240.  
  241. var pos = e.GetPosition(paintSurface);
  242.  
  243. var x = Math.Min(pos.X, startPoint.X);
  244. var y = Math.Min(pos.Y, startPoint.Y);
  245.  
  246. var w = Math.Max(pos.X, startPoint.X) - x;
  247. var h = Math.Max(pos.Y, startPoint.Y) - y;
  248.  
  249. rectangle.Width = w;
  250. rectangle.Height = h;
  251.  
  252. Canvas.SetLeft(rectangle, x);
  253. Canvas.SetTop(rectangle, y);
  254.  
  255. }
  256.  
  257. else if (mode == "circle")
  258. {
  259. if (e.LeftButton == MouseButtonState.Released || circle == null)
  260. return;
  261.  
  262. var pos = e.GetPosition(paintSurface);
  263.  
  264. var x = Math.Min(pos.X, startPoint.X);
  265. var y = Math.Min(pos.Y, startPoint.Y);
  266.  
  267. var w = Math.Max(pos.X, startPoint.X) - x;
  268. var h = Math.Max(pos.Y, startPoint.Y) - y;
  269.  
  270. circle.Width = w;
  271. circle.Height = h;
  272.  
  273. Canvas.SetLeft(circle, x);
  274. Canvas.SetTop(circle, y);
  275.  
  276. }
  277. else if(mode=="line")
  278. {
  279. line = new Line();
  280. line.Stroke = SystemColors.WindowFrameBrush;
  281. line.X1 = currentPoint.X;
  282. line.Y1 = currentPoint.Y - 106;
  283. line.X2 = e.GetPosition(this).X;
  284. line.Y2 = e.GetPosition(this).Y - 106;
  285.  
  286. currentPoint = e.GetPosition(this);
  287. line.Name = "line_" + cnt;
  288. line.Stroke = pen[cnt].Brush;
  289. line.StrokeThickness = pen[cnt].Thickness;
  290. paintSurface.Children.Add(line);
  291. }
  292.  
  293.  
  294. }
  295. else
  296. {
  297. line.X2 = e.GetPosition(this).X;
  298. line.Y2 = e.GetPosition(this).Y - 106;
  299. paintSurface.InvalidateVisual();
  300. }
  301. }
  302. }
  303. else
  304. {
  305. Line line = null;
  306. foreach (Line line1 in paintSurface.Children.OfType<Line>())
  307. {
  308.  
  309. line = line1;
  310. if ((Math.Abs(line.X1 - e.GetPosition(this).X) < 5) && (Math.Abs(e.GetPosition(this).Y - 106 - line.Y1) < 5))
  311. {
  312. point_index = 0;
  313. marker_x = line.X1;
  314. marker_y = line.Y1;
  315. line_cur = line;
  316. }
  317. else if ((Math.Abs(line.X2 - e.GetPosition(this).X)) < 5 && (Math.Abs(e.GetPosition(this).Y - 106 - line.Y2) < 5))
  318. {
  319. point_index = 1;
  320. marker_x = line.X2;
  321. marker_y = line.Y2;
  322. line_cur = line;
  323. }
  324. }
  325. }
  326. if (marker_x != -1)
  327. {
  328. System.Windows.Shapes.Rectangle rect;
  329. rect = new System.Windows.Shapes.Rectangle();
  330. rect.Stroke = new SolidColorBrush(Colors.Red);
  331. rect.Fill = new SolidColorBrush(Colors.Transparent);
  332. rect.Width = 10;
  333. rect.Height = 10;
  334. Canvas.SetLeft(rect, marker_x - 5);
  335. Canvas.SetTop(rect, marker_y - 5);
  336. paintSurface.Children.Add(rect);
  337. }
  338. else
  339. {
  340.  
  341. foreach (Rectangle rect in paintSurface.Children.OfType<Rectangle>())
  342. {
  343. if (rect.Width == 10)
  344. {
  345. paintSurface.Children.Remove(rect);
  346. line_cur = null;
  347. paintSurface.InvalidateVisual();
  348. break;
  349. }
  350.  
  351. }
  352.  
  353.  
  354. }
  355. }
  356.  
  357.  
  358. }
  359.  
  360. private void Color_Click(object sender, RoutedEventArgs e)
  361. {
  362. for (int i = cnt; i < 100; i++)
  363. pen[i].Brush = ((Button)sender).Background;
  364. }
  365.  
  366. private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  367. {
  368. for (int i = cnt; i < 100; i++)
  369. pen[i].Thickness = slider.Value;
  370. }
  371.  
  372. private void NewFile(object sender, RoutedEventArgs e)
  373. {
  374. paintSurface.Background = new SolidColorBrush(Colors.White);
  375. paintSurface.Children.Clear();
  376. }
  377. private void OpenFile(object sender, RoutedEventArgs e)
  378. {
  379. Microsoft.Win32.OpenFileDialog dl1 = new Microsoft.Win32.OpenFileDialog();
  380. dl1.FileName = "MYFileSave";
  381. dl1.DefaultExt = ".png";
  382. dl1.Filter = "Image documents (.png)|*.png";
  383. Nullable<bool> result = dl1.ShowDialog();
  384.  
  385. if (result == true)
  386. {
  387. string filename = dl1.FileName;
  388. ImageBrush brush = new ImageBrush();
  389. brush.ImageSource = new BitmapImage(new Uri(@filename, UriKind.Relative));
  390. paintSurface.Background = brush;
  391. }
  392. }
  393. public static void ToImageSource(Canvas canvas, string filename)
  394. {
  395. RenderTargetBitmap bmp = new RenderTargetBitmap((int)canvas.ActualWidth, (int)canvas.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);
  396. canvas.Measure(new Size((int)canvas.ActualWidth, (int)canvas.ActualHeight));
  397. canvas.Arrange(new Rect(new Size((int)canvas.ActualWidth, (int)canvas.ActualHeight)));
  398. bmp.Render(canvas);
  399. PngBitmapEncoder encoder = new PngBitmapEncoder();
  400. encoder.Frames.Add(BitmapFrame.Create(bmp));
  401.  
  402. using (FileStream file = File.Create(filename))
  403. {
  404. encoder.Save(file);
  405. }
  406. }
  407. private void SaveFile(object sender, RoutedEventArgs e)
  408. {
  409. Microsoft.Win32.SaveFileDialog saveimg = new Microsoft.Win32.SaveFileDialog();
  410. //Canvas can = new Canvas(); // канвас уже есть
  411. saveimg.DefaultExt = ".PNG";
  412. saveimg.Filter = "Image (.PNG)|*.PNG";
  413. if (saveimg.ShowDialog() == true)
  414. {
  415. ToImageSource(paintSurface, saveimg.FileName); //DragArena - имя имеющегося канваса
  416. }
  417. }
  418.  
  419. private void Exit(object sender, RoutedEventArgs e)
  420. {
  421. SaveFile(sender, e);
  422. Close();
  423. }
  424.  
  425. private void line_Click(object sender, RoutedEventArgs e)
  426. {
  427.  
  428. mode = "line";
  429.  
  430. }
  431.  
  432. private void circle_Click(object sender, RoutedEventArgs e)
  433. {
  434.  
  435. mode = "circle";
  436. }
  437.  
  438. private void rectangle_Click(object sender, RoutedEventArgs e)
  439. {
  440.  
  441. mode = "rectangle";
  442. }
  443.  
  444. private void triangle1_Click(object sender, RoutedEventArgs e)
  445. {
  446.  
  447. mode = "triangle";
  448. }
  449. private void bucket_Click(object sender, RoutedEventArgs e)
  450. {
  451. mode = "fill";
  452. }
  453. private void eraser_Click(object sender, RoutedEventArgs e)
  454. {
  455.  
  456. mode = "pen";
  457.  
  458. for (int i = cnt; i < 100; i++)
  459. {
  460. pen[i].Brush = new SolidColorBrush(Colors.White);
  461. pen[i].Thickness = 3f;
  462. }
  463.  
  464. }
  465.  
  466.  
  467.  
  468. private void undo_Click_(object sender, RoutedEventArgs e)
  469. {
  470.  
  471. if (paintSurface.Children.Count != 0)
  472. paintSurface.Children.RemoveAt(paintSurface.Children.Count - 1);
  473. else return;
  474. }
  475.  
  476. private void Pencil_Click_(object sender, RoutedEventArgs e)
  477. {
  478. mode = "pen";
  479.  
  480. for (int i = 0; i < 100; i++)
  481. pen[i] = new Pen(SystemColors.WindowFrameBrush, 2f);
  482. }
  483.  
  484.  
  485.  
  486. }
  487.  
  488. }
  489.  
Advertisement
Add Comment
Please, Sign In to add comment