1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;
namespace PNGEDIT1
{
/// Класс главного окна приложения
public partial class MainForm : Form
{
private Form2 form2 = new Form2();
private Pen pen = new Pen(Color.Black);
private Point startPt;
private int mode;
private Point movePt;
private Point nullPt = new Point(int.MaxValue, 0);
private SolidBrush brush = new SolidBrush(Color.White);
private int figureMode;
private bool equalSize;
private Bitmap oldImage;
private Font font;
//Конструктор
public MainForm()
{
InitializeComponent();
AddOwnedForm(form2);
openFileDialog1.InitialDirectory = saveFileDialog1.InitialDirectory =
Directory.GetCurrentDirectory();
form2.numericUpDown1.Value = panel1.ClientSize.Width;
form2.numericUpDown2.Value = panel1.ClientSize.Height;
form2.button1_Click(this, null);
pen.StartCap = pen.EndCap = LineCap.Round;
pen.Alignment = PenAlignment.Inset;
oldImage = new Bitmap(pictureBox1.Image);
font = Font.Clone() as Font;
comboBox1.SelectedIndex = 0;
}
/// Обработчик выбора пункта меню Новый
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
form2.ActiveControl = form2.numericUpDown1;
if (form2.ShowDialog() == DialogResult.OK)
{
saveFileDialog1.FileName = "";
Text = "Image Editor";
UpdateOldImage();
}
}
/// Обработчик выбора пункта меню Открыть
private void ToolStripMenuItem2_Click(object sender, EventArgs e)
{
startPt = nullPt;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string s = openFileDialog1.FileName;
try
{
Image im = new Bitmap(s);
Graphics g = Graphics.FromImage(im);
g.Dispose();
if (pictureBox1.Image != null)
pictureBox1.Image.Dispose();
pictureBox1.Image = im;
UpdateOldImage();
}
catch
{
MessageBox.Show("File " + s + " has a wrong format.", "Error");
return;
}
Text = "Image Editor - " + s;
saveFileDialog1.FileName = Path.ChangeExtension(s, "png");
openFileDialog1.FileName = "";
}
}
/// Обработчик выбора пункта меню Сохранить
private void ToolStripMenuItem3_Click(object sender, EventArgs e)
{
startPt = nullPt;
string s0 = saveFileDialog1.FileName;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string s = saveFileDialog1.FileName;
if (s.ToUpper() == s0.ToUpper())
{
s0 = Path.GetDirectoryName(s0) + "\\($$##$$).png";
pictureBox1.Image.Save(s0);
pictureBox1.Image.Dispose();
File.Delete(s);
File.Move(s0, s);
pictureBox1.Image = new Bitmap(s);
}
else
pictureBox1.Image.Save(s);
Text = "Image Editor - " + s;
}
}
//Координаты указателя
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = string.Format("X,Y: {0},{1}", e.X, e.Y);
if (startPt == nullPt)
return;
if (e.Button == MouseButtons.Left)
{
switch (mode)
{
case 0:
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.DrawLine(pen, startPt, e.Location);
g.Dispose();
startPt = e.Location;
pictureBox1.Invalidate();
break;
case 1:
case 2:
ReversibleDraw();
movePt = e.Location;
equalSize = Control.ModifierKeys == Keys.Control;
ReversibleDraw();
break;
}
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
movePt = startPt = e.Location;
UpdateOldImage();
if (Control.ModifierKeys == Keys.Alt)
{
Color c = (pictureBox1.Image as Bitmap).GetPixel(e.X, e.Y);
if (e.Button == MouseButtons.Left)
label2.BackColor = c;
else
label4.BackColor = c;
}
else
if (mode == 3)
{
Graphics g = Graphics.FromImage(pictureBox1.Image);
using (SolidBrush b = new SolidBrush(pen.Color))
g.DrawString(textBox1.Text, font, b, e.Location);
g.Dispose();
pictureBox1.Invalidate();
}
}
private void button4_Click(object sender, EventArgs e)
{
UpdateOldImage();
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
g.Clear(brush.Color);
pictureBox1.Invalidate();
}
//Реализация цвета линий
private void label2_Click(object sender, EventArgs e)
{
Label lb = sender as Label;
colorDialog1.Color = lb.BackColor;
if (colorDialog1.ShowDialog() == DialogResult.OK)
lb.BackColor = colorDialog1.Color;
}
//Реализация цвета линий для фигуры
private void label2_BackColorChanged(object sender, EventArgs e)
{
pen.Color = label2.BackColor;
label5.Invalidate();
}
//Реализация выбора толщены кисти
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
pen.Width = (int)numericUpDown1.Value;
label5.Invalidate();
}
private void ReversibleDraw()
{
Point p1 = pictureBox1.PointToScreen(startPt),
p2 = pictureBox1.PointToScreen(movePt);
if (mode == 1)
ControlPaint.DrawReversibleLine(p1, p2, Color.Black);
else
ControlPaint.DrawReversibleFrame(PtToRect(p1, p2), Color.Black,
(FrameStyle)((figureMode + 1) % 2));
}
//Реализация выбора единственного инструмента для рисования
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (!rb.Checked)
return;
mode = rb.TabIndex;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (startPt == nullPt)
return;
if (mode >= 1)
{
ReversibleDraw();
label1.Invalidate();
Graphics g = Graphics.FromImage(pictureBox1.Image);
switch (mode)
{
case 1:
g.DrawLine(pen, startPt, movePt);
break;
case 2:
DrawFigure(PtToRect(startPt, movePt), g);
break;
}
g.Dispose();
pictureBox1.Invalidate();
}
}
//Выбор фонового цвета для фигуры
private void label4_BackColorChanged(object sender, EventArgs e)
{
brush.Color = label4.BackColor;
label5.Invalidate();
}
//Выбор между прямоугольником и элипсом
private void DrawFigure(Rectangle r, Graphics g)
{
switch (figureMode)
{
case 0:
if (!checkBox1.Checked)
g.FillRectangle(brush, r);
g.DrawRectangle(pen, r);
break;
case 1:
if (!checkBox1.Checked)
g.FillEllipse(brush, r);
g.DrawEllipse(pen, r);
break;
}
}
//Прямоугольник
private Rectangle PtToRect(Point p1, Point p2)
{
if (equalSize)
{
int dx = p2.X - p1.X, dy = p2.Y - p1.Y;
if (Math.Abs(dx) > Math.Abs(dy))
p2.X = p1.X + Math.Sign(dx) * Math.Abs(dy);
else
p2.Y = p1.Y + Math.Sign(dy) * Math.Abs(dx);
}
int x = Math.Min(p1.X, p2.X),
y = Math.Min(p1.Y, p2.Y),
w = Math.Abs(p2.X - p1.X),
h = Math.Abs(p2.Y - p1.Y);
return new Rectangle(x, y, w, h);
}
//Элипс
private void label5_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = label5.ClientRectangle;
r.Width--; r.Height--;
DrawFigure(r, g);
}
//выбор инструмента фигура
private void label5_MouseDown(object sender, MouseEventArgs e)
{
radioButton3.Checked = true;
figureMode = (figureMode + 1) % 2;
label5.Invalidate();
}
//Установление прозрачности для фигуры
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
label5.Invalidate();
}
private void UpdateOldImage()
{
oldImage.Dispose();
oldImage = new Bitmap(pictureBox1.Image);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = new Bitmap(oldImage);
}
}
// Окно ввода текста
private void textBox1_Enter(object sender, EventArgs e)
{
radioButton4.Checked = true;
}
// Реализация выбора стиля текста
private void button5_Click(object sender, EventArgs e)
{
fontDialog1.Font = font;
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
Font f = font;
textBox1.Font = font = fontDialog1.Font;
f.Dispose();
}
}
// Реализация выбора стиля линии
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
using (Pen p = new Pen(e.ForeColor, 2))
{
p.DashStyle = (DashStyle)e.Index;
int y = (e.Bounds.Top + e.Bounds.Bottom) / 2;
e.Graphics.DrawLine(p, e.Bounds.Left, y, e.Bounds.Right, y);
}
e.DrawFocusRectangle();
}
// Реализация выбора вида линии
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
pen.DashStyle = (DashStyle)comboBox1.SelectedIndex;
label5.Invalidate();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//Очистка габочего поля
private void ToolStripMenuItem4_Click(object sender, EventArgs e)
{
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
g.Clear(Color.White);
pictureBox1.Invalidate();
}
//Вызов окна "О программе"
private void ToolStripMenuItem12_Click(object sender, EventArgs e)
{
AboutBox1 about = new AboutBox1();
about.Show();
}
}
} |