-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
154 lines (133 loc) · 5.4 KB
/
Main.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SimplePaint
{
public partial class Main : Form
{
private bool flagPìntar = false;
private bool flagApagar = false;
private Graphics graphicsPainelPintura;
private float espessuraCaneta;
private Color corBorracha;
private Image imagemSalvar;
private Graphics graphicsImagem;
public Main()
{
InitializeComponent();
for (int espessura = 2; espessura <= 100; espessura += 2) comboBoxEspessuraDaCaneta.Items.Add(espessura);
comboBoxEspessuraDaCaneta.Text = "10";
comboBoxEspessuraDaCaneta.MaxDropDownItems = 4;
comboBoxEspessuraDaCaneta.IntegralHeight = false;
graphicsPainelPintura = panelPintura.CreateGraphics();
espessuraCaneta = float.Parse(comboBoxEspessuraDaCaneta.Text);
corBorracha = panelPintura.BackColor;
imagemSalvar = new Bitmap(panelPintura.Width, panelPintura.Height);
graphicsImagem = Graphics.FromImage(imagemSalvar);
graphicsImagem.Clear(panelPintura.BackColor);
}
private void buttonCorCaneta_Click(object sender, EventArgs e)
{
var colorDialog = new ColorDialog();
var corEscolhida = colorDialog.ShowDialog();
if (corEscolhida == DialogResult.OK)
{
buttonCorCaneta.BackColor = colorDialog.Color;
}
}
private void Main_Load(object sender, EventArgs e)
{
}
private void panelPintura_MouseDown(object sender, MouseEventArgs e)
{
flagPìntar = true;
}
private void panelPintura_MouseUp(object sender, MouseEventArgs e)
{
flagPìntar = false;
}
private void panelPintura_MouseMove(object sender, MouseEventArgs e)
{
if (flagPìntar)
{
if (!flagApagar)
{
graphicsPainelPintura
.DrawEllipse(new Pen(buttonCorCaneta.BackColor, espessuraCaneta), new RectangleF(e.X, e.Y, espessuraCaneta, espessuraCaneta));
graphicsImagem
.DrawEllipse(new Pen(buttonCorCaneta.BackColor, espessuraCaneta), new RectangleF(e.X, e.Y, espessuraCaneta, espessuraCaneta));
}
else
{
graphicsPainelPintura
.DrawRectangle(new Pen(corBorracha, espessuraCaneta), new Rectangle(e.X, e.Y, (int)espessuraCaneta, (int)espessuraCaneta));
graphicsImagem
.DrawRectangle(new Pen(corBorracha, espessuraCaneta), new Rectangle(e.X, e.Y, (int)espessuraCaneta, (int)espessuraCaneta));
}
}
}
private void buttonLimpar_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Você tem certeza que deseja apagar tudo?", "Apagar desenho", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
graphicsPainelPintura.Clear(Color.White);
imagemSalvar = new Bitmap(panelPintura.Width, panelPintura.Height);
graphicsImagem = Graphics.FromImage(imagemSalvar);
}
}
private void comboBoxEspessuraDaCaneta_SelectedIndexChanged(object sender, EventArgs e)
{
espessuraCaneta = float.Parse(comboBoxEspessuraDaCaneta.SelectedItem.ToString());
}
private void buttonBorracha_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() == DialogResult.OK)
{
corBorracha = colorDialog.Color;
}
}
else
{
flagApagar = !flagApagar;
if (flagApagar)
buttonBorracha.BackColor = corBorracha;
}
}
private void buttonSalvar_Click(object sender, EventArgs e)
{
var saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Portable Network Graphics|.png|Arquivo JPEG|.jpeg";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
switch(saveFileDialog.FilterIndex)
{
case 1:
imagemSalvar.Save(saveFileDialog.FileName, ImageFormat.Png);
break;
case 2:
imagemSalvar.Save(saveFileDialog.FileName, ImageFormat.Jpeg);
break;
}
}
}
private void panelPintura_Resize(object sender, EventArgs e)
{
var imagemTemp = new Bitmap(panelPintura.Width, panelPintura.Height);
var graphicsTemp = Graphics.FromImage(imagemTemp);
graphicsPainelPintura = panelPintura.CreateGraphics();
graphicsTemp.DrawImage(imagemSalvar, 0, 0);
imagemSalvar = imagemTemp;
graphicsImagem = graphicsTemp;
}
}
}