namespace Painter
{
partial class FormPainter
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripComboBoxMode = new System.Windows.Forms.ToolStripComboBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.toolStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripComboBoxMode});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(468, 25);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripComboBoxMode
//
this.toolStripComboBoxMode.Items.AddRange(new object[] {
"画笔",
"直线",
"矩形",
"圆形"});
this.toolStripComboBoxMode.Name = "toolStripComboBoxMode";
this.toolStripComboBoxMode.Size = new System.Drawing.Size(75, 25);
this.toolStripComboBoxMode.Text = "画笔";
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(0, 28);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(456, 381);
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.FormPainter_MouseDown);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.FormPainter_MouseMove);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.FormPainter_MouseUp);
//
// FormPainter
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(468, 421);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.toolStrip1);
this.Name = "FormPainter";
this.Text = "Form1";
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripComboBox toolStripComboBoxMode;
private System.Windows.Forms.PictureBox pictureBox1;
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Painter
{
public partial class FormPainter : Form
{
Graphics g;
Bitmap img;
Bitmap buf;
Pen pen = new Pen(Color.Black, 3);
public FormPainter()
{
InitializeComponent();
img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g = Graphics.FromImage(img);
}
bool isMouseDown = false;
List<Point> points = new List<Point>();
private void FormPainter_MouseDown(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
isMouseDown = true;
points.Clear();
points.Add(p);
pictureBox1.Image = img;
}
private void FormPainter_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
img = buf; // 将上次 MouseMove 画的暂存结果取回
pictureBox1.Image = img; // 然后显示出來
}
private void FormPainter_MouseMove(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
buf = new Bitmap(img); // 建立一个新的 buf 缓存区,画的时候
Graphics g = Graphics.FromImage(buf);
if (points.Count > 0 && isMouseDown)
{
Point pStart = points[0];
Point pLast = points[points.Count - 1];
if (toolStripComboBoxMode.Text.Equals("画笔"))
{
Point p0 = pStart;
foreach (Point p1 in points)
{
g.DrawLine(pen, p0, p1);
p0 = p1;
}
}
else if (toolStripComboBoxMode.Text.Equals("直线"))
{
g.DrawLine(pen, pStart, p);
}
else if (toolStripComboBoxMode.Text.Equals("矩形"))
{
g.DrawRectangle(pen, pStart.X, pStart.Y, p.X - pStart.X, p.Y - pStart.Y);
}
else if (toolStripComboBoxMode.Text.Equals("圆形"))
{
g.DrawEllipse(pen, pStart.X, pStart.Y, p.X - pStart.X, p.Y - pStart.Y);
}
}
points.Add(p);
pictureBox1.Image = buf;
}
//...
}
}