Blueherb In solitude, where we are least alone

C# 我的小画板

我的画板

先看实现图

 

 实现过程

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
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 huaban
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            toolStrip1.Enabled = false;
            foreColor = Color.Black ;
            backColor = Color.White ;
        }
        Color backColor = new Color();
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(backColor);
            toolStrip1.Enabled = true;
            //创建一个Bitmap
            theImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            editFileName = "新建文件";
            //修改窗口标题
            this.Text = "MyDraw\t" + editFileName;
            ig = Graphics.FromImage(theImage);
            ig.Clear(backColor);
        }
        string editFileName = "";
        private Image theImage;
        public Graphics ig { get; private set; }
 
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Multiselect = false;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //修改窗口标题
                this.Text = "MyDraw\t" + openFileDialog1.FileName;
                editFileName = openFileDialog1.FileName;
                theImage = Image.FromFile(openFileDialog1.FileName);
                Graphics g = this.CreateGraphics();
                g.DrawImage(theImage, this.ClientRectangle);
                ig = Graphics.FromImage(theImage);
                ig.DrawImage(theImage, this.ClientRectangle);
                //ToolBar可以使用了
                toolStrip1.Enabled = true;
            }
        }
 
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "图像(*.bmp)|*.bmp";
            saveFileDialog1.FileName = editFileName;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                theImage.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
                this.Text = "MyDraw\t" + saveFileDialog1.FileName;
                editFileName = saveFileDialog1.FileName;
            }
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //将Image中保存的图像,绘制出来
            Graphics g = this.CreateGraphics();
            if (theImage != null)
            {
                g.Clear(Color.White);
                g.DrawImage(theImage, this.ClientRectangle);
            }
        }
        Color foreColor = new Color();
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
 
            if (e.Button == MouseButtons.Left)       
            {
                //如果选择文字输入,则打开strInput窗体
                if (drawTool == (int)drawTools.String)
                {
                    Frm_Text inputBox = new Frm_Text();
                    inputBox.StartPosition = FormStartPosition.CenterParent;
                    if (inputBox.ShowDialog() == DialogResult.OK)
                    {
                        Graphics g = this.CreateGraphics();
                        Font theFont = this.Font;
                        g.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
                        ig.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
                    }
                }
                //如果开始绘制,则开始记录鼠标位置
                else if ((isDrawing = !isDrawing) == true)
                {
                    startPoint = new Point(e.X, e.Y);
                    oldPoint = new Point(e.X, e.Y);
                }
            }
        }
        int  drawTool = 0;
        enum drawTools
        {
             None = 0,
             Pen = 1,
             Line = 2,
             Ellipse = 3,
             Rectangle = 4,
             String = 5,
             Rubber =6
        }
        Point oldPoint = new Point();
        Point startPoint = new Point();
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics g;
            g = this.CreateGraphics();
 
            if (isDrawing)
            {
                switch (drawTool)
                {
                    case (int)drawTools.None:
                        break;
                    case (int)drawTools.Pen:
                        //从上一个点到当前点绘制线段
                        g.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
                        ig.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
                        oldPoint.X = e.X;
                        oldPoint.Y = e.Y;
                        break;
                    case (int)drawTools.Line:
                        //首先恢复此次操作之前的图像,然后再添加Line
                        this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
                        g.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
                        break;
                    case (int)drawTools.Ellipse:
                        //首先恢复此次操作之前的图像,然后再添加Ellipse
                        this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
                        g.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                        break;
                    case (int)drawTools.Rectangle:
                        //首先恢复此次操作之前的图像,然后再添加Rectangle
                        this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
                        g.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                        break;
                    case (int)drawTools.String:
                        break;
                    case (int)drawTools.Rubber:
                        //用背景色绘制宽线段
                        g.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
                        ig.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
                        oldPoint.X = e.X;
                        oldPoint.Y = e.Y;
                        break;
                }
            }
        }
        bool isDrawing = false;
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
            switch (drawTool)
            {
                case (int)drawTools.Line:
                    ig.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
                    break;
                case (int)drawTools.Ellipse:
                    ig.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                    break;
                case (int)drawTools.Rectangle:
                    ig.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                    break;
            }
        }
 
        private void Frm_Main_Paint(object sender, PaintEventArgs e)
        {
            //将Image中保存的图像,绘制出来
            Graphics g = this.CreateGraphics();
            if (theImage != null)
            {
                g.Clear(Color.White);
                g.DrawImage(theImage, this.ClientRectangle);
            }
        }
 
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            toolStripButton6.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
            if (drawTool == (int)drawTools.Pen)
            {
                drawTool = (int)drawTools.None ;
                toolStripButton1.Checked = false ;
            }
            else
            {
                drawTool = (int)drawTools.Pen;
                toolStripButton1.Checked = true;
            }  
        }
 
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            toolStripButton1.Checked = false;
            toolStripButton6.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
            if (drawTool == (int)drawTools.Line)
            {
                drawTool = (int)drawTools.None;
                toolStripButton2.Checked = false;
            }
            else
            {
                drawTool = (int)drawTools.Line;
                toolStripButton2.Checked = true;
            }
        }
 
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
 
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton6.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
            if (drawTool == (int)drawTools.Rectangle)
            {
                drawTool = (int)drawTools.None;
                toolStripButton3.Checked = false;
            }
            else
            {
                drawTool = (int)drawTools.Rectangle;
                toolStripButton3.Checked = true;
            }          
        }
 
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton6.Checked = false;
            toolStripButton5.Checked = false;
            if (drawTool == (int)drawTools.Ellipse)
            {
                drawTool = (int)drawTools.None;
                toolStripButton4.Checked = false;
            }
            else
            {
                drawTool = (int)drawTools.Ellipse;
                toolStripButton4.Checked = true;
            
        }
 
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
 
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton6.Checked = false;
            if (drawTool == (int)drawTools.String)
            {
                drawTool = (int)drawTools.None;
                toolStripButton5.Checked = false;
            }
            else
            {
                drawTool = (int)drawTools.String;
                toolStripButton5.Checked = true;
            
        }
 
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
            if (drawTool == (int)drawTools.Rubber )
            {
                drawTool = (int)drawTools.None;
                toolStripButton6.Checked = false;
            }
            else
            {
                drawTool = (int)drawTools.Rubber;
                toolStripButton6.Checked = true;
            
        }
 
        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                foreColor = colorDialog1.Color  ;
            }
             
        }
    }
}

  

 金燕电子

 

posted @   孤燕  阅读(2520)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
 
点击右上角即可分享
微信分享提示