Draw Ellipse

image_pdfimage_print
   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class ClientEllipse: Form
{
     public static void Main()
     {
          Application.Run(new ClientEllipse());
     }
     public ClientEllipse()
     {
          Text = "Client Ellipse";
          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)
     {
          grfx.DrawEllipse(new Pen(clr), 0, 0, cx - 1, cy - 1);
     }
}

    


Graphics: DrawRectangle

image_pdfimage_print
   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class OutlineClientRectangle: Form
{
     public static void Main()
     {
          Application.Run(new OutlineClientRectangle());
     }
     public OutlineClientRectangle()
     {
          Text = "Client Rectangle";
          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)
     {
          grfx.DrawRectangle(Pens.Red, 0, 0, cx - 1, cy - 1);
     }
}

    


Graphics: DrawRectangles

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class FourByFours: Form
{
     public static void Main()
     {
          Application.Run(new FourByFours());
     }
     public FourByFours()
     {
          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.DrawRectangles(pen, new Rectangle[] {new Rectangle(8, 2, 4, 4)});
     }
}

    


Clear a Graphics

image_pdfimage_print
   
 

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)));
     }
}

    


Graphics: MeasureString

image_pdfimage_print
   
 


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

image_pdfimage_print

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

image_pdfimage_print

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]