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(); } }
2D Graphics
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); } }
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: Pen Alignment
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; 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()); } }