HatchBrush: HatchStyle Plaid

image_pdfimage_print


   


  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
      SetStyle(ControlStyles.ResizeRedraw, true);
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(211, 104);
      this.Text = "";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }

    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
            Graphics g = e.Graphics;
            HatchBrush hb = new HatchBrush(HatchStyle.Plaid, Color.AntiqueWhite ,Color.Black);
            g.FillEllipse(hb,30, 30, Width-50, 30);
        }
  }


           
          


Overlapping Hatch Brushes

image_pdfimage_print

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

class OverlappingHatchBrushes: Form
{
public static void Main()
{
Application.Run(new OverlappingHatchBrushes());
}
public OverlappingHatchBrushes()
{
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)
{
HatchBrush hbrush = new HatchBrush(HatchStyle.HorizontalBrick,
Color.White);
for (int i = 0; i < 10; i++) grfx.FillRectangle(hbrush, i * cx / 10, i * cy / 10, cx / 8, cy / 8); } } [/csharp]

Hatch Brush Rendering Origin

image_pdfimage_print

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

class HatchBrushRenderingOrigin: Form
{
public static void Main()
{
Application.Run(new HatchBrushRenderingOrigin());
}
public HatchBrushRenderingOrigin()
{
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)
{
HatchBrush hbrush = new HatchBrush(HatchStyle.HorizontalBrick,
Color.White);
for (int i = 0; i < 10; i++) { grfx.RenderingOrigin = new Point(i * cx / 10, i * cy / 10); grfx.FillRectangle(hbrush, i * cx / 10, i * cy / 10, cx / 8, cy / 8); } } } [/csharp]

Fill a Rectangle with LinearGradientBrush

image_pdfimage_print
   
 
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());
    }
}

    


Fill a Rectangle with TextureBrush

image_pdfimage_print
   
 
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

image_pdfimage_print
   
 


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

image_pdfimage_print
   
 

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); 
     }
}