Csharp/C Sharp/2D Graphics/Graphics

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Содержание

Apply the scaling transformation(scale subsequent operations by 2x horizontally and 3x vertically)

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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);
    g.DrawRectangle(Pens.Black, 10, 10, 50, 50);
    g.DrawEllipse(Pens.Black, 10, 10, 10, 10);
    g.ScaleTransform(2.0f, 3.0f);
    g.DrawRectangle(Pens.Black, 10, 10, 50, 50);
    g.DrawEllipse(Pens.Black, 10, 10, 10, 10);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


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


Clear a Graphics

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


Clipping: ResetClip

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form {
    GraphicsPath gP;
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
        Graphics g = e.Graphics;
        DrawPathTransform(g);
        g.Dispose();
    }
    void AddStringToPath(string s) {
        int fSt = (int)FontStyle.Regular;
        Point xy = new Point(50, 10);
        FontFamily fF = new FontFamily("Times new roman");
        StringFormat sFr = StringFormat.GenericDefault;
        gP.AddString(s, fF, fSt, 100, xy, sFr);
    }
    void DrawPathTransform(Graphics g) {
        gP = new GraphicsPath();
        AddStringToPath("C#");
        g.FillPath(Brushes.Gray, gP);
        Matrix m = new Matrix();
        m.Translate(5, 5);
        gP.Transform(m);
        g.FillPath(Brushes.Yellow, gP);
        g.DrawPath(Pens.Red, gP);
    }
    void DrawClip(Graphics g) {
        gP = new GraphicsPath();
        AddStringToPath("C#");
        g.SetClip(gP);
        g.TranslateClip(-5, -5);
        RectangleF rc = gP.GetBounds();
        rc.Offset(-5, -5);
        g.FillRectangle(Brushes.Gray, rc);
        g.ResetClip();
        g.FillPath(Brushes.Yellow, gP);
        g.DrawPath(Pens.Red, gP);
    }
}


DpiY and DpiX

 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class DotsPerInch: Form
{
     public static void Main()
     {
          Application.Run(new DotsPerInch());
     }
     public DotsPerInch()
     {
          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(String.Format("DpiX = {0}\nDpiY = {1}",
                                        grfx.DpiX, grfx.DpiY),
                          Font, new SolidBrush(clr), 0, 0);
     }
}


Draw an Image

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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;
    Bitmap bmp = new Bitmap("rama.jpg");
    g.DrawImage(bmp, 0, 0);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Draw a Rectangle

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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);
        Pen p = new Pen(Color.Black);
        g.DrawRectangle(p, 3, 3, 8, 7);
        p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Draw Ellipse

 

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


DrawImage with destination points

 

using System;
using System.Collections.Generic;
using System.ruponentModel;
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;
    Bitmap bmp = new Bitmap("rama.jpg");
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    Point[] destinationPoints = {
      new Point(0, 0),    // destination for upper-left point of original
      new Point(100, 0),  // destination for upper-right point of original
      new Point(50, 100)};// destination for lower-left point of original
    g.DrawImage(bmp, destinationPoints);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


DrawImage with size

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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;
    Bitmap bmp = new Bitmap("rama.jpg");
    Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
    g.DrawImage(bmp, this.ClientRectangle);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


DrawPie with offset

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class PieChart: Form
{
     int[] aiValues = { 50, 100, 25, 150, 100, 75 };
   
     public static void Main()
     {
          Application.Run(new PieChart());
     }
     public PieChart()
     {
          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)
     {
          Rectangle rect   = new Rectangle(50, 50, 200, 200);
          Pen       pen    = new Pen(clr);
          int       iTotal = 0;
          float     fAngle = 0, fSweep;
   
          foreach(int iValue in aiValues)
               iTotal += iValue;
   
          foreach(int iValue in aiValues)
          {
               fSweep = 360f * iValue / iTotal;
               DrawPieSlice(grfx, pen, rect, fAngle, fSweep);
               fAngle += fSweep;
          }
     }
     protected virtual void DrawPieSlice(Graphics grfx, Pen pen, 
                                         Rectangle rect,
                                         float fAngle, float fSweep)
     {
          float fSlice = (float)(2 * Math.PI * (fAngle + fSweep / 2) / 360);
   
          rect.Offset((int)(rect.Width  / 10 * Math.Cos(fSlice)),
                      (int)(rect.Height / 10 * Math.Sin(fSlice)));
                      
          grfx.DrawPie(pen, rect, fAngle, fSweep); 
     }
}


Fill a Rectangle with LinearGradientBrush

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        LinearGradientBrush lgb = new LinearGradientBrush(
                                             new Point(0, 0),
                                             new Point(50, 10),
                                             Color.White,
                                             Color.Black);
        g.FillRectangle(lgb, this.ClientRectangle);
        lgb.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Fill a Rectangle with TextureBrush

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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;
    Bitmap bmp = new Bitmap("alphabet.gif");
    TextureBrush tb = new TextureBrush(bmp);
    g.FillRectangle(tb, 45, 45, 70, 150);
    bmp.Dispose();
    tb.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


FillEllipse: Red Traffic Light

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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());
    }
}


FillPolygon: Alternate and Winding Fill Modes

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class FillModesClassical : Form {
    public static void Main() {
        Application.Run(new FillModesClassical());
    }
    public FillModesClassical() {
        ResizeRedraw = true;
        ClientSize = new Size(2 * ClientSize.Height, ClientSize.Height);
    }
    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.FillPolygon(brush, apt, FillMode.Alternate);
    }
}


Fill the area "bounded" by the path

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
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);
    GraphicsPath gp = new GraphicsPath();
    gp.AddLine(10, 10, 10, 50);
    gp.AddLine(10, 50, 50, 50);
    gp.AddLine(50, 50, 50, 10);
    gp.StartFigure();
    gp.AddLine(60, 10, 60, 50);
    gp.AddLine(60, 50, 100, 50);
    gp.AddLine(100, 50, 100, 10);
    gp.CloseFigure();
    Rectangle r = new Rectangle(110, 10, 40, 40);
    gp.AddEllipse(r);
    g.FillPath(Brushes.Orange, gp);
    g.DrawPath(Pens.Black, gp);
    gp.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Graphics: Draw an Icon

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
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();
    }
}


Graphics: DrawArc

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class DashedEllipse: Form
{
     public static void Main()
     {
          Application.Run(new DashedEllipse());
     }
     public DashedEllipse()
     {
          Text = "Dashed Ellipse Using DrawArc";
          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);
          Rectangle rect = new Rectangle(0, 0, cx - 1, cy - 1);
   
          for (int iAngle = 0; iAngle < 360; iAngle += 15)
               grfx.DrawArc(pen, rect, iAngle, 10);
     }
}


Graphics: DrawPie

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class PieChart: Form
{
     int[] aiValues = { 50, 100, 25, 150, 100, 75 };
   
     public static void Main()
     {
          Application.Run(new PieChart());
     }
     public PieChart()
     {
          Text = "Pie Chart";
          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)
     {
          Rectangle rect   = new Rectangle(50, 50, 200, 200);
          Pen       pen    = new Pen(clr);
          int       iTotal = 0;
          float     fAngle = 0, fSweep;
   
          foreach(int iValue in aiValues)
               iTotal += iValue;
   
          foreach(int iValue in aiValues)
          {
               fSweep = 360f * iValue / iTotal;
               DrawPieSlice(grfx, pen, rect, fAngle, fSweep);
               fAngle += fSweep;
          }
     }
     protected virtual void DrawPieSlice(Graphics grfx, Pen pen, 
                                         Rectangle rect,
                                         float fAngle, float fSweep)
     {
          grfx.DrawPie(pen, rect, fAngle, fSweep); 
     }
}


Graphics: DrawRectangle

 

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

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


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


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


Graphics: FillRectangle

 
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.FillRectangle(brush, new Rectangle(2, 8, 4, 4));
     }
}


Graphics: FillRectangles

 
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.FillRectangles(brush, new Rectangle[] 
                                             {new Rectangle(8, 8, 4, 4)});
     }
}


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


Graphics: PageScale

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class ArbitraryCoordinates: Form
{
     public static void Main()
     {
          Application.Run(new ArbitraryCoordinates());
     }
     public ArbitraryCoordinates()
     {
          Text = "Arbitrary Coordinates";
          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.PageUnit = GraphicsUnit.Pixel;
          SizeF sizef = grfx.VisibleClipBounds.Size;
          grfx.PageScale = Math.Min(sizef.Width  / grfx.DpiX / 1000, 
                                    sizef.Height / grfx.DpiY / 1000);
   
          grfx.DrawEllipse(new Pen(clr), 0, 0, 990, 990);
     }
}


Graphics: PageUnit

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class ArbitraryCoordinates: Form
{
     public static void Main()
     {
          Application.Run(new ArbitraryCoordinates());
     }
     public ArbitraryCoordinates()
     {
          Text = "Arbitrary Coordinates";
          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.PageUnit = GraphicsUnit.Pixel;
          SizeF sizef = grfx.VisibleClipBounds.Size;
   
          grfx.DrawEllipse(new Pen(clr), 0, 0, 990, 990);
     }
}


Graphics: Pen Alignment

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Pen p = new Pen(Color.Black, 3);
        p.Alignment = PenAlignment.Center;
        g.DrawRectangle(p, 3, 3, 8, 7);
        p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


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


Graphics: SetClip

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class KeyholeClip: Form
{
     protected Image        image;
     protected GraphicsPath path;
   
     public static void Main()
     {
          Application.Run(new KeyholeClip());
     }
     public KeyholeClip()
     {
          ResizeRedraw = true;
          image = Image.FromFile("Color.jpg");
   
          path = new GraphicsPath();
          path.AddArc(80, 0, 80, 80, 45, -270);
          path.AddLine(70, 180, 170, 180);
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          pea.Graphics.SetClip(path);
          pea.Graphics.DrawImage(image, 0, 0, image.Width, image.Height);
     }      
}


Graphics.SmoothingMode

 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class AntiAlias: Form
{
     public static void Main()
     {
          Application.Run(new AntiAlias());
     }
     public AntiAlias()
     {
          Text = "Anti-Alias Demo";
          BackColor = SystemColors.Window;
          ForeColor = SystemColors.WindowText;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
          Pen      pen  = new Pen(ForeColor);
   
          grfx.SmoothingMode   = SmoothingMode.None;
   
          grfx.DrawLine(pen, 2, 2, 18, 10);
     }
}


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: TranslateClip

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class KeyholeClipCentered : Form {
    GraphicsPath path = new GraphicsPath();
    Image image = Image.FromFile("Color.jpg");
    public static void Main() {
        Application.Run(new KeyholeClipCentered());
    }
    public KeyholeClipCentered() {
        ResizeRedraw = true;
        path.AddArc(80, 0, 80, 80, 45, -270);
        path.AddLine(70, 180, 170, 180);
    }
    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.SetClip(path);
        RectangleF rectf = path.GetBounds();
        int xOffset = (int)((cx - rectf.Width) / 2 - rectf.X);
        int yOffset = (int)((cy - rectf.Height) / 2 - rectf.Y);
        grfx.TranslateClip(xOffset, yOffset);
        grfx.DrawImage(image, xOffset, yOffset, image.Width, image.Height);
    }
}


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


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


One-Inch Ellipse

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class TryOneInchEllipse: Form
{
     public static void Main()
     {
          Application.Run(new TryOneInchEllipse());
     }
     public TryOneInchEllipse()
     {
          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, grfx.DpiX, grfx.DpiY);
     }
}


Paint along points in a list points

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class MainForm : Form {
    private List<Point> myPts = new List<Point>();
    public MainForm() {
        this.Text = "Basic Paint Form";
        this.Paint += new PaintEventHandler(MainForm_Paint);
        this.MouseDown += new MouseEventHandler(MainForm_MouseDown);
    }
    void MainForm_MouseDown(object sender, MouseEventArgs e) {
        myPts.Add(new Point(e.X, e.Y));
        Invalidate();
    }
    public void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.DrawString("Hello GDI+", new Font("Times New Roman", 20),Brushes.Green, 0, 0);
        foreach (Point p in myPts)
            g.FillEllipse(Brushes.Firebrick, p.X, p.Y, 10, 10);
    }
}


Picture Cube: DrawImage

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs pea) {
        CubeDBuf();
    }
    private void CubeDBuf() {
        Graphics g;
        string path;
        int x = 100, y = 40;
        Point A = new Point(10, 50);
        Point B = new Point(180, 50);
        Point C = new Point(10, 170);
        Point a = new Point(A.X + x, A.Y - y);
        Point b = new Point(B.X + x, B.Y - y);
        Point Z = new Point(B.X, C.Y);
        Point[] p3Fro = { A, B, C };
        Point[] p3Top = { a, b, A };
        Point[] p3Rig = { B, b, Z };
        Bitmap bm = new Bitmap(B.X + x, C.Y + y);
        g = Graphics.FromImage(bm);
        path = "A.bmp";
        Image im1 = Image.FromFile(path);
        g.DrawImage(im1, p3Fro);
        path = "B.BMP";
        Image im3 = Image.FromFile(path);
        g.DrawImage(im3, p3Top);
        path = "C.bmp";
        Image im2 = Image.FromFile(path);
        g.DrawImage(im2, p3Rig);
        g = Graphics.FromHwnd(this.Handle);
        g.DrawImage(bm, 1, 1);
        g.Dispose();
    }
}


PixelOffsetMode

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class AntiAlias: Form
{
     public static void Main()
     {
          Application.Run(new AntiAlias());
     }
     public AntiAlias()
     {
          Text = "Anti-Alias Demo";
          BackColor = SystemColors.Window;
          ForeColor = SystemColors.WindowText;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
          Pen      pen  = new Pen(ForeColor);
   
          grfx.PixelOffsetMode = PixelOffsetMode.Default;
   
          grfx.DrawLine(pen, 2, 2, 18, 10);
     }
}


Reset the transformation Translate by 30 pixels in vertical direction

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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, ClientRectangle);
    g.DrawEllipse(Pens.Black, 20, 20, 30, 50);
    g.TranslateTransform(-15, 0);
    g.DrawEllipse(Pens.Black, 20, 20, 30, 50);
    g.ResetTransform();
    g.TranslateTransform(0, 30);
    g.DrawEllipse(Pens.Black, 20, 20, 30, 50);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Rotate the text 45 degrees clockwise then Translate the text 150 pixels horizontally

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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 f = new Font("Times New Roman", 24);
    g.DrawString("Rotate then Translate", f, Brushes.Black, 0, 0);
    g.RotateTransform(45);
    g.TranslateTransform(150, 0);
    g.DrawString("Rotate  then Translate ", f, Brushes.Black, 0, 0);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Rotate the text through 45 degrees

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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 f = new Font("Times New Roman", 24);
    g.DrawString("Rotation", f, Brushes.Black, 0, 0);
    g.RotateTransform(45);
    g.DrawString("Rotation", f, Brushes.Black, 0, 0);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Scribble with Mouse

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class frmScribble : System.Windows.Forms.Form {
    private System.ruponentModel.Container components = null;
    private bool mouseDown = false;
    private Point lastPoint = Point.Empty;
    private string color = "black";
    private Graphics g;
    private Pen p;
    public frmScribble() {
        g = CreateGraphics();
        p = new Pen(Color.FromName(color));
    }
    protected override void OnMouseDown(MouseEventArgs e) {
        mouseDown = true;
        if (e.Button == MouseButtons.Right) {
            ContextMenu m = new ContextMenu();
            m.MenuItems.Add(0, new MenuItem("black", new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(1, new MenuItem("white", new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(2, new MenuItem("red", new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(3, new MenuItem("green", new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(4, new MenuItem("blue", new EventHandler(RightMouseButton_Click)));
            m.Show(this, new Point(e.X, e.Y));
        }
    }
    protected void RightMouseButton_Click(object sender, EventArgs e) {
        color = ((MenuItem)sender).Text;
        p = new Pen(Color.FromName(color));
    }
    protected override void OnMouseUp(MouseEventArgs e) {
        mouseDown = false;
    }
    protected override void OnMouseMove(MouseEventArgs e) {
        if (lastPoint.Equals(Point.Empty)) lastPoint = new Point(e.X, e.Y);
        if (mouseDown) {
            Point pMousePos = new Point(e.X, e.Y);
            g.DrawLine(p, pMousePos, lastPoint);
        }
        lastPoint = new Point(e.X, e.Y);
    }
    [STAThread]
    static void Main() {
        Application.Run(new frmScribble());
    }
}


Transform and RotateTransform in a loop

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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 f = new Font("Times New Roman", 16);
    for (float angle = 0; angle < 360; angle += 45)
    {
      g.ResetTransform();
      g.TranslateTransform(ClientRectangle.Width / 2,
                 ClientRectangle.Height / 2);
      g.RotateTransform(angle);
      g.DrawString("Hello, World", f, Brushes.Black, 50, 0);
    }
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


TransformPoints

 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class WhatSizeTransform : Form {
    public static void Main() {
        Application.Run(new WhatSizeTransform());
    }
    public WhatSizeTransform() {
        Text = "With TransformPoints";
        ResizeRedraw = true;
    }
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
        Brush brush = new SolidBrush(clr);
        int y = 0;
        Point[] apt = { new Point(cx, cy) };
        grfx.TransformPoints(CoordinateSpace.Device, CoordinateSpace.Page, apt);
        DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Pixel);
        DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Display);
        DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Document);
        DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Inch);
        DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Millimeter);
        DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Point);
    }
    void DoIt(Graphics grfx, Brush brush, ref int y,Point pt, GraphicsUnit gu) {
        GraphicsState gs = grfx.Save();
        grfx.PageUnit = gu;
        grfx.PageScale = 1;
        PointF[] aptf = { pt };
        grfx.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device, aptf);
        SizeF sizef = new SizeF(aptf[0]);
        grfx.Restore(gs);
        grfx.DrawString(gu + ": " + sizef, Font, brush, 0, y);
        y += (int)Math.Ceiling(Font.GetHeight(grfx));
    }
}


Use different Font object to draw a line of text

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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 & 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());
    }
}


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