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; LinearGradientBrush lgb = new LinearGradientBrush( new Point(0, 0), new Point(50, 10), Color.White, Color.Black); g.FillRectangle(lgb, this.ClientRectangle); lgb.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
2D Graphics
Fill a Rectangle with TextureBrush
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("alphabet.gif"); TextureBrush tb = new TextureBrush(bmp); g.FillRectangle(tb, 45, 45, 70, 150); bmp.Dispose(); tb.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
TransformPoints
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class WhatSizeTransform : Form { public static void Main() { Application.Run(new WhatSizeTransform()); } public WhatSizeTransform() { Text = "With TransformPoints"; ResizeRedraw = true; } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Brush brush = new SolidBrush(clr); int y = 0; Point[] apt = { new Point(cx, cy) }; grfx.TransformPoints(CoordinateSpace.Device, CoordinateSpace.Page, apt); DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Pixel); DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Display); DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Document); DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Inch); DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Millimeter); DoIt(grfx, brush, ref y, apt[0], GraphicsUnit.Point); } void DoIt(Graphics grfx, Brush brush, ref int y,Point pt, GraphicsUnit gu) { GraphicsState gs = grfx.Save(); grfx.PageUnit = gu; grfx.PageScale = 1; PointF[] aptf = { pt }; grfx.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device, aptf); SizeF sizef = new SizeF(aptf[0]); grfx.Restore(gs); grfx.DrawString(gu + ": " + sizef, Font, brush, 0, y); y += (int)Math.Ceiling(Font.GetHeight(grfx)); } }
DrawPie with offset
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() { 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) { float fSlice = (float)(2 * Math.PI * (fAngle + fSweep / 2) / 360); rect.Offset((int)(rect.Width / 10 * Math.Cos(fSlice)), (int)(rect.Height / 10 * Math.Sin(fSlice))); grfx.DrawPie(pen, rect, fAngle, fSweep); } }
Graphics.SmoothingMode
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.SmoothingMode = SmoothingMode.None; grfx.DrawLine(pen, 2, 2, 18, 10); } }
DrawImage with size
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"); Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height); g.DrawImage(bmp, this.ClientRectangle); } public static void Main() { Application.Run(new Form1()); } }
DrawImage with destination 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 Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Bitmap bmp = new Bitmap("rama.jpg"); g.FillRectangle(Brushes.White, this.ClientRectangle); Point[] destinationPoints = { new Point(0, 0), // destination for upper-left point of original new Point(100, 0), // destination for upper-right point of original new Point(50, 100)};// destination for lower-left point of original g.DrawImage(bmp, destinationPoints); } public static void Main() { Application.Run(new Form1()); } }