Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using Microsoft.Win32;
- using System.IO;
- using System.Reflection.Metadata;
- public static class RenderVisualService
- {
- private const double defaultDpi = 96.0;
- private static BitmapSource GetRenderTargetBitmapFromControl(Visual targetControl, double dpi = defaultDpi)
- {
- if (targetControl == null) return null;
- var bounds = VisualTreeHelper.GetDescendantBounds(targetControl);
- var renderTargetBitmap = new RenderTargetBitmap((int)(bounds.Width * dpi / 96.0),
- (int)(bounds.Height * dpi / 96.0),
- dpi,
- dpi,
- PixelFormats.Pbgra32);
- var drawingVisual = new DrawingVisual();
- using (var drawingContext = drawingVisual.RenderOpen())
- {
- var visualBrush = new VisualBrush(targetControl);
- drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
- }
- renderTargetBitmap.Render(drawingVisual);
- return renderTargetBitmap;
- }
- public static void RenderToPNGFile(Visual targetControl, string filename)
- {
- var renderTargetBitmap = GetRenderTargetBitmapFromControl(targetControl);
- var encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
- var result = new BitmapImage();
- try
- {
- using (var fileStream = new FileStream(filename, FileMode.Create))
- {
- encoder.Save(fileStream);
- }
- }
- catch (Exception ex)
- {
- System.Diagnostics.Debug.WriteLine($"There was an error saving the file: {ex.Message}");
- }
- }
- }
- namespace VectorGraphicEditor
- {
- public partial class MainWindow : Window
- {
- Pen[] pen;
- Line[] lines;
- Line[][] lines1;
- int point_index = 0;
- Line line_cur = null;
- bool pen_mode = false;
- String mode="";
- Point currentPoint = new Point();
- int cnt = 0,l_cnt=0;
- double x1, y1;
- private Point startPoint;
- private Rectangle rect;
- private Ellipse circle;
- public MainWindow()
- {
- lines = new Line[100];
- lines1 = new Line[100][];
- for(int i = 0; i < lines1.Length; i++)
- {
- lines1[i] = new Line[100];
- }
- x1 = y1 = 0;
- InitializeComponent();
- pen = new Pen[100];
- for (int i = 0; i < 100; i++)
- pen[i]=new Pen(SystemColors.WindowFrameBrush,2f);
- paintSurface.Background = new SolidColorBrush(Colors.White);
- }
- private void paintSurface_MouseUp(object sender, MouseButtonEventArgs e)
- {
- circle = null;
- rect = null;
- if (!pen_mode)
- cnt++;
- else l_cnt++;
- }
- private void paintSurface_MouseDown(object sender,MouseButtonEventArgs e)
- {
- if (mode == "rectangle")
- {
- startPoint = e.GetPosition(paintSurface);
- rect = new Rectangle
- {
- Stroke = pen[cnt].Brush,
- StrokeThickness = pen[cnt].Thickness
- };
- Canvas.SetLeft(rect, startPoint.X);
- Canvas.SetTop(rect, startPoint.Y);
- paintSurface.Children.Add(rect);
- }
- else if (mode == "circle")
- {
- startPoint = e.GetPosition(paintSurface);
- circle = new Ellipse
- {
- Stroke = pen[cnt].Brush,
- StrokeThickness = pen[cnt].Thickness
- };
- Canvas.SetLeft(rect, startPoint.X);
- Canvas.SetTop(rect, startPoint.Y);
- paintSurface.Children.Add(circle);
- }
- else if (pen_mode)
- {
- x1 = e.GetPosition(this).X;
- y1 = e.GetPosition(this).Y;
- }
- else
- {
- double marker_x = -1, marker_y = -1;
- if (e.LeftButton == MouseButtonState.Pressed)
- currentPoint = e.GetPosition(this);
- Line line;
- foreach (Line line1 in paintSurface.Children.OfType<Line>())
- {
- line = line1;
- if (Math.Abs(line.X1 - e.GetPosition(this).X) < 5 && Math.Abs(e.GetPosition(this).Y - 106 - line.Y1) < 5)
- {
- point_index = 0;
- marker_x = line.X1;
- marker_y = line.Y1;
- line_cur = line;
- }
- else if (Math.Abs(line.X2 - e.GetPosition(this).X) < 5 && Math.Abs(e.GetPosition(this).Y - 106 - line.Y2) < 5)
- {
- point_index = 1;
- marker_x = line.X2;
- marker_y = line.Y2;
- line_cur = line;
- }
- }
- }
- }
- private void paintSurface_MouseMove(object sender,MouseEventArgs e)
- {
- if (pen_mode)
- {
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- if (mode == "line")
- {
- Line line_ = new Line();
- line_.X1 = e.GetPosition(this).X;
- line_.Y1 = e.GetPosition(this).Y - 106;
- line_.X2 = x1;
- line_.Y2 = y1 - 106;
- line_.StrokeThickness = pen[cnt].Thickness;
- line_.Stroke = pen[cnt].Brush;
- paintSurface.Children.Add(line_);
- for (int i = 0; i < lines1[l_cnt].Length - 1; i++)
- {
- if (lines1[l_cnt][i] == null)
- {
- lines1[l_cnt][i] = line_;
- break;
- }
- }
- }
- else if (mode == "rectangle")
- {
- if (e.LeftButton == MouseButtonState.Released || rect == null)
- return;
- var pos = e.GetPosition(paintSurface);
- var x = Math.Min(pos.X, startPoint.X);
- var y = Math.Min(pos.Y, startPoint.Y);
- var w = Math.Max(pos.X, startPoint.X) - x;
- var h = Math.Max(pos.Y, startPoint.Y) - y;
- rect.Width = w;
- rect.Height = h;
- Canvas.SetLeft(rect, x);
- Canvas.SetTop(rect, y);
- }
- else if (mode == "circle")
- {
- if (e.LeftButton == MouseButtonState.Released || circle == null)
- return;
- var pos = e.GetPosition(paintSurface);
- var x = Math.Min(pos.X, startPoint.X);
- var y = Math.Min(pos.Y, startPoint.Y);
- var w = Math.Max(pos.X, startPoint.X) - x;
- var h = Math.Max(pos.Y, startPoint.Y) - y;
- rect.Width = w;
- rect.Height = h;
- Canvas.SetLeft(circle, x);
- Canvas.SetTop(circle, y);
- }
- x1 = e.GetPosition(this).X;
- y1 = e.GetPosition(this).Y;
- }
- }
- else {
- double marker_x = -1, marker_y = -1;
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- Line line = null;
- if (line_cur != null)
- {
- if (point_index == 0)
- {
- line_cur.X1 = e.GetPosition(this).X;
- line_cur.Y1 = e.GetPosition(this).Y - 106;
- marker_x = line_cur.X1;
- marker_y = line_cur.Y1;
- Console.WriteLine("Правлю точку 0");
- }
- else
- {
- line_cur.X2 = e.GetPosition(this).X;
- line_cur.Y2 = e.GetPosition(this).Y - 106;
- marker_x = line_cur.X2;
- marker_y = line_cur.Y2;
- Console.WriteLine("Правлю точку 1");
- }
- for (int i = 0; i < 10; i++)
- foreach (System.Windows.Shapes.Rectangle rect1 in paintSurface.Children.OfType<System.Windows.Shapes.Rectangle>())
- {
- paintSurface.Children.Remove(rect1);
- break;
- }
- System.Windows.Shapes.Rectangle rect;
- rect = new System.Windows.Shapes.Rectangle();
- rect.Stroke = new SolidColorBrush(Colors.Red);
- rect.Fill = new SolidColorBrush(Colors.Transparent);
- rect.Width = 10;
- rect.Height = 10;
- Canvas.SetLeft(rect, marker_x - 5);
- Canvas.SetTop(rect, marker_y - 5);
- paintSurface.Children.Add(rect);
- return;
- }
- else
- {
- foreach (Line line1 in paintSurface.Children.OfType<Line>())
- {
- line = line1;
- if (line.Name == "line_" + cnt)
- {
- break;
- }
- else line = null;
- }
- }
- if (line_cur == null)
- {
- if (line == null)
- {
- line = new Line();
- line.Stroke = SystemColors.WindowFrameBrush;
- line.X1 = currentPoint.X;
- line.Y1 = currentPoint.Y - 106;
- line.X2 = e.GetPosition(this).X;
- line.Y2 = e.GetPosition(this).Y - 106;
- currentPoint = e.GetPosition(this);
- line.Name = "line_" + cnt;
- line.Stroke = pen[cnt].Brush;
- line.StrokeThickness = pen[cnt].Thickness;
- paintSurface.Children.Add(line);
- for (int i = 0; i < lines.Length - 1; i++)
- {
- if (lines[i] == null)
- {
- lines[i] = line;
- break;
- }
- }
- }
- else
- {
- line.X2 = e.GetPosition(this).X;
- line.Y2 = e.GetPosition(this).Y - 106;
- paintSurface.InvalidateVisual();
- }
- }
- }
- else
- {
- Line line = null;
- foreach (Line line1 in paintSurface.Children.OfType<Line>())
- {
- line = line1;
- if ((Math.Abs(line.X1 - e.GetPosition(this).X) < 5) && (Math.Abs(e.GetPosition(this).Y - 106 - line.Y1) < 5))
- {
- point_index = 0;
- marker_x = line.X1;
- marker_y = line.Y1;
- line_cur = line;
- }
- else if ((Math.Abs(line.X2 - e.GetPosition(this).X)) < 5 && (Math.Abs(e.GetPosition(this).Y - 106 - line.Y2) < 5))
- {
- point_index = 1;
- marker_x = line.X2;
- marker_y = line.Y2;
- line_cur = line;
- }
- }
- }
- if (marker_x != -1)
- {
- System.Windows.Shapes.Rectangle rect;
- rect = new System.Windows.Shapes.Rectangle();
- rect.Stroke = new SolidColorBrush(Colors.Red);
- rect.Fill = new SolidColorBrush(Colors.Transparent);
- rect.Width = 10;
- rect.Height = 10;
- Canvas.SetLeft(rect, marker_x - 5);
- Canvas.SetTop(rect, marker_y - 5);
- paintSurface.Children.Add(rect);
- }
- else
- {
- foreach (Rectangle rect in paintSurface.Children.OfType<Rectangle>())
- {
- paintSurface.Children.Remove(rect);
- line_cur = null;
- paintSurface.InvalidateVisual();
- break;
- }
- }
- }
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- if (mode != "erase")
- {
- for (int i = cnt; i < 100; i++)
- pen[i].Brush = ((Button)sender).Background;
- }
- }
- private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- for (int i = cnt; i < 100; i++)
- pen[i].Thickness = slider.Value;
- }
- private void NewFile(object sender, RoutedEventArgs e)
- {
- paintSurface.Children.Clear();
- }
- private void OpenFile(object sender, RoutedEventArgs e)
- {
- openFileDialog1.Multiselect = true;
- openFileDialog1.Filter = "Image files (*.BMP, *.JPG, *.GIF, *.TIF, *.PNG, *.ICO, *.EMF, *.WMF)|*.bmp;*.jpg;*.gif; *.tif; *.png; *.ico; *.emf; *.wmf";
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- System.IO.FileStream fs = new System.IO.FileStream(openFileDialog1.FileName, System.IO.FileMode.Open);
- System.Drawing.Image img = System.Drawing.Image.FromStream(fs);
- fs.Close();
- pictureBox1.Image = img;
- }
- }
- private void SaveFile(object sender, RoutedEventArgs e)
- {
- RenderVisualService.RenderToPNGFile(paintSurface, "myawesomeimage.png");
- }
- private void Exit(object sender, RoutedEventArgs e)
- {
- RenderVisualService.RenderToPNGFile(paintSurface, "myawesomeimage.png");
- Close();
- }
- private void line_Click(object sender, RoutedEventArgs e)
- {
- pen_mode = false;
- }
- private void circle_Click(object sender, RoutedEventArgs e)
- {
- mode = "circle";
- }
- private void rectangle_Click(object sender, RoutedEventArgs e)
- {
- pen_mode = true;
- mode = "rectangle";
- }
- private void bucket_Click(object sender, RoutedEventArgs e)
- {
- }
- private void eraser_Click(object sender, RoutedEventArgs e)
- {
- mode = "erase";
- pen_mode = true;
- for (int i = cnt; i < 100; i++)
- {
- pen[i].Brush = new SolidColorBrush(Colors.White);
- pen[i].Thickness = 3f;
- }
- }
- private void Pencil_Click_(object sender, RoutedEventArgs e)
- {
- pen_mode = true;
- for (int i = 0; i < 100; i++)
- pen[i] = new Pen(SystemColors.WindowFrameBrush, 2f);
- }
- private void Button_Click_4(object sender, RoutedEventArgs e)
- {
- if (pen_mode)
- {
- for(int i= lines1[l_cnt].Length - 1;i>=0;i--)
- foreach (Line line1 in paintSurface.Children.OfType<Line>())
- {
- if (l_cnt == 0) break;
- if (line1 ==lines1[l_cnt-1][i])
- {
- lines1[l_cnt-1] = null;
- paintSurface.Children.Remove(line1);
- break;
- }
- paintSurface.InvalidateVisual();
- }
- }
- else
- {
- foreach (Line line1 in paintSurface.Children.OfType<Line>())
- {
- if (cnt == 0) {
- paintSurface.Children.Remove((Line)lines[cnt]);
- break;
- }
- if (line1 == lines[cnt-1])
- {
- lines[cnt - 1] = null;
- cnt--;
- paintSurface.Children.Remove(line1);
- break;
- }
- paintSurface.InvalidateVisual();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment