Maxim_Leo

Untitled

Jun 12th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.36 KB | None | 0 0
  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.  
  11. namespace MyGraphicEditor
  12. {
  13. public partial class Form1 : Form
  14. {
  15. Bitmap map;
  16. Pen[] pen;
  17. Pen pen1;
  18. // Pen pen = new Pen(Color.Black,3f);
  19. Graphics graphics;
  20. bool pen_mode;
  21. int cnt = 0;
  22. Point[][] p;
  23. bool mdown;
  24. String mode;
  25. bool point_focused;
  26. int catch_line_lindex;
  27. int catch_point_lindex;
  28.  
  29. private class ArrayPoints
  30. {
  31. private int index = 0;
  32. private Point[] points;
  33. public ArrayPoints(int size)
  34. {
  35. if (size <= 0) size = 2;
  36. points = new Point[size];
  37. }
  38. public void setPoint(int x,int y)
  39. {
  40. if (index >= points.Length) index = 0;
  41. points[index] = new Point(x, y);
  42. index++;
  43. }
  44. public void resetPoints()
  45. {
  46. index = 0;
  47. }
  48. public int getCountPoints()
  49. {
  50. return index;
  51. }
  52. public Point[] GetPoints()
  53. {
  54. return points;
  55. }
  56. }
  57.  
  58. private ArrayPoints arrayPoints;
  59. public Form1()
  60. {
  61. arrayPoints = new ArrayPoints(2);
  62. pen1 = new Pen(Color.Black, 3f);
  63. pen_mode = false;
  64. map = new Bitmap(100, 100);
  65. mode = "Рисуем линию";
  66. catch_line_lindex = -1;
  67. catch_point_lindex = -1;
  68. point_focused = false;
  69. mdown = false;
  70. InitializeComponent();
  71. setSize();
  72. p = new Point[100][];
  73. pen = new Pen[100];
  74. for (int i = 0; i < 100; i++)
  75. {
  76. p[i] = new Point[2];
  77. pen[i] = new Pen(Color.Black, 3f);
  78. }
  79.  
  80. }
  81.  
  82. private void setSize()
  83. {
  84. Rectangle rectangle = Screen.PrimaryScreen.Bounds;
  85. map = new Bitmap(rectangle.Width, rectangle.Height);
  86. graphics = Graphics.FromImage(map);
  87. for(int i = 0; i < cnt; i++)
  88. {
  89. pen[i].StartCap = System.Drawing.Drawing2D.LineCap.Round;
  90. pen[i].EndCap = System.Drawing.Drawing2D.LineCap.Round;
  91. }
  92.  
  93.  
  94. }
  95.  
  96. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  97. {
  98. mdown = false;
  99. if (pen_mode)
  100. {
  101. arrayPoints.resetPoints();
  102. }
  103. else {
  104. if (mode == "Рисуем линию")
  105. {
  106. p[cnt][1].X = e.X;
  107. p[cnt][1].Y = e.Y;
  108. cnt++;
  109. }
  110. if (mode == "Изменяем линию")
  111. {
  112. p[catch_line_lindex][catch_point_lindex].X = e.X;
  113. p[catch_line_lindex][catch_point_lindex].Y = e.Y;
  114. }
  115.  
  116. pictureBox1.Invalidate();
  117. mode = "Рисуем линию";
  118. }
  119.  
  120. }
  121. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  122. {
  123. mdown = true;
  124. if (!pen_mode) {
  125. if (mode == "Рисуем линию")
  126. {
  127. p[cnt][0].X = e.X;
  128. p[cnt][0].Y = e.Y;
  129. }
  130. if (mode == "Изменяем линию")
  131. {
  132. p[catch_line_lindex][catch_point_lindex].X = e.X;
  133. p[catch_line_lindex][catch_point_lindex].Y = e.Y;
  134. }
  135. }
  136.  
  137.  
  138.  
  139. }
  140. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  141. {
  142. if (pen_mode)
  143. {
  144. if (!mdown) return;
  145. arrayPoints.setPoint(e.X, e.Y);
  146. if (arrayPoints.getCountPoints() >= 2)
  147. {
  148. graphics.DrawLines(pen1,arrayPoints.GetPoints());
  149. pictureBox1.Image = map;
  150. arrayPoints.setPoint(e.X, e.Y);
  151. }
  152. }
  153. else
  154. {
  155. if (mdown)
  156. {
  157. if (mode == "Рисуем линию")
  158. {
  159. p[cnt][1].X = e.X;
  160. p[cnt][1].Y = e.Y;
  161. }
  162. if (mode == "Изменяем линию")
  163. {
  164. p[catch_line_lindex][catch_point_lindex].X = e.X;
  165. p[catch_line_lindex][catch_point_lindex].Y = e.Y;
  166. }
  167. }
  168. else
  169. {
  170.  
  171. mode = "Рисуем линию";
  172. point_focused = false;
  173. catch_point_lindex = -1;
  174. catch_line_lindex = -1;
  175. for (int i = 0; i < cnt; i++)
  176. {
  177. if (Math.Abs(p[i][0].X - e.X) < 5 && Math.Abs(p[i][0].Y - e.Y) < 5)
  178. {
  179. point_focused = true;
  180. catch_point_lindex = 0;
  181. catch_line_lindex = i;
  182. mode = "Изменяем линию";
  183. }
  184. if (Math.Abs(p[i][1].X - e.X) < 5 && Math.Abs(p[i][1].Y - e.Y) < 5)
  185. {
  186. point_focused = true;
  187. catch_point_lindex = 1;
  188. catch_line_lindex = i;
  189. mode = "Изменяем линию";
  190. }
  191. }
  192.  
  193.  
  194. }
  195. }
  196. pictureBox1.Invalidate();
  197. }
  198. private void pictureBox1_Paint(object sender,PaintEventArgs e)
  199. {
  200. graphics = e.Graphics;
  201. if (!pen_mode)
  202. {
  203. if (point_focused)
  204. {
  205. graphics.DrawRectangle(new Pen(Color.Red), p[catch_line_lindex][catch_point_lindex].X - 5, p[catch_line_lindex][catch_point_lindex].Y - 5, 10, 10);
  206. }
  207.  
  208. for (int i = 0; i < cnt; i++)
  209. {
  210. graphics.DrawLine(pen[i], p[i][0].X, p[i][0].Y, p[i][1].X, p[i][1].Y);
  211. }
  212. if (mdown)
  213. {
  214. graphics.DrawLine(pen[cnt], p[cnt][0].X, p[cnt][0].Y, p[cnt][1].X, p[cnt][1].Y);
  215. }
  216. }
  217.  
  218.  
  219.  
  220. }
  221.  
  222.  
  223. private void button2_Click(object sender, EventArgs e)
  224. {
  225. pictureBox1.Dispose();
  226.  
  227. }
  228.  
  229. private void trackBar1_ValueChanged(object sender, EventArgs e)
  230. {
  231. for (int i = cnt; i < 100; i++)
  232. pen[i].Width = trackBar1.Value;
  233. }
  234.  
  235. private void button3_Click(object sender, EventArgs e)
  236. {
  237. for(int i=cnt;i<100;i++)
  238. pen[i].Color = ((Button)sender).BackColor;
  239. }
  240.  
  241. private void button5_Click(object sender, EventArgs e)
  242. {
  243. pen[cnt].Color = ((Button)sender).BackColor;
  244. }
  245.  
  246. private void button4_Click(object sender, EventArgs e)
  247. {
  248. if (colorDialog1.ShowDialog() == DialogResult.OK)
  249. {
  250. pen[cnt].Color = colorDialog1.Color;
  251. ((Button)sender).BackColor = colorDialog1.Color;
  252. }
  253. }
  254.  
  255. private void button1_Click(object sender, EventArgs e)
  256. {
  257.  
  258. if (pictureBox1.Image != null) //если в pictureBox есть изображение
  259. {
  260. //создание диалогового окна "Сохранить как..", для сохранения изображения
  261. saveFileDialog1 = new SaveFileDialog();
  262. saveFileDialog1.Title = "Сохранить картинку как...";
  263. //отображать ли предупреждение, если пользователь указывает имя уже существующего файла
  264. saveFileDialog1.OverwritePrompt = true;
  265. //отображать ли предупреждение, если пользователь указывает несуществующий путь
  266. saveFileDialog1.CheckPathExists = true;
  267. //список форматов файла, отображаемый в поле "Тип файла"
  268. saveFileDialog1.Filter = "Image Files(*.BMP)|*.BMP|Image Files(*.JPG)|*.JPG|Image Files(*.GIF)|*.GIF|Image Files(*.PNG)|*.PNG|All files (*.*)|*.*";
  269. //отображается ли кнопка "Справка" в диалоговом окне
  270. saveFileDialog1.ShowHelp = true;
  271. if (saveFileDialog1.ShowDialog() == DialogResult.OK) //если в диалоговом окне нажата кнопка "ОК"
  272. {
  273.  
  274. pictureBox1.Image.Save(saveFileDialog1.FileName);
  275.  
  276.  
  277. }
  278. }
  279. }
  280.  
  281. private void button13_Click(object sender, EventArgs e)
  282. {
  283. Bitmap image; //Bitmap для открываемого изображения
  284.  
  285. OpenFileDialog open_dialog = new OpenFileDialog(); //создание диалогового окна для выбора файла
  286. open_dialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"; //формат загружаемого файла
  287. if (open_dialog.ShowDialog() == DialogResult.OK) //если в окне была нажата кнопка "ОК"
  288. {
  289. try
  290. {
  291. image = new Bitmap(open_dialog.FileName);
  292. //вместо pictureBox1 укажите pictureBox, в который нужно загрузить изображение
  293. this.pictureBox1.Size = image.Size;
  294. pictureBox1.Image = image;
  295. pictureBox1.Invalidate();
  296. }
  297. catch
  298. {
  299. DialogResult rezult = MessageBox.Show("Невозможно открыть выбранный файл",
  300. "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  301. }
  302. }
  303. }
  304.  
  305.  
  306.  
  307. private void button14_Click(object sender, EventArgs e)
  308. { if (!pen_mode)
  309. pen_mode = true;
  310. else pen_mode = false;
  311. }
  312. }
  313. }
  314.  
  315. Пытаюсь сделать графический векторный редактор. Но возникло несколько проблем. в button2(кнопка очищения рисунка) возникает ошибка - неправильный аргумент. Не знаю как очистить рисунок, все перепробовал - не работает. Button1(кнопка сохранения) тоже не работает. Также я сделал два режима рисования. Первый это рисование прямыми,их еще можно перетаскивать. Второе это произвольное рисование(рисует,когда зажата мышка). Вот произвольное почему-то не работает. Там тоже ошибка возникает в строке graphics.DrawLines(pen1,arrayPoints.GetPoints()); Хотя я тут массив точек и карандаш подал на вход,все правильно.
  316.  
Advertisement
Add Comment
Please, Sign In to add comment