using System;
using System.Drawing;
using System.Windows.Forms;
class RandomClear: Form
{
public static void Main()
{
Application.Run(new RandomClear());
}
public RandomClear()
{
Text = "Random Clear";
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics graphics = pea.Graphics;
Random rand = new Random();
graphics.Clear(Color.FromArgb(rand.Next(256),
rand.Next(256),
rand.Next(256)));
}
}
2D Graphics
Graphics: MeasureString
using System;
using System.Drawing;
using System.Windows.Forms;
class SysInfoColumns: Form
{
public static void Main()
{
Application.Run(new SysInfoColumns());
}
public SysInfoColumns()
{
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
Brush brush = new SolidBrush(ForeColor);
SizeF sizef;
float cxCol, y = 0;
int cySpace;
sizef = grfx.MeasureString("ArrangeStartingPosition ", Font);
cxCol = sizef.Width;
cySpace = Font.Height;
grfx.DrawString("ArrangeDirection", Font, brush, 0, y);
grfx.DrawString(SystemInformation.ArrangeDirection.ToString(),
Font, brush, cxCol, y);
y += cySpace;
grfx.DrawString("ArrangeStartingPosition", Font, brush, 0, y);
grfx.DrawString(
SystemInformation.ArrangeStartingPosition.ToString(),
Font, brush, cxCol, y);
y += cySpace;
grfx.DrawString("BootMode", Font, brush, 0, y);
grfx.DrawString(SystemInformation.BootMode.ToString(),
Font, brush, cxCol, y);
y += cySpace;
grfx.DrawString("Border3DSize", Font, brush, 0, y);
grfx.DrawString(SystemInformation.Border3DSize.ToString(),
Font, brush, cxCol, y);
y += cySpace;
grfx.DrawString("BorderSize", Font, brush, 0, y);
grfx.DrawString(SystemInformation.BorderSize.ToString(),
Font, brush, cxCol, y);
y += cySpace;
grfx.DrawString("CaptionButtonSize", Font, brush, 0, y);
grfx.DrawString(SystemInformation.CaptionButtonSize.ToString(),
Font, brush, cxCol, y);
}
}
Bezier Art: DrawBeziers
using System;
using System.Drawing;
using System.Windows.Forms;
class BezierArt: Form
{
public static void Main()
{
Application.Run(new BezierArt());
}
public BezierArt()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,200, 200);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Pen pen = new Pen(clr);
PointF[] aptf = new PointF[4];
int iNum = 100;
for (int i = 0; i < iNum; i++) { double dAngle = 2 * i * Math.PI / iNum; aptf[0].X = cx / 2 + cx / 2 * (float) Math.Cos(dAngle); aptf[0].Y = 5 * cy / 8 + cy / 16 * (float) Math.Sin(dAngle); aptf[1] = new PointF(cx / 2, -cy); aptf[2] = new PointF(cx / 2, 2 * cy); dAngle += Math.PI; aptf[3].X = cx / 2 + cx / 4 * (float) Math.Cos(dAngle); aptf[3].Y = cy / 2 + cy / 16 * (float) Math.Sin(dAngle); grfx.DrawBeziers(pen, aptf); } } } [/csharp]
Graphics: FillClosedCurve
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class ClosedCurveFillModes: Form
{
public static void Main()
{
Application.Run(new ClosedCurveFillModes());
}
ClosedCurveFillModes()
{
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)
{
Brush brush = new SolidBrush(clr);
Point[] apt = new Point[5];
for (int i = 0; i < apt.Length; i++) { double dAngle = (i * 0.8 - 0.5) * Math.PI; apt[i] = new Point( (int)(cx *(0.25 + 0.24 * Math.Cos(dAngle))), (int)(cy *(0.50 + 0.48 * Math.Sin(dAngle)))); } grfx.FillClosedCurve(brush, apt, FillMode.Alternate); for (int i = 0; i < apt.Length; i++) apt[i].X += cx / 2; grfx.FillClosedCurve(brush, apt, FillMode.Winding); } } [/csharp]
Line and Arc Combo
using System;
using System.Drawing;
using System.Windows.Forms;
class LineArcCombo: Form
{
public static void Main()
{
Application.Run(new LineArcCombo());
}
public LineArcCombo()
{
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)
{
Pen pen = new Pen(clr, 25);
grfx.DrawLine(pen, 25, 100, 125, 100);
grfx.DrawArc (pen, 125, 50, 100, 100, -180, 180);
grfx.DrawLine(pen, 225, 100, 325, 100);
}
}
Wide Polyline: DrawLines
using System;
using System.Drawing;
using System.Windows.Forms;
class WidePolyline: Form
{
public static void Main()
{
Application.Run(new WidePolyline());
}
public WidePolyline()
{
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)
{
Pen pen = new Pen(clr, 25);
grfx.DrawLines(pen, new Point[] {
new Point( 25, 100), new Point(125, 100),
new Point(125, 50), new Point(225, 50),
new Point(225, 100), new Point(325, 100) });
}
}
Graphics: FillEllipse
using System;
using System.Drawing;
using System.Windows.Forms;
class FourByFours: Form
{
public static void Main()
{
Application.Run(new FourByFours());
}
public FourByFours()
{
Text = "Four by Fours";
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)
{
Pen pen = new Pen(clr);
Brush brush = new SolidBrush(clr);
grfx.FillEllipse(brush, new Rectangle(14, 8, 4, 4));
}
}