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()); } }
Graphics: TranslateTransform
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class RotatedRectangles: Form
{
public static void Main()
{
Application.Run(new RotatedRectangles());
}
public RotatedRectangles()
{
Text = “Rotated Rectangles”;
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);
grfx.PageUnit = GraphicsUnit.Pixel;
PointF[] aptf = { (PointF) grfx.VisibleClipBounds.Size };
grfx.PageUnit = GraphicsUnit.Inch;
grfx.PageScale = 0.01f;
grfx.TransformPoints(CoordinateSpace.Page,
CoordinateSpace.Device, aptf);
grfx.TranslateTransform(aptf[0].X / 2, aptf[0].Y / 2);
for (int i = 0; i < 6; i++) { grfx.DrawRectangle(pen, 0, 0, 200, 200); grfx.RotateTransform(10); } } } [/csharp]
Graphics: Transform
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.Transform = new Matrix(1, 1, -1, 1, 0, 0); grfx.DrawString("abc", Font, new SolidBrush(clr), new Rectangle(0, 0, cx, cy)); } }
Graphics: ScaleTransform
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.ScaleTransform(1, 3); grfx.DrawString("abc", Font, new SolidBrush(clr), new Rectangle(0, 0, cx, cy)); } }
Use different Font object to draw a line of text
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 font = new Font("Times New Roman", 12, FontStyle.Regular); Font bfont = new Font("Times New Roman", 12, FontStyle.Bold); Font ifont = new Font("Times New Roman", 12, FontStyle.Italic); Font bifont = new Font("Times New Roman", 12,FontStyle.Bold | FontStyle.Italic); Font sfont = new Font("Times New Roman", 12, FontStyle.Strikeout); Font ufont = new Font("Times New Roman", 12, FontStyle.Underline); Font bsfont = new Font("Times New Roman", 12,FontStyle.Bold | FontStyle.Strikeout); int h = font.Height; g.DrawString("Regular", font, Brushes.Black, 0, 0); g.DrawString("Bold", bfont, Brushes.Black, 0, h); g.DrawString("Italic", ifont, Brushes.Black, 0, h * 2); g.DrawString("Bold-Italic", bifont, Brushes.Black, 0, h * 3); g.DrawString("Strikeout", sfont, Brushes.Black, 0, h * 4); g.DrawString("Underline", ufont, Brushes.Black, 0, h * 5); g.DrawString("Bold & Strikeout", bsfont, Brushes.Black, 0, h * 6); font.Dispose(); bfont.Dispose(); ifont.Dispose(); bifont.Dispose(); sfont.Dispose(); ufont.Dispose(); bsfont.Dispose(); } public static void Main() { Application.Run(new Form1()); } }