new Pen(clr, f)

image_pdfimage_print

using System;
using System.Drawing;
using System.Windows.Forms;

class PenWidths: Form
{
public static void Main()
{
Application.Run(new PenWidths());
}
public PenWidths()
{
Text = “Pen Widths”;
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)
{
Brush brush = new SolidBrush(clr);
float y = 0;

grfx.PageUnit = GraphicsUnit.Point;
grfx.PageScale = 1;

for (float f = 0; f < 3.2; f += 0.2f) { Pen pen = new Pen(clr, f); string str = String.Format("{0:F1} point wide pen: ", f); SizeF sizef = grfx.MeasureString(str, Font); grfx.DrawString(str, Font, brush, 0, y); grfx.DrawLine(pen, sizef.Width, y + sizef.Height / 2, sizef.Width + 144, y + sizef.Height / 2); y += sizef.Height; } } } [/csharp]

new Pen(lgbrush, Math.Min(cx, cy) / 25), Gradient Pen

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class GradientPen: Form 
{
     public static void Main()
     {
          Application.Run(new GradientPen());
     }
     public GradientPen()
     {
          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)
     {
          Brush lgbrush = new LinearGradientBrush(
                                   new Rectangle(0, 0, cx, cy), 
                                   Color.White, Color.Black,
                                   LinearGradientMode.BackwardDiagonal);
   
          Pen pen = new Pen(lgbrush, Math.Min(cx, cy) / 25);
   
          pen.Alignment = PenAlignment.Inset;
   
          grfx.DrawRectangle(pen, 0, 0, cx, cy);
          grfx.DrawLine(pen, 0, 0, cx, cy);
          grfx.DrawLine(pen, 0, cy, cx, 0);
     }
}

    


Two-Triangle Tile

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class TwoTriangleTile : Form {
    const int iSide = 50;         // Side of square for triangle

    public static void Main() {
        Application.Run(new TwoTriangleTile());
    }
    public TwoTriangleTile() {
        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) {
        Point[] apt = { new Point(0, 0), new Point(iSide, 0), new Point(0, iSide) };

        PathGradientBrush pgbrush1 =
                       new PathGradientBrush(apt, WrapMode.TileFlipXY);

        apt = new Point[] {new Point(iSide, 0), new Point(iSide, iSide), 
                             new Point(0, iSide)};

        PathGradientBrush pgbrush2 =
                       new PathGradientBrush(apt, WrapMode.TileFlipXY);

        grfx.FillRectangle(pgbrush1, 0, 0, cx, cy);
        grfx.FillRectangle(pgbrush2, 0, 0, cx, cy);
    }
}

    


Square Tile

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class SquareTile: Form
{
     const int iSide = 50;         // Side of square
   
     public static void Main()
     {
          Application.Run(new SquareTile());
     }
     public SquareTile()
     {
          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)
     {
          Point[] apt = {new Point(0,     0),     new Point(iSide, 0), 
                         new Point(iSide, iSide), new Point(0,     iSide)};
   
          PathGradientBrush pgbrush =
                         new PathGradientBrush(apt, WrapMode.TileFlipXY);
   
          pgbrush.SurroundColors = new Color[] { Color.Red,  Color.Lime,
                                                 Color.Blue, Color.White};
   
          grfx.FillRectangle(pgbrush, 0, 0, cx, cy);
     }
}

    


Triangle Tile

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class TriangleTile: Form
{
     const int iSide = 50;         // Side of square for triangle
     MenuItem  miChecked;
   
     public static void Main()
     {
          Application.Run(new TriangleTile());
     }
     public TriangleTile()
     {
          ResizeRedraw = true; 
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&amp;Wrap-Mode");
   
          foreach (WrapMode wm in Enum.GetValues(typeof(WrapMode)))
          {
               MenuItem mi = new MenuItem();
               mi.Text     = wm.ToString(); 
               mi.Click   += new EventHandler(MenuWrapModeOnClick);
               Menu.MenuItems[0].MenuItems.Add(mi);
          }
          miChecked = Menu.MenuItems[0].MenuItems[0];
          miChecked.Checked = true;
     }
     void MenuWrapModeOnClick(object obj, EventArgs ea)
     {
          miChecked.Checked = false;
          miChecked = (MenuItem) obj;
          miChecked.Checked = true;
          Invalidate();
     }
     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)
     {
          Point[] apt = { new Point(0,     0), 
                          new Point(iSide, 0), 
                          new Point(0,     iSide)};
   
          PathGradientBrush pgbrush = 
                    new PathGradientBrush(apt, (WrapMode) miChecked.Index);
   
          grfx.FillRectangle(pgbrush, 0, 0, cx, cy);
     }
}

    


Gradient Brush Ball

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class BouncingGradientBrushBall : Form {
    public static void Main() {
        Application.Run(new BouncingGradientBrushBall());
    }
    public BouncingGradientBrushBall() {
        ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs pea) {
        GraphicsPath path = new GraphicsPath();
        Rectangle rect = new Rectangle(10, 10, 30, 30);
        path.AddEllipse(rect);

        PathGradientBrush pgbrush = new PathGradientBrush(path);
        pgbrush.CenterPoint = new PointF((rect.Left + rect.Right) / 3,
                                         (rect.Top + rect.Bottom) / 3);
        pgbrush.CenterColor = Color.White;
        pgbrush.SurroundColors = new Color[] { Color.Red };

        pea.Graphics.FillRectangle(pgbrush, rect);
    }
}

    


Star Gradient Brush

image_pdfimage_print

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class StarGradientBrush: Form
{
public static void Main()
{
Application.Run(new StarGradientBrush());
}
public StarGradientBrush()
{
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)
{
Point[] apt = new Point[5];

for (int i = 0; i < apt.Length; i++) { double dAngle = (i * 0.8 - 0.5) * Math.PI; apt[i] = new Point( (int)(cx * (0.50 + 0.48 * Math.Cos(dAngle))), (int)(cy * (0.50 + 0.48 * Math.Sin(dAngle)))); } PathGradientBrush pgbrush = new PathGradientBrush(apt); pgbrush.CenterColor = Color.White; pgbrush.SurroundColors = new Color[1] { Color.Black }; grfx.FillRectangle(pgbrush, 0, 0, cx, cy); } } [/csharp]