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
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace LinyxPainter { public partial class MainForm : Form { #region 定义变量 Graphics graphics; Bitmap paintImg; int pWidth; int pHeight; bool isPainting=false; int startXPos; int startYPos; Btns btn = Btns.Pencil; Color drawColor=Color.Black; float drawWidth = 2f;
enum Btns { Pencil, Line, Circle, Rectangle, Eraser } #endregion
public MainForm() { InitializeComponent(); }
private void MainForm_Load(object sender, EventArgs e) { pWidth = Painter.Width; pHeight = Painter.Height;
paintImg = new Bitmap(pWidth, pHeight); graphics = Graphics.FromImage(paintImg); graphics.Clear(Color.White); Painter.Image = paintImg; }
private void Painter_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isPainting = true; startXPos = e.X; startYPos = e.Y; } }
private void Painter_MouseMove(object sender, MouseEventArgs e) { if(isPainting) { #region 绘制临时图像 Graphics currGra = Graphics.FromImage(paintImg); Pen pen = new Pen(drawColor, drawWidth); pen.StartCap = LineCap.Round; pen.EndCap = LineCap.Round; currGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; if (btn == Btns.Pencil||btn==Btns.Eraser) { switch (btn) { case Btns.Pencil: currGra.DrawLine(pen, startXPos, startYPos, e.X, e.Y); break; case Btns.Eraser: pen.Color = Color.White; pen.Width = 30f; currGra.DrawLine(pen, startXPos, startYPos, e.X, e.Y); pen.Color = drawColor; pen.Width = drawWidth; break; } startXPos = e.X; startYPos = e.Y; Painter.Image = paintImg; } else if(btn==Btns.Line||btn==Btns.Circle||btn==Btns.Rectangle) { Bitmap tempImg = new Bitmap(pWidth, pHeight); tempImg = (Bitmap)paintImg.Clone(); currGra = Graphics.FromImage(tempImg);
switch (btn) { case Btns.Line: currGra.DrawLine(pen, startXPos, startYPos, e.X, e.Y); break; case Btns.Circle: currGra.DrawEllipse(pen, startXPos, startYPos, e.X - startXPos, e.Y - startYPos); break; case Btns.Rectangle: if (e.X < startXPos) { if(e.Y < startYPos) currGra.DrawRectangle(pen, e.X, e.Y, startXPos - e.X, startYPos - e.Y); else currGra.DrawRectangle(pen, e.X, startYPos, startXPos - e.X, e.Y - startYPos); } else { if(e.Y < startYPos) currGra.DrawRectangle(pen, startXPos, e.Y, e.X - startXPos, startYPos - e.Y); else currGra.DrawRectangle(pen, startXPos, startYPos, e.X - startXPos, e.Y - startYPos); } break; } Painter.Image = tempImg; } currGra.Dispose(); #endregion } }
private void Painter_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isPainting = false;
#region 画最后一笔 Graphics currGra = Graphics.FromImage(paintImg); Pen pen = new Pen(drawColor, drawWidth); pen.StartCap = LineCap.Round; pen.EndCap = LineCap.Round; currGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
switch (btn) { case Btns.Line: currGra.DrawLine(pen, startXPos, startYPos, e.X, e.Y); break; case Btns.Circle: currGra.DrawEllipse(pen, startXPos, startYPos, e.X - startXPos, e.Y - startYPos); break; case Btns.Rectangle: if (e.X < startXPos) { if (e.Y < startYPos) currGra.DrawRectangle(pen, e.X, e.Y, startXPos - e.X, startYPos - e.Y); else currGra.DrawRectangle(pen, e.X, startYPos, startXPos - e.X, e.Y - startYPos); } else { if (e.Y < startYPos) currGra.DrawRectangle(pen, startXPos, e.Y, e.X - startXPos, startYPos - e.Y); else currGra.DrawRectangle(pen, startXPos, startYPos, e.X - startXPos, e.Y - startYPos); } break; default: break; } Painter.Image = paintImg; #endregion
startXPos = 0; startYPos = 0; } }
#region 基础操作按钮
private void OpenBtn_Click(object sender, EventArgs e) { if(openFileDialog.ShowDialog()==DialogResult.OK) { paintImg = (Bitmap)Image.FromFile(openFileDialog.FileName); Painter.Image = paintImg; } }
private void SaveBtn_Click(object sender, EventArgs e) { if(saveFileDialog.ShowDialog()==DialogResult.OK) { paintImg.Save(saveFileDialog.FileName, ImageFormat.Jpeg); } }
private void ClearBtn_Click(object sender, EventArgs e) { Graphics currGra = Graphics.FromImage(paintImg); currGra.Clear(Color.White); Painter.Image = paintImg; currGra.Dispose(); }
#endregion
#region 枚举当前笔刷 private void PencilBtn_Click(object sender, EventArgs e) { btn = Btns.Pencil; }
private void LineBtn_Click(object sender, EventArgs e) { btn = Btns.Line; }
private void CircleBtn_Click(object sender, EventArgs e) { btn = Btns.Circle; }
private void RectBtn_Click(object sender, EventArgs e) { btn = Btns.Rectangle; } private void EraserBtn_Click(object sender, EventArgs e) { btn = Btns.Eraser; } #endregion
#region 更改笔刷样式 private void ColorBtn_Click(object sender, EventArgs e) { ColorDialog colorDialog = new ColorDialog(); colorDialog.FullOpen = true; colorDialog.Color = drawColor; if(colorDialog.ShowDialog()==DialogResult.OK) { drawColor = colorDialog.Color; ColorBtn.BackColor = colorDialog.Color; } }
private void WidthBtn_Click(object sender, EventArgs e) { float width = (float)Convert.ToInt32(WidthText.Text); if (width != 0) drawWidth = width; }
#endregion
private void saveFileDialog_FileOk(object sender, CancelEventArgs e) {
}
private void openFileDialog_FileOk(object sender, CancelEventArgs e) {
} } }
|