C # Drawing and redrawing with the "rubber Strip" method

Source: Internet
Author: User
Tags abs foreach bool empty
Some days ago in the forum posted a post, 100 points to seek the "rubber Strip" method drawing code. The effect is not very good, so they refer to the code to the user to write a new, solve the problem of drawing and redrawing. Because only write part, so the function is limited, at the same time perhaps the algorithm is not very good, hope everybody pointing!! The form contains only one pictrueBox1, first putting the code down:
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Drawing.Drawing2D;

Namespace Gdi_ Practice
{
<summary>
Summary description of the 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>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</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>
The main entry point for the application.
</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));

}
}



}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.