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());
}
}
2D Graphics
Draw a Rectangle
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
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
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]
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);
}
}
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)});
}
}