Graphics: DrawString

image_pdfimage_print
   
 
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

image_pdfimage_print
   
 
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

image_pdfimage_print
   
 

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

}

    


Draw an Image

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

    


Draw a Rectangle

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

        Pen p = new Pen(Color.Black);
        g.DrawRectangle(p, 3, 3, 8, 7);
        p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


Graphics: DrawPie

image_pdfimage_print
   
 

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

image_pdfimage_print

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); } } [/csharp]