Windows Forms C# Графический векторный редактор
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyGraphicEditor
{
public partial class Form1 : Form
{
Bitmap map = new Bitmap(100,100);
Pen pen = new Pen(Color.Black,3f);
Graphics graphics;
int cnt = 0;
Point[][] p;
bool mdown;
String mode;
bool point_focused;
int catch_line_lindex;
int catch_point_lindex;
public Form1()
{
mode = "Рисуем линию";
catch_line_lindex = -1;
catch_point_lindex = -1;
point_focused = false;
mdown = false;
InitializeComponent();
setSize();
p = new Point[100][];
for (int i = 0; i < 100; i++)
p[i] = new Point[2];
}
private void setSize()
{
Rectangle rectangle = Screen.PrimaryScreen.Bounds;
map = new Bitmap(rectangle.Width, rectangle.Height);
graphics = Graphics.FromImage(map);
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
mdown = false;
if (mode == "Рисуем линию")
{
p[cnt][1].X = e.X;
p[cnt][1].Y = e.Y;
cnt++;
}
if (mode == "Изменяем линию")
{
p[catch_line_lindex][catch_point_lindex].X = e.X;
p[catch_line_lindex][catch_point_lindex].Y = e.Y;
}
pictureBox1.Invalidate();
mode = "Рисуем линию";
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mdown = true;
if(mode == "Рисуем линию")
{
p[cnt][0].X = e.X;
p[cnt][0].Y = e.Y;
}
if (mode == "Изменяем линию")
{
p[catch_line_lindex][catch_point_lindex].X = e.X;
p[catch_line_lindex][catch_point_lindex].Y = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mdown)
{
if (mode == "Рисуем линию")
{
p[cnt][1].X = e.X;
p[cnt][1].Y = e.Y;
}
if (mode == "Изменяем линию")
{
p[catch_line_lindex][catch_point_lindex].X = e.X;
p[catch_line_lindex][catch_point_lindex].Y = e.Y;
}
}
else
{
mode = "Рисуем линию";
point_focused = false;
catch_point_lindex = -1;
catch_line_lindex = -1;
for (int i = 0; i < cnt; i++)
{
if(Math.Abs(p[i][0].X-e.X)<5 && Math.Abs(p[i][0].Y - e.Y) < 5)
{
point_focused = true;
catch_point_lindex = 0;
catch_line_lindex = i;
mode = "Изменяем линию";
}
if (Math.Abs(p[i][1].X - e.X) < 5 && Math.Abs(p[i][1].Y - e.Y) < 5)
{
point_focused = true;
catch_point_lindex = 1;
catch_line_lindex = i;
mode = "Изменяем линию";
}
}
}
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender,PaintEventArgs e)
{
graphics = e.Graphics;
if (point_focused)
{
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);
}
for(int i = 0; i < cnt; i++)
{
graphics.DrawLine(pen, p[i][0].X, p[i][0].Y, p[i][1].X, p[i][1].Y);
}
if (mdown)
{
graphics.DrawLine(pen, p[cnt][0].X, p[cnt][0].Y, p[cnt][1].X, p[cnt][1].Y);
}
}
private void button2_Click(object sender, EventArgs e)
{
graphics.Clear(pictureBox1.BackColor);
pictureBox1.Image = map;
}
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
pen.Width = trackBar1.Value;
}
private void button3_Click(object sender, EventArgs e)
{
pen.Color = ((Button)sender).BackColor;
}
private void button5_Click(object sender, EventArgs e)
{
pen.Color = ((Button)sender).BackColor;
}
private void button4_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
pen.Color = colorDialog1.Color;
((Button)sender).BackColor = colorDialog1.Color;
}
}
private void button1_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "JPG(*.JPG)|*.jpg";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (pictureBox1.Image != null)
{
pictureBox1.Image.Save(saveFileDialog1.FileName);
}
}
}
}
}
Только начал работать с windows forms в c#. У меня возникает ошибка в строке graphics.Clear(pictureBox1.BackColor); Пишет System.ArgumentException: "Parameter is not valid." Плюс у меня не сохраняется изображение в файл.