Graphics: TranslateTransform

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class RotatedRectangles: Form
{
public static void Main()
{
Application.Run(new RotatedRectangles());
}
public RotatedRectangles()
{
Text = “Rotated Rectangles”;
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);
grfx.PageUnit = GraphicsUnit.Pixel;
PointF[] aptf = { (PointF) grfx.VisibleClipBounds.Size };
grfx.PageUnit = GraphicsUnit.Inch;
grfx.PageScale = 0.01f;

grfx.TransformPoints(CoordinateSpace.Page,
CoordinateSpace.Device, aptf);

grfx.TranslateTransform(aptf[0].X / 2, aptf[0].Y / 2);

for (int i = 0; i < 6; i++) { grfx.DrawRectangle(pen, 0, 0, 200, 200); grfx.RotateTransform(10); } } } [/csharp]

Graphics: Transform

   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class MobyDick: Form
{
     public static void Main()
     {
          Application.Run(new MobyDick());
     }
     public MobyDick()
     {
          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.Transform = new Matrix(1, 1, -1, 1, 0, 0);
          grfx.DrawString("abc", Font, new SolidBrush(clr), 
                          new Rectangle(0, 0, cx, cy));
     }
}

    


Graphics: ScaleTransform

   
 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class MobyDick: Form
{
     public static void Main()
     {
          Application.Run(new MobyDick());
     }
     public MobyDick()
     {
          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.ScaleTransform(1, 3);
          grfx.DrawString("abc", Font, new SolidBrush(clr), 
                          new Rectangle(0, 0, cx, cy));
     }
}

    


Use different Font object to draw a line of text

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);

    Font font = new Font("Times New Roman", 12, FontStyle.Regular);
    Font bfont = new Font("Times New Roman", 12, FontStyle.Bold);
    Font ifont = new Font("Times New Roman", 12, FontStyle.Italic);
    Font bifont = new Font("Times New Roman", 12,FontStyle.Bold | FontStyle.Italic);
    Font sfont = new Font("Times New Roman", 12, FontStyle.Strikeout);
    Font ufont = new Font("Times New Roman", 12, FontStyle.Underline);
    Font bsfont = new Font("Times New Roman", 12,FontStyle.Bold | FontStyle.Strikeout);
    int h = font.Height;

    g.DrawString("Regular", font, Brushes.Black, 0, 0);
    g.DrawString("Bold", bfont, Brushes.Black, 0, h);
    g.DrawString("Italic", ifont, Brushes.Black, 0, h * 2);
    g.DrawString("Bold-Italic", bifont, Brushes.Black, 0, h * 3);
    g.DrawString("Strikeout", sfont, Brushes.Black, 0, h * 4);
    g.DrawString("Underline", ufont, Brushes.Black, 0, h * 5);
    g.DrawString("Bold &amp; Strikeout", bsfont, Brushes.Black, 0, h * 6);

    font.Dispose();
    bfont.Dispose();
    ifont.Dispose();
    bifont.Dispose();
    sfont.Dispose();
    ufont.Dispose();
    bsfont.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


Graphics: DrawString

   
 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class MobyDick: Form
{
     public static void Main()
     {
          Application.Run(new MobyDick());
     }
     public MobyDick()
     {
          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.DrawString("abc", Font, new SolidBrush(clr), 
                          new Rectangle(0, 0, cx, cy));
     }
}

    


Graphics: Draw an Icon

   
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Net;
using System.IO;

public class Form1 : System.Windows.Forms.Form {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
        string p = "icon.ico";
        Icon ic = new Icon(p);
        this.Icon = ic;  // Icon 1)
        Graphics g = e.Graphics;
        g.DrawIcon(ic, 0, 0);  // Icon 2)
        Image i = ic.ToBitmap();
        g.DrawImage(i, 50, 0);  // Icon 3)


        g.Dispose();
    }
}

    


FillEllipse: Red Traffic Light

   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form {
    private int lightColor = 1;

    public Form1() {
        InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;

        g.FillRectangle(Brushes.White, this.ClientRectangle);
        Rectangle r = new Rectangle(10, 10, 60, 180);
        g.FillRectangle(Brushes.LightGray, r);
        Rectangle r1 = new Rectangle(10, 10, 60, 60);
        Rectangle r2 = new Rectangle(10, 70, 60, 60);
        Rectangle r3 = new Rectangle(10, 130, 60, 60);
        switch (lightColor) {
            case 1:
                g.FillEllipse(Brushes.Red, r1);
                g.FillEllipse(Brushes.Black, r2);
                g.FillEllipse(Brushes.Black, r3);
                break;
            case 2:
                g.FillEllipse(Brushes.Black, r1);
                g.FillEllipse(Brushes.Yellow, r2);
                g.FillEllipse(Brushes.Black, r3);
                break;
            case 3:
                g.FillEllipse(Brushes.Black, r1);
                g.FillEllipse(Brushes.Black, r2);
                g.FillEllipse(Brushes.Green, r3);
                break;
        }
    }

    private void Form1_Click(object sender, EventArgs e) {
        lightColor++;
        if (lightColor == 4)
            lightColor = 1;
        this.Invalidate();
    }
    private void InitializeComponent() {
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 271);
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
        this.Click += new System.EventHandler(this.Form1_Click);
    }

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

}