简单易用的C#画板程序,适合初学者学习。这个画板包含基本的绘图功能,如画笔、橡皮擦、形状绘制等。
1. 主窗体设计 (Form1.Designer.cs)
csharp
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Panel panelToolBox;
private System.Windows.Forms.Panel panelCanvas;
private System.Windows.Forms.Button btnPencil;
private System.Windows.Forms.Button btnLine;
private System.Windows.Forms.Button btnRectangle;
private System.Windows.Forms.Button btnEllipse;
private System.Windows.Forms.Button btnEraser;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnColor;
private System.Windows.Forms.NumericUpDown numSize;
private System.Windows.Forms.PictureBox pictureBoxCanvas;
private System.Windows.Forms.ColorDialog colorDialog;
private System.Windows.Forms.Label lblTool;
private System.Windows.Forms.Label lblSize;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnOpen;
private System.Windows.Forms.SaveFileDialog saveFileDialog;
private System.Windows.Forms.OpenFileDialog openFileDialog;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.panelToolBox = new System.Windows.Forms.Panel();
this.btnSave = new System.Windows.Forms.Button();
this.btnOpen = new System.Windows.Forms.Button();
this.lblSize = new System.Windows.Forms.Label();
this.lblTool = new System.Windows.Forms.Label();
this.numSize = new System.Windows.Forms.NumericUpDown();
this.btnColor = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btnEraser = new System.Windows.Forms.Button();
this.btnEllipse = new System.Windows.Forms.Button();
this.btnRectangle = new System.Windows.Forms.Button();
this.btnLine = new System.Windows.Forms.Button();
this.btnPencil = new System.Windows.Forms.Button();
this.panelCanvas = new System.Windows.Forms.Panel();
this.pictureBoxCanvas = new System.Windows.Forms.PictureBox();
this.colorDialog = new System.Windows.Forms.ColorDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.panelToolBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.numSize)).BeginInit();
this.panelCanvas.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCanvas)).BeginInit();
this.SuspendLayout();
// panelToolBox
this.panelToolBox.BackColor = System.Drawing.SystemColors.ControlDark;
this.panelToolBox.Controls.Add(this.btnSave);
this.panelToolBox.Controls.Add(this.btnOpen);
this.panelToolBox.Controls.Add(this.lblSize);
this.panelToolBox.Controls.Add(this.lblTool);
this.panelToolBox.Controls.Add(this.numSize);
this.panelToolBox.Controls.Add(this.btnColor);
this.panelToolBox.Controls.Add(this.btnClear);
this.panelToolBox.Controls.Add(this.btnEraser);
this.panelToolBox.Controls.Add(this.btnEllipse);
this.panelToolBox.Controls.Add(this.btnRectangle);
this.panelToolBox.Controls.Add(this.btnLine);
this.panelToolBox.Controls.Add(this.btnPencil);
this.panelToolBox.Dock = System.Windows.Forms.DockStyle.Left;
this.panelToolBox.Location = new System.Drawing.Point(0, 0);
this.panelToolBox.Name = "panelToolBox";
this.panelToolBox.Size = new System.Drawing.Size(120, 600);
this.panelToolBox.TabIndex = 0;
// btnSave
this.btnSave.Location = new System.Drawing.Point(10, 350);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(100, 30);
this.btnSave.TabIndex = 11;
this.btnSave.Text = "保存";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
// btnOpen
this.btnOpen.Location = new System.Drawing.Point(10, 310);
this.btnOpen.Name = "btnOpen";
this.btnOpen.Size = new System.Drawing.Size(100, 30);
this.btnOpen.TabIndex = 10;
this.btnOpen.Text = "打开";
this.btnOpen.UseVisualStyleBackColor = true;
this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
// lblSize
this.lblSize.AutoSize = true;
this.lblSize.Location = new System.Drawing.Point(10, 250);
this.lblSize.Name = "lblSize";
this.lblSize.Size = new System.Drawing.Size(41, 12);
this.lblSize.TabIndex = 9;
this.lblSize.Text = "粗细:";
// lblTool
this.lblTool.AutoSize = true;
this.lblTool.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
this.lblTool.Location = new System.Drawing.Point(10, 10);
this.lblTool.Name = "lblTool";
this.lblTool.Size = new System.Drawing.Size(57, 12);
this.lblTool.TabIndex = 8;
this.lblTool.Text = "绘图工具";
// numSize
this.numSize.Location = new System.Drawing.Point(10, 270);
this.numSize.Minimum = new decimal(new int[] {1, 0, 0, 0});
this.numSize.Name = "numSize";
this.numSize.Size = new System.Drawing.Size(100, 21);
this.numSize.TabIndex = 7;
this.numSize.Value = new decimal(new int[] {3, 0, 0, 0});
// btnColor
this.btnColor.BackColor = System.Drawing.Color.Black;
this.btnColor.Location = new System.Drawing.Point(10, 220);
this.btnColor.Name = "btnColor";
this.btnColor.Size = new System.Drawing.Size(100, 30);
this.btnColor.TabIndex = 6;
this.btnColor.UseVisualStyleBackColor = false;
this.btnColor.Click += new System.EventHandler(this.btnColor_Click);
// btnClear
this.btnClear.Location = new System.Drawing.Point(10, 180);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(100, 30);
this.btnClear.TabIndex = 5;
this.btnClear.Text = "清空画板";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
// btnEraser
this.btnEraser.Location = new System.Drawing.Point(10, 150);
this.btnEraser.Name = "btnEraser";
this.btnEraser.Size = new System.Drawing.Size(100, 30);
this.btnEraser.TabIndex = 4;
this.btnEraser.Text = "橡皮擦";
this.btnEraser.UseVisualStyleBackColor = true;
this.btnEraser.Click += new System.EventHandler(this.btnEraser_Click);
// btnEllipse
this.btnEllipse.Location = new System.Drawing.Point(10, 120);
this.btnEllipse.Name = "btnEllipse";
this.btnEllipse.Size = new System.Drawing.Size(100, 30);
this.btnEllipse.TabIndex = 3;
this.btnEllipse.Text = "椭圆";
this.btnEllipse.UseVisualStyleBackColor = true;
this.btnEllipse.Click += new System.EventHandler(this.btnEllipse_Click);
// btnRectangle
this.btnRectangle.Location = new System.Drawing.Point(10, 90);
this.btnRectangle.Name = "btnRectangle";
this.btnRectangle.Size = new System.Drawing.Size(100, 30);
this.btnRectangle.TabIndex = 2;
this.btnRectangle.Text = "矩形";
this.btnRectangle.UseVisualStyleBackColor = true;
this.btnRectangle.Click += new System.EventHandler(this.btnRectangle_Click);
// btnLine
this.btnLine.Location = new System.Drawing.Point(10, 60);
this.btnLine.Name = "btnLine";
this.btnLine.Size = new System.Drawing.Size(100, 30);
this.btnLine.TabIndex = 1;
this.btnLine.Text = "直线";
this.btnLine.UseVisualStyleBackColor = true;
this.btnLine.Click += new System.EventHandler(this.btnLine_Click);
// btnPencil
this.btnPencil.Location = new System.Drawing.Point(10, 30);
this.btnPencil.Name = "btnPencil";
this.btnPencil.Size = new System.Drawing.Size(100, 30);
this.btnPencil.TabIndex = 0;
this.btnPencil.Text = "铅笔";
this.btnPencil.UseVisualStyleBackColor = true;
this.btnPencil.Click += new System.EventHandler(this.btnPencil_Click);
// panelCanvas
this.panelCanvas.BackColor = System.Drawing.SystemColors.AppWorkspace;
this.panelCanvas.Controls.Add(this.pictureBoxCanvas);
this.panelCanvas.Dock = System.Windows.Forms.DockStyle.Fill;
this.panelCanvas.Location = new System.Drawing.Point(120, 0);
this.panelCanvas.Name = "panelCanvas";
this.panelCanvas.Size = new System.Drawing.Size(680, 600);
this.panelCanvas.TabIndex = 1;
// pictureBoxCanvas
this.pictureBoxCanvas.BackColor = System.Drawing.Color.White;
this.pictureBoxCanvas.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCanvas.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCanvas.Name = "pictureBoxCanvas";
this.pictureBoxCanvas.Size = new System.Drawing.Size(680, 600);
this.pictureBoxCanvas.TabIndex = 0;
this.pictureBoxCanvas.TabStop = false;
this.pictureBoxCanvas.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBoxCanvas_MouseDown);
this.pictureBoxCanvas.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBoxCanvas_MouseMove);
this.pictureBoxCanvas.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBoxCanvas_MouseUp);
// saveFileDialog
this.saveFileDialog.Filter = "PNG 图片|*.png|JPEG 图片|*.jpg|BMP 图片|*.bmp";
// openFileDialog
this.openFileDialog.Filter = "图片文件|*.png;*.jpg;*.bmp";
// Form1
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 600);
this.Controls.Add(this.panelCanvas);
this.Controls.Add(this.panelToolBox);
this.Name = "Form1";
this.Text = "简易画板";
this.Load += new System.EventHandler(this.Form1_Load);
this.panelToolBox.ResumeLayout(false);
this.panelToolBox.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.numSize)).EndInit();
this.panelCanvas.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCanvas)).EndInit();
this.ResumeLayout(false);
}
}
2. 主窗体代码 (Form1.cs)
csharp
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
namespace SimplePaint
{
public partial class Form1 : Form
{
// 绘图工具枚举
private enum DrawingTool
{
Pencil,
Line,
Rectangle,
Ellipse,
Eraser
}
// 变量声明
private DrawingTool currentTool = DrawingTool.Pencil;
private Bitmap canvasBitmap;
private Graphics canvasGraphics;
private Pen currentPen;
private Pen eraserPen;
private Point startPoint;
private Point endPoint;
private bool isDrawing = false;
private Bitmap tempBitmap;
private Graphics tempGraphics;
public Form1()
{
InitializeComponent();
InitializeDrawingTools();
}
private void InitializeDrawingTools()
{
// 初始化画笔
currentPen = new Pen(Color.Black, (float)numSize.Value);
currentPen.StartCap = LineCap.Round;
currentPen.EndCap = LineCap.Round;
// 初始化橡皮擦(白色画笔)
eraserPen = new Pen(Color.White, (float)numSize.Value);
eraserPen.StartCap = LineCap.Round;
eraserPen.EndCap = LineCap.Round;
// 初始化画布
InitializeCanvas();
}
private void InitializeCanvas()
{
// 创建画布位图
canvasBitmap = new Bitmap(pictureBoxCanvas.Width, pictureBoxCanvas.Height);
canvasGraphics = Graphics.FromImage(canvasBitmap);
canvasGraphics.SmoothingMode = SmoothingMode.AntiAlias;
canvasGraphics.Clear(Color.White);
// 设置PictureBox的图像
pictureBoxCanvas.Image = canvasBitmap;
}
private void Form1_Load(object sender, EventArgs e)
{
// 窗体加载时的初始化
UpdateToolSelection();
}
private void UpdateToolSelection()
{
// 更新工具按钮的选中状态
btnPencil.BackColor = (currentTool == DrawingTool.Pencil) ? SystemColors.ControlLight : SystemColors.Control;
btnLine.BackColor = (currentTool == DrawingTool.Line) ? SystemColors.ControlLight : SystemColors.Control;
btnRectangle.BackColor = (currentTool == DrawingTool.Rectangle) ? SystemColors.ControlLight : SystemColors.Control;
btnEllipse.BackColor = (currentTool == DrawingTool.Ellipse) ? SystemColors.ControlLight : SystemColors.Control;
btnEraser.BackColor = (currentTool == DrawingTool.Eraser) ? SystemColors.ControlLight : SystemColors.Control;
}
// 工具按钮点击事件
private void btnPencil_Click(object sender, EventArgs e)
{
currentTool = DrawingTool.Pencil;
UpdateToolSelection();
}
private void btnLine_Click(object sender, EventArgs e)
{
currentTool = DrawingTool.Line;
UpdateToolSelection();
}
private void btnRectangle_Click(object sender, EventArgs e)
{
currentTool = DrawingTool.Rectangle;
UpdateToolSelection();
}
private void btnEllipse_Click(object sender, EventArgs e)
{
currentTool = DrawingTool.Ellipse;
UpdateToolSelection();
}
private void btnEraser_Click(object sender, EventArgs e)
{
currentTool = DrawingTool.Eraser;
UpdateToolSelection();
}
private void btnColor_Click(object sender, EventArgs e)
{
// 颜色选择对话框
if (colorDialog.ShowDialog() == DialogResult.OK)
{
currentPen.Color = colorDialog.Color;
btnColor.BackColor = colorDialog.Color;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
// 清空画布
if (MessageBox.Show("确定要清空画板吗?", "确认",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
canvasGraphics.Clear(Color.White);
pictureBoxCanvas.Invalidate();
}
}
private void btnSave_Click(object sender, EventArgs e)
{
// 保存图片
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
string extension = Path.GetExtension(saveFileDialog.FileName).ToLower();
System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Png;
switch (extension)
{
case ".jpg":
case ".jpeg":
format = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case ".bmp":
format = System.Drawing.Imaging.ImageFormat.Bmp;
break;
}
canvasBitmap.Save(saveFileDialog.FileName, format);
MessageBox.Show("图片保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("保存失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
// 打开图片
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
Bitmap openedImage = new Bitmap(openFileDialog.FileName);
canvasGraphics.DrawImage(openedImage, 0, 0, pictureBoxCanvas.Width, pictureBoxCanvas.Height);
pictureBoxCanvas.Invalidate();
}
catch (Exception ex)
{
MessageBox.Show("打开图片失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
// 鼠标事件处理
private void pictureBoxCanvas_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = true;
startPoint = e.Location;
// 对于非铅笔工具,创建临时画布用于预览
if (currentTool != DrawingTool.Pencil && currentTool != DrawingTool.Eraser)
{
tempBitmap = new Bitmap(canvasBitmap);
tempGraphics = Graphics.FromImage(tempBitmap);
tempGraphics.SmoothingMode = SmoothingMode.AntiAlias;
}
}
}
private void pictureBoxCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
endPoint = e.Location;
switch (currentTool)
{
case DrawingTool.Pencil:
// 铅笔工具 - 直接绘制
canvasGraphics.DrawLine(currentPen, startPoint, endPoint);
startPoint = endPoint;
pictureBoxCanvas.Invalidate();
break;
case DrawingTool.Eraser:
// 橡皮擦工具 - 直接绘制
canvasGraphics.DrawLine(eraserPen, startPoint, endPoint);
startPoint = endPoint;
pictureBoxCanvas.Invalidate();
break;
case DrawingTool.Line:
case DrawingTool.Rectangle:
case DrawingTool.Ellipse:
// 其他工具 - 在临时画布上预览
if (tempBitmap != null)
{
// 恢复原始图像
tempGraphics.DrawImage(canvasBitmap, 0, 0);
// 绘制预览图形
DrawPreview(tempGraphics);
pictureBoxCanvas.Image = tempBitmap;
}
break;
}
}
}
private void pictureBoxCanvas_MouseUp(object sender, MouseEventArgs e)
{
if (isDrawing && e.Button == MouseButtons.Left)
{
isDrawing = false;
endPoint = e.Location;
// 在最终画布上绘制图形
switch (currentTool)
{
case DrawingTool.Line:
case DrawingTool.Rectangle:
case DrawingTool.Ellipse:
DrawPreview(canvasGraphics);
pictureBoxCanvas.Image = canvasBitmap;
// 释放临时资源
if (tempGraphics != null)
{
tempGraphics.Dispose();
tempBitmap.Dispose();
tempGraphics = null;
tempBitmap = null;
}
break;
}
pictureBoxCanvas.Invalidate();
}
}
private void DrawPreview(Graphics g)
{
// 计算矩形区域
int x = Math.Min(startPoint.X, endPoint.X);
int y = Math.Min(startPoint.Y, endPoint.Y);
int width = Math.Abs(endPoint.X - startPoint.X);
int height = Math.Abs(endPoint.Y - startPoint.Y);
// 根据当前工具绘制图形
switch (currentTool)
{
case DrawingTool.Line:
g.DrawLine(currentPen, startPoint, endPoint);
break;
case DrawingTool.Rectangle:
g.DrawRectangle(currentPen, x, y, width, height);
break;
case DrawingTool.Ellipse:
g.DrawEllipse(currentPen, x, y, width, height);
break;
}
}
// 画笔粗细改变事件
private void numSize_ValueChanged(object sender, EventArgs e)
{
currentPen.Width = (float)numSize.Value;
eraserPen.Width = (float)numSize.Value;
}
// 窗体大小改变时调整画布
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (canvasBitmap != null && pictureBoxCanvas.Width > 0 && pictureBoxCanvas.Height > 0)
{
// 创建新的画布
Bitmap newBitmap = new Bitmap(pictureBoxCanvas.Width, pictureBoxCanvas.Height);
Graphics newGraphics = Graphics.FromImage(newBitmap);
newGraphics.SmoothingMode = SmoothingMode.AntiAlias;
newGraphics.Clear(Color.White);
// 将旧画布内容绘制到新画布上
newGraphics.DrawImage(canvasBitmap, 0, 0);
// 更新引用
canvasGraphics.Dispose();
canvasBitmap.Dispose();
canvasBitmap = newBitmap;
canvasGraphics = newGraphics;
pictureBoxCanvas.Image = canvasBitmap;
}
}
// 资源释放
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
if (canvasGraphics != null) canvasGraphics.Dispose();
if (canvasBitmap != null) canvasBitmap.Dispose();
if (currentPen != null) currentPen.Dispose();
if (eraserPen != null) eraserPen.Dispose();
if (tempGraphics != null) tempGraphics.Dispose();
if (tempBitmap != null) tempBitmap.Dispose();
}
}
}
3. 程序入口点 (Program.cs)
csharp
using System;
using System.Windows.Forms;
namespace SimplePaint
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
4. 项目配置文件 (SimplePaint.csproj)
xml
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>paint.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>
</Project>
5. 扩展功能类 - 添加文本工具
csharp
// TextToolForm.cs - 文本输入窗体
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SimplePaint
{
public partial class TextToolForm : Form
{
public string EnteredText { get; private set; }
public Font SelectedFont { get; private set; }
public Color SelectedColor { get; private set; }
public TextToolForm()
{
InitializeComponent();
SelectedFont = new Font("Arial", 12);
SelectedColor = Color.Black;
}
private void InitializeComponent()
{
this.txtText = new TextBox();
this.btnFont = new Button();
this.btnColor = new Button();
this.btnOK = new Button();
this.btnCancel = new Button();
this.fontDialog = new FontDialog();
this.colorDialog = new ColorDialog();
this.SuspendLayout();
// txtText
this.txtText.Location = new Point(10, 10);
this.txtText.Multiline = true;
this.txtText.Size = new Size(300, 100);
this.txtText.TabIndex = 0;
// btnFont
this.btnFont.Location = new Point(10, 120);
this.btnFont.Size = new Size(100, 30);
this.btnFont.TabIndex = 1;
this.btnFont.Text = "选择字体";
this.btnFont.Click += new EventHandler(this.btnFont_Click);
// btnColor
this.btnColor.Location = new Point(120, 120);
this.btnColor.Size = new Size(100, 30);
this.btnColor.TabIndex = 2;
this.btnColor.Text = "选择颜色";
this.btnColor.Click += new EventHandler(this.btnColor_Click);
// btnOK
this.btnOK.Location = new Point(150, 160);
this.btnOK.Size = new Size(75, 30);
this.btnOK.TabIndex = 3;
this.btnOK.Text = "确定";
this.btnOK.Click += new EventHandler(this.btnOK_Click);
// btnCancel
this.btnCancel.Location = new Point(235, 160);
this.btnCancel.Size = new Size(75, 30);
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "取消";
this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
// TextToolForm
this.ClientSize = new Size(320, 200);
this.Controls.AddRange(new Control[] {
this.txtText, this.btnFont, this.btnColor, this.btnOK, this.btnCancel});
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.StartPosition = FormStartPosition.CenterParent;
this.Text = "添加文本";
this.ResumeLayout(false);
}
private void btnFont_Click(object sender, EventArgs e)
{
if (fontDialog.ShowDialog() == DialogResult.OK)
{
SelectedFont = fontDialog.Font;
}
}
private void btnColor_Click(object sender, EventArgs e)
{
if (colorDialog.ShowDialog() == DialogResult.OK)
{
SelectedColor = colorDialog.Color;
}
}
private void btnOK_Click(object sender, EventArgs e)
{
EnteredText = txtText.Text;
DialogResult = DialogResult.OK;
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}
参考项目 C#入门级画板示例源码(画图板) www.3dddown.com/csa/62386.html
说明
功能特性:
- 基本绘图工具:铅笔、直线、矩形、椭圆、橡皮擦
- 颜色选择:可自定义画笔颜色
- 粗细调整:可调整画笔和橡皮擦的粗细
- 文件操作:支持打开、保存图片(PNG、JPEG、BMP格式)
- 清空画板:一键清空所有绘图内容
- 实时预览:绘制形状时有实时预览效果
编译和运行:
- 使用Visual Studio创建新的Windows Forms项目
- 将上述代码文件添加到项目中
- 编译并运行程序
学习要点:
- Windows Forms基础控件使用
- GDI+绘图技术
- 鼠标事件处理
- 图形图像处理
- 文件对话框操作