Простой графический редактор для картинок форматов BMP, PNG, ICO, GIF, JPG, TIFF.
Демонстрирует приёмы рисования мышкой на PictureBox.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Графический_редактор
{
public partial class Form1 : Form
{
bool isMouseDown;
Point oldLocation;
List<GraphicsPath> paths = new List<GraphicsPath>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
paths.Clear();
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
oldLocation = e.Location;
isMouseDown = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (!toolStripButton3.Checked)
{
return;
}
if (!isMouseDown)
{
return;
}
paths.Add(new GraphicsPath());
paths[paths.Count - 1].AddLine(oldLocation, e.Location);
oldLocation = e.Location;
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (GraphicsPath path in paths)
{
Graphics.FromImage(pictureBox1.Image).DrawPath(Pens.Black, path);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
switch (saveFileDialog1.FilterIndex)
{
case 0:
pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
break;
case 1:
pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Png);
break;
case 2:
pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Icon);
break;
case 3:
pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Gif);
break;
case 4:
pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
break;
case 5:
pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Tiff);
break;
}
}
}
}
}
Иконки взяты отсюда.