using System;
using System.Collections.Generic;
using System.ComponentModel;
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());
}
}
2D Graphics
Rotate the text through 45 degrees
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 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());
}
}
Reset the transformation Translate by 30 pixels in vertical direction
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, 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());
}
}
Apply the scaling transformation(scale subsequent operations by 2x horizontally and 3x vertically)
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);
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());
}
}
Transform and RotateTransform in a loop
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 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());
}
}
[/csharp]
Rotate the text 45 degrees clockwise then Translate the text 150 pixels horizontally
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 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());
}
}
Clipping: ResetClip
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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);
}
}