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()); } }
Author: coder
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); } }
Picture Cube: DrawImage
using System; using System.Drawing; using System.Collections; using System.ComponentModel; 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(); } }
Paint along points in a list points
using System; using System.Collections.Generic; using System.ComponentModel; 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); } }
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); } }
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} DpiY = {1}", grfx.DpiX, grfx.DpiY), Font, new SolidBrush(clr), 0, 0); } }
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); } }