Maxim_Leo

Untitled

Jun 16th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.78 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.Reflection.Metadata;
  18.  
  19. public static class RenderVisualService
  20. {
  21. private const double defaultDpi = 96.0;
  22.  
  23. private static BitmapSource GetRenderTargetBitmapFromControl(Visual targetControl, double dpi = defaultDpi)
  24. {
  25. if (targetControl == null) return null;
  26.  
  27. var bounds = VisualTreeHelper.GetDescendantBounds(targetControl);
  28. var renderTargetBitmap = new RenderTargetBitmap((int)(bounds.Width * dpi / 96.0),
  29. (int)(bounds.Height * dpi / 96.0),
  30. dpi,
  31. dpi,
  32. PixelFormats.Pbgra32);
  33.  
  34. var drawingVisual = new DrawingVisual();
  35.  
  36. using (var drawingContext = drawingVisual.RenderOpen())
  37. {
  38. var visualBrush = new VisualBrush(targetControl);
  39. drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
  40. }
  41.  
  42. renderTargetBitmap.Render(drawingVisual);
  43. return renderTargetBitmap;
  44. }
  45. public static void RenderToPNGFile(Visual targetControl, string filename)
  46. {
  47. var renderTargetBitmap = GetRenderTargetBitmapFromControl(targetControl);
  48.  
  49. var encoder = new PngBitmapEncoder();
  50. encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
  51.  
  52. var result = new BitmapImage();
  53.  
  54. try
  55. {
  56. using (var fileStream = new FileStream(filename, FileMode.Create))
  57. {
  58. encoder.Save(fileStream);
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. System.Diagnostics.Debug.WriteLine($"There was an error saving the file: {ex.Message}");
  64. }
  65. }
  66. }
  67.  
  68.  
  69.  
  70.  
  71. namespace VectorGraphicEditor
  72. {
  73. public partial class MainWindow : Window
  74. {
  75. Pen[] pen;
  76. Line[] lines;
  77. Line[][] lines1;
  78. int point_index = 0;
  79. Line line_cur = null;
  80. bool pen_mode = false;
  81. String mode="";
  82. Point currentPoint = new Point();
  83. int cnt = 0,l_cnt=0;
  84. double x1, y1;
  85.  
  86. private Point startPoint;
  87. private Rectangle rect;
  88. private Ellipse circle;
  89. public MainWindow()
  90. {
  91. lines = new Line[100];
  92. lines1 = new Line[100][];
  93. for(int i = 0; i < lines1.Length; i++)
  94. {
  95. lines1[i] = new Line[100];
  96. }
  97. x1 = y1 = 0;
  98. InitializeComponent();
  99. pen = new Pen[100];
  100. for (int i = 0; i < 100; i++)
  101. pen[i]=new Pen(SystemColors.WindowFrameBrush,2f);
  102. paintSurface.Background = new SolidColorBrush(Colors.White);
  103.  
  104.  
  105. }
  106.  
  107. private void paintSurface_MouseUp(object sender, MouseButtonEventArgs e)
  108. {
  109. circle = null;
  110. rect = null;
  111.  
  112. if (!pen_mode)
  113. cnt++;
  114. else l_cnt++;
  115.  
  116.  
  117.  
  118. }
  119. private void paintSurface_MouseDown(object sender,MouseButtonEventArgs e)
  120. {
  121.  
  122. if (mode == "rectangle")
  123. {
  124. startPoint = e.GetPosition(paintSurface);
  125.  
  126. rect = new Rectangle
  127. {
  128. Stroke = pen[cnt].Brush,
  129. StrokeThickness = pen[cnt].Thickness
  130. };
  131. Canvas.SetLeft(rect, startPoint.X);
  132. Canvas.SetTop(rect, startPoint.Y);
  133. paintSurface.Children.Add(rect);
  134. }
  135. else if (mode == "circle")
  136. {
  137. startPoint = e.GetPosition(paintSurface);
  138.  
  139. circle = new Ellipse
  140. {
  141. Stroke = pen[cnt].Brush,
  142. StrokeThickness = pen[cnt].Thickness
  143. };
  144. Canvas.SetLeft(rect, startPoint.X);
  145. Canvas.SetTop(rect, startPoint.Y);
  146. paintSurface.Children.Add(circle);
  147. }
  148. else if (pen_mode)
  149. {
  150.  
  151. x1 = e.GetPosition(this).X;
  152. y1 = e.GetPosition(this).Y;
  153. }
  154. else
  155. {
  156. double marker_x = -1, marker_y = -1;
  157.  
  158. if (e.LeftButton == MouseButtonState.Pressed)
  159. currentPoint = e.GetPosition(this);
  160. Line line;
  161. foreach (Line line1 in paintSurface.Children.OfType<Line>())
  162. {
  163. line = line1;
  164. if (Math.Abs(line.X1 - e.GetPosition(this).X) < 5 && Math.Abs(e.GetPosition(this).Y - 106 - line.Y1) < 5)
  165. {
  166. point_index = 0;
  167. marker_x = line.X1;
  168. marker_y = line.Y1;
  169. line_cur = line;
  170. }
  171. else if (Math.Abs(line.X2 - e.GetPosition(this).X) < 5 && Math.Abs(e.GetPosition(this).Y - 106 - line.Y2) < 5)
  172. {
  173. point_index = 1;
  174. marker_x = line.X2;
  175. marker_y = line.Y2;
  176. line_cur = line;
  177. }
  178. }
  179.  
  180. }
  181.  
  182. }
  183. private void paintSurface_MouseMove(object sender,MouseEventArgs e)
  184. {
  185. if (pen_mode)
  186. {
  187. if (e.LeftButton == MouseButtonState.Pressed)
  188. {
  189. if (mode == "line")
  190. {
  191. Line line_ = new Line();
  192. line_.X1 = e.GetPosition(this).X;
  193. line_.Y1 = e.GetPosition(this).Y - 106;
  194. line_.X2 = x1;
  195. line_.Y2 = y1 - 106;
  196. line_.StrokeThickness = pen[cnt].Thickness;
  197. line_.Stroke = pen[cnt].Brush;
  198.  
  199. paintSurface.Children.Add(line_);
  200.  
  201. for (int i = 0; i < lines1[l_cnt].Length - 1; i++)
  202. {
  203.  
  204. if (lines1[l_cnt][i] == null)
  205. {
  206. lines1[l_cnt][i] = line_;
  207. break;
  208. }
  209.  
  210. }
  211. }
  212.  
  213. else if (mode == "rectangle")
  214. {
  215. if (e.LeftButton == MouseButtonState.Released || rect == null)
  216. return;
  217.  
  218. var pos = e.GetPosition(paintSurface);
  219.  
  220. var x = Math.Min(pos.X, startPoint.X);
  221. var y = Math.Min(pos.Y, startPoint.Y);
  222.  
  223. var w = Math.Max(pos.X, startPoint.X) - x;
  224. var h = Math.Max(pos.Y, startPoint.Y) - y;
  225.  
  226. rect.Width = w;
  227. rect.Height = h;
  228.  
  229. Canvas.SetLeft(rect, x);
  230. Canvas.SetTop(rect, y);
  231.  
  232. }
  233. else if (mode == "circle")
  234. {
  235. if (e.LeftButton == MouseButtonState.Released || circle == null)
  236. return;
  237.  
  238. var pos = e.GetPosition(paintSurface);
  239.  
  240. var x = Math.Min(pos.X, startPoint.X);
  241. var y = Math.Min(pos.Y, startPoint.Y);
  242.  
  243. var w = Math.Max(pos.X, startPoint.X) - x;
  244. var h = Math.Max(pos.Y, startPoint.Y) - y;
  245.  
  246. rect.Width = w;
  247. rect.Height = h;
  248.  
  249. Canvas.SetLeft(circle, x);
  250. Canvas.SetTop(circle, y);
  251.  
  252. }
  253.  
  254.  
  255. x1 = e.GetPosition(this).X;
  256. y1 = e.GetPosition(this).Y;
  257. }
  258.  
  259.  
  260.  
  261. }
  262. else {
  263. double marker_x = -1, marker_y = -1;
  264. if (e.LeftButton == MouseButtonState.Pressed)
  265. {
  266. Line line = null;
  267. if (line_cur != null)
  268. {
  269. if (point_index == 0)
  270. {
  271. line_cur.X1 = e.GetPosition(this).X;
  272. line_cur.Y1 = e.GetPosition(this).Y - 106;
  273. marker_x = line_cur.X1;
  274. marker_y = line_cur.Y1;
  275. Console.WriteLine("Правлю точку 0");
  276. }
  277. else
  278. {
  279. line_cur.X2 = e.GetPosition(this).X;
  280. line_cur.Y2 = e.GetPosition(this).Y - 106;
  281. marker_x = line_cur.X2;
  282. marker_y = line_cur.Y2;
  283. Console.WriteLine("Правлю точку 1");
  284. }
  285. for (int i = 0; i < 10; i++)
  286. foreach (System.Windows.Shapes.Rectangle rect1 in paintSurface.Children.OfType<System.Windows.Shapes.Rectangle>())
  287. {
  288. paintSurface.Children.Remove(rect1);
  289. break;
  290.  
  291. }
  292. System.Windows.Shapes.Rectangle rect;
  293. rect = new System.Windows.Shapes.Rectangle();
  294. rect.Stroke = new SolidColorBrush(Colors.Red);
  295. rect.Fill = new SolidColorBrush(Colors.Transparent);
  296. rect.Width = 10;
  297. rect.Height = 10;
  298. Canvas.SetLeft(rect, marker_x - 5);
  299. Canvas.SetTop(rect, marker_y - 5);
  300. paintSurface.Children.Add(rect);
  301. return;
  302. }
  303. else
  304. {
  305. foreach (Line line1 in paintSurface.Children.OfType<Line>())
  306. {
  307. line = line1;
  308. if (line.Name == "line_" + cnt)
  309. {
  310. break;
  311. }
  312. else line = null;
  313. }
  314. }
  315. if (line_cur == null)
  316. {
  317. if (line == null)
  318. {
  319. line = new Line();
  320. line.Stroke = SystemColors.WindowFrameBrush;
  321. line.X1 = currentPoint.X;
  322. line.Y1 = currentPoint.Y - 106;
  323. line.X2 = e.GetPosition(this).X;
  324. line.Y2 = e.GetPosition(this).Y - 106;
  325.  
  326. currentPoint = e.GetPosition(this);
  327. line.Name = "line_" + cnt;
  328. line.Stroke = pen[cnt].Brush;
  329. line.StrokeThickness = pen[cnt].Thickness;
  330. paintSurface.Children.Add(line);
  331.  
  332.  
  333. for (int i = 0; i < lines.Length - 1; i++)
  334. {
  335. if (lines[i] == null)
  336. {
  337. lines[i] = line;
  338. break;
  339. }
  340. }
  341.  
  342. }
  343. else
  344. {
  345. line.X2 = e.GetPosition(this).X;
  346. line.Y2 = e.GetPosition(this).Y - 106;
  347. paintSurface.InvalidateVisual();
  348. }
  349. }
  350. }
  351. else
  352. {
  353. Line line = null;
  354. foreach (Line line1 in paintSurface.Children.OfType<Line>())
  355. {
  356. line = line1;
  357. if ((Math.Abs(line.X1 - e.GetPosition(this).X) < 5) && (Math.Abs(e.GetPosition(this).Y - 106 - line.Y1) < 5))
  358. {
  359. point_index = 0;
  360. marker_x = line.X1;
  361. marker_y = line.Y1;
  362. line_cur = line;
  363. }
  364. else if ((Math.Abs(line.X2 - e.GetPosition(this).X)) < 5 && (Math.Abs(e.GetPosition(this).Y - 106 - line.Y2) < 5))
  365. {
  366. point_index = 1;
  367. marker_x = line.X2;
  368. marker_y = line.Y2;
  369. line_cur = line;
  370. }
  371. }
  372. }
  373. if (marker_x != -1)
  374. {
  375. System.Windows.Shapes.Rectangle rect;
  376. rect = new System.Windows.Shapes.Rectangle();
  377. rect.Stroke = new SolidColorBrush(Colors.Red);
  378. rect.Fill = new SolidColorBrush(Colors.Transparent);
  379. rect.Width = 10;
  380. rect.Height = 10;
  381. Canvas.SetLeft(rect, marker_x - 5);
  382. Canvas.SetTop(rect, marker_y - 5);
  383. paintSurface.Children.Add(rect);
  384. }
  385. else
  386. {
  387.  
  388. foreach (Rectangle rect in paintSurface.Children.OfType<Rectangle>())
  389. {
  390. paintSurface.Children.Remove(rect);
  391. line_cur = null;
  392. paintSurface.InvalidateVisual();
  393. break;
  394. }
  395.  
  396.  
  397. }
  398. }
  399.  
  400.  
  401. }
  402.  
  403. private void Button_Click(object sender, RoutedEventArgs e)
  404. {
  405. if (mode != "erase")
  406. {
  407. for (int i = cnt; i < 100; i++)
  408. pen[i].Brush = ((Button)sender).Background;
  409. }
  410.  
  411.  
  412. }
  413.  
  414. private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  415. {
  416. for (int i = cnt; i < 100; i++)
  417. pen[i].Thickness = slider.Value;
  418. }
  419.  
  420.  
  421. private void NewFile(object sender, RoutedEventArgs e)
  422. {
  423. paintSurface.Children.Clear();
  424. }
  425. private void OpenFile(object sender, RoutedEventArgs e)
  426. {
  427. openFileDialog1.Multiselect = true;
  428. openFileDialog1.Filter = "Image files (*.BMP, *.JPG, *.GIF, *.TIF, *.PNG, *.ICO, *.EMF, *.WMF)|*.bmp;*.jpg;*.gif; *.tif; *.png; *.ico; *.emf; *.wmf";
  429. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  430. {
  431. System.IO.FileStream fs = new System.IO.FileStream(openFileDialog1.FileName, System.IO.FileMode.Open);
  432. System.Drawing.Image img = System.Drawing.Image.FromStream(fs);
  433. fs.Close();
  434. pictureBox1.Image = img;
  435. }
  436. }
  437. private void SaveFile(object sender, RoutedEventArgs e)
  438. {
  439. RenderVisualService.RenderToPNGFile(paintSurface, "myawesomeimage.png");
  440. }
  441.  
  442. private void Exit(object sender, RoutedEventArgs e)
  443. {
  444. RenderVisualService.RenderToPNGFile(paintSurface, "myawesomeimage.png");
  445. Close();
  446. }
  447.  
  448. private void line_Click(object sender, RoutedEventArgs e)
  449. {
  450. pen_mode = false;
  451. }
  452.  
  453. private void circle_Click(object sender, RoutedEventArgs e)
  454. {
  455. mode = "circle";
  456. }
  457.  
  458. private void rectangle_Click(object sender, RoutedEventArgs e)
  459. {
  460. pen_mode = true;
  461. mode = "rectangle";
  462. }
  463.  
  464. private void bucket_Click(object sender, RoutedEventArgs e)
  465. {
  466.  
  467. }
  468. private void eraser_Click(object sender, RoutedEventArgs e)
  469. {
  470.  
  471. mode = "erase";
  472. pen_mode = true;
  473. for (int i = cnt; i < 100; i++)
  474. {
  475. pen[i].Brush = new SolidColorBrush(Colors.White);
  476. pen[i].Thickness = 3f;
  477. }
  478.  
  479. }
  480.  
  481. private void Pencil_Click_(object sender, RoutedEventArgs e)
  482. {
  483. pen_mode = true;
  484. for (int i = 0; i < 100; i++)
  485. pen[i] = new Pen(SystemColors.WindowFrameBrush, 2f);
  486. }
  487.  
  488. private void Button_Click_4(object sender, RoutedEventArgs e)
  489. {
  490. if (pen_mode)
  491. {
  492.  
  493. for(int i= lines1[l_cnt].Length - 1;i>=0;i--)
  494. foreach (Line line1 in paintSurface.Children.OfType<Line>())
  495. {
  496. if (l_cnt == 0) break;
  497. if (line1 ==lines1[l_cnt-1][i])
  498. {
  499. lines1[l_cnt-1] = null;
  500.  
  501. paintSurface.Children.Remove(line1);
  502. break;
  503. }
  504. paintSurface.InvalidateVisual();
  505. }
  506.  
  507.  
  508. }
  509. else
  510. {
  511.  
  512. foreach (Line line1 in paintSurface.Children.OfType<Line>())
  513. {
  514. if (cnt == 0) {
  515. paintSurface.Children.Remove((Line)lines[cnt]);
  516. break;
  517. }
  518.  
  519. if (line1 == lines[cnt-1])
  520. {
  521. lines[cnt - 1] = null;
  522. cnt--;
  523. paintSurface.Children.Remove(line1);
  524. break;
  525. }
  526. paintSurface.InvalidateVisual();
  527. }
  528. }
  529.  
  530.  
  531. }
  532.  
  533.  
  534. }
  535.  
  536. }
  537.  
Advertisement
Add Comment
Please, Sign In to add comment