using System; using System.Drawing; using System.Windows.Forms; class Bezier: Form { protected Point[] apt = new Point[4]; public static void Main() { Application.Run(new Bezier()); } public Bezier() { ResizeRedraw = true; OnResize(EventArgs.Empty); } protected override void OnResize(EventArgs ea) { base.OnResize(ea); int cx = ClientSize.Width; int cy = ClientSize.Height; apt[0] = new Point( cx / 4, cy / 2); apt[1] = new Point( cx / 2, cy / 4); apt[2] = new Point( cx / 2, 3 * cy / 4); apt[3] = new Point(3 * cx / 4, cy / 2); } protected override void OnMouseDown(MouseEventArgs mea) { Point pt; if (mea.Button == MouseButtons.Left) pt = apt[1]; else if (mea.Button == MouseButtons.Right) pt = apt[2]; else return; Cursor.Position = PointToScreen(pt); } protected override void OnMouseMove(MouseEventArgs mea) { if (mea.Button == MouseButtons.Left) { apt[1] = new Point(mea.X, mea.Y); Invalidate(); } else if (mea.Button == MouseButtons.Right) { apt[2] = new Point(mea.X, mea.Y); Invalidate(); } } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; grfx.DrawBeziers(new Pen(ForeColor), apt); Pen pen = new Pen(Color.FromArgb(0x80, ForeColor)); grfx.DrawLine(pen, apt[0], apt[1]); grfx.DrawLine(pen, apt[2], apt[3]); } }
2D Graphics
Spiral
using System;
using System.Drawing;
using System.Windows.Forms;
class Spiral: Form
{
public static void Main()
{
Application.Run(new Spiral());
}
public Spiral()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
const int iNumRevs = 20;
int iNumPoints = iNumRevs * 2 * (cx + cy);
PointF[] aptf = new PointF[iNumPoints];
float fAngle, fScale;
for (int i = 0; i < iNumPoints; i++) { fAngle = (float)(i * 2 * Math.PI /(iNumPoints / iNumRevs)); fScale = 1 - (float)i / iNumPoints; aptf[i].X = (float)(cx / 2 * (1 + fScale * Math.Cos(fAngle))); aptf[i].Y = (float)(cy / 2 * (1 + fScale * Math.Sin(fAngle))); } grfx.DrawLines(new Pen(clr), aptf); } } [/csharp]
Ellipse with DrawLines
using System;
using System.Drawing;
using System.Windows.Forms;
class PolyEllipse: Form
{
public static void Main()
{
Application.Run(new PolyEllipse());
}
public PolyEllipse()
{
Text = “Ellipse with DrawLines”;
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
int iNum = 2 * (cx + cy);
PointF[] aptf = new PointF[iNum];
for (int i = 0; i < iNum; i++) { double dAng = i * 2 * Math.PI / (iNum - 1); aptf[i].X = (cx - 1) / 2f * (1 + (float)Math.Cos(dAng)); aptf[i].Y = (cy - 1) / 2f * (1 + (float)Math.Sin(dAng)); } grfx.DrawLines(new Pen(clr), aptf); } } [/csharp]
Sine Curve
using System;
using System.Drawing;
using System.Windows.Forms;
class SineCurve: Form
{
public static void Main()
{
Application.Run(new SineCurve());
}
public SineCurve()
{
Text = “Sine Curve”;
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
PointF[] aptf = new PointF[cx];
for (int i = 0; i < cx; i++) { aptf[i].X = i; aptf[i].Y = cy / 2 * (1 -(float) Math.Sin(i * 2 * Math.PI / (cx - 1))); } grfx.DrawLines(new Pen(clr), aptf); } } [/csharp]
TranslateTransform: 100,100
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); SetStyle(ControlStyles.ResizeRedraw, true); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(400, 400); this.Text = ""; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; g.PageUnit = GraphicsUnit.Pixel; Point renderingOrgPt = new Point(0,0); renderingOrgPt.X = 100; renderingOrgPt.Y = 100; g.TranslateTransform(renderingOrgPt.X, renderingOrgPt.Y); g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100); g.Dispose(); } }
Coordiate Translate Transform
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); SetStyle(ControlStyles.ResizeRedraw, true); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(400, 400); this.Text = ""; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; g.PageUnit = GraphicsUnit.Inch; Point renderingOrgPt = new Point(0,0); renderingOrgPt.X = 1; renderingOrgPt.Y = 1; g.TranslateTransform(renderingOrgPt.X,renderingOrgPt.Y); g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100); g.Dispose(); } }
Return a randomly-generated color
// crudwork // Copyright 2004 by Steve T. Pham (http://www.crudwork.com) // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with This program. If not, see <http://www.gnu.org/licenses/>. using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace crudwork.Utilities { /// <summary> /// Color Utility /// </summary> public static class ColorUtil { private static Random random = new Random(); private static int r; private static int g; private static int b; /// <summary> /// Return a randomly-generated color /// </summary> /// <returns></returns> public static Color GetRandomColor() { r = random.Next(0, 256); g = random.Next(0, 256); b = random.Next(0, 256); return Color.FromArgb(r, g, b); } } }