C#中用“橡皮條”法繪圖和重繪
最後更新:2017-02-28
來源:互聯網
上載者:User
前些日子在論壇上發了個文章,100分尋求“橡皮條”法繪圖的代碼。效果不是很好,於是自己參照網友給的代碼重新寫了一個,解決了繪圖與重繪的問題。由於唯寫了部分,所以功能有限,同時可能演算法不是很好,希望大家指點!!表單中僅包含一個pictrueBox1,先將代碼付諸於下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace GDI_練習
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.PictureBox pictureBox1;
private Point p1 = Point.Empty, p2 = Point.Empty;
private bool isMouseDown = false, isMouseUp = false;
ArrayList addArray = new ArrayList();
public struct SharpType
{
public string type;
public Point p1, p2;
public Color foreColor, backColor;
public Brush brush;
public SharpType( string type, Point p1, Point p2, Color foreColor, Color backColor, Brush brush )
{
this.type = type;
this.p1 = p1;
this.p2 = p2;
this.foreColor = foreColor;
this.backColor = backColor;
this.brush = brush;
}
}
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 表單設計器支援所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 表單設計器產生的程式碼
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.White;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(432, 397);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 397);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if( ! isMouseUp )
{
this.isMouseDown = true;
this.p1 = new Point( e.X, e.Y );
}
}
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Graphics g = this.pictureBox1.CreateGraphics();
if( isMouseDown && p2 != Point.Empty )
g.DrawEllipse( Pens.White, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
if( isMouseDown && ! isMouseUp )
{
p2 = new Point( e.X, e.Y );
g.DrawEllipse( Pens.Black, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
}
foreach( SharpType type in addArray )
{
g.DrawEllipse( Pens.Black, type.p1.X, type.p1.Y, Math.Abs( type.p1.X - type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );
}
g.Dispose();
}
private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.isMouseDown = false;
p2 = new Point( e.X, e.Y );
Graphics g = this.pictureBox1.CreateGraphics();
g.DrawEllipse( Pens.Black, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
addArray.Add( new SharpType( "a", p1, p2, Color.Black, Color.Empty, Brushes.Black ) );
p1 = Point.Empty;
p2 = Point.Empty;
g.Dispose();
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
foreach( SharpType type in addArray )
{
e.Graphics.DrawEllipse( Pens.Black, type.p1.X, type.p1.Y, Math.Abs( type.p1.X - type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );
}
}
}
}