Graphics Properties

image_pdfimage_print


   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GraphicsProps_c
{
    /// <summary>
    /// Summary description for GraphicsProps.
    /// </summary>
    public class GraphicsProps : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button B0;
    private System.Windows.Forms.Button B1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public GraphicsProps()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
      this.B0 = new System.Windows.Forms.Button();
      this.B1 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // B0
      // 
      this.B0.Location = new System.Drawing.Point(16, 240);
      this.B0.Name = "B0";
      this.B0.Size = new System.Drawing.Size(40, 24);
      this.B0.TabIndex = 0;
      this.B0.Text = "B0";
      this.B0.Click += new System.EventHandler(this.B0_Click);
      // 
      // B1
      // 
      this.B1.Location = new System.Drawing.Point(72, 240);
      this.B1.Name = "B1";
      this.B1.Size = new System.Drawing.Size(40, 24);
      this.B1.TabIndex = 1;
      this.B1.Text = "B1";
      this.B1.Click += new System.EventHandler(this.B1_Click);
      // 
      // GraphicsProps
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.B1,
                                                                  this.B0});
      this.Name = "GraphicsProps";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "GraphicsProps";
      this.Load += new System.EventHandler(this.GraphicsProps_Load);
      this.ResumeLayout(false);

    }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new GraphicsProps());
        }

    private void GraphicsProps_Load(object sender, System.EventArgs e)
    {
    }
    protected override void OnPaint ( PaintEventArgs e )
    {
      Graphics G = e.Graphics;

      G.Clear(Color.Bisque);
      //Change one of the Graphics attributes and save state
      G.SmoothingMode=SmoothingMode.AntiAlias;
      GraphicsState OldG = G.Save();

      //Restore the attribute and draw a line
      G.SmoothingMode=SmoothingMode.Default;
      G.DrawLine(new Pen(Color.DarkMagenta, 20), 10, 50, 
                                (int)(this.Width - 10), 140);

      //Restore the old Graphics state and draw another line
      G.Restore(OldG);
      G.DrawLine(new Pen(Color.DarkMagenta, 20), 10, 100, 
                                (int)(this.Width - 10), 190);

      G.Dispose();
    }

    private void B0_Click(object sender, System.EventArgs e)
    {
      BeginContainerNoArg(this.CreateGraphics());
    }

    private void B1_Click(object sender, System.EventArgs e)
    {
     // BeginContainerIntRectArg(this.CreateGraphics());
     // World2PageXform(this.CreateGraphics());
      RenderText(this.CreateGraphics());
    }

    public void BeginContainerNoArg(Graphics G)
    {
      G.Clear(Color.Bisque);

      //Change one of the attributes of the Graphics object
      //then save the state.
      G.SmoothingMode = SmoothingMode.AntiAlias;
      GraphicsContainer OldG  = G.BeginContainer();

      //Restore the Smoothing mode state and draw a line
      G.SmoothingMode = SmoothingMode.Default;
      G.DrawLine(new Pen(Color.Chocolate, 20), 10, 50, 
                                  (int)(this.Width - 10), 150);

      //Restore the old Graphics state and draw another line
      G.EndContainer(OldG);
      G.DrawLine(new Pen(Color.Chocolate, 20), 10, 100, 
                                  (int)(this.Width - 10), 200);

      G.Dispose();
    }

    public void BeginContainerIntRectArg(Graphics G)
    {
      G.Clear(Color.Bisque);

      // Define transformation for container.
      Rectangle srcRect = new Rectangle(0, 0, 200, 200);
      Rectangle destRect = new Rectangle(0, 0, 100, 100);
      // Begin graphics container.
      GraphicsContainer containerState  = G.BeginContainer(destRect, 
                                                srcRect, GraphicsUnit.Pixel);

      G.DrawLine(new Pen(Color.DarkOrchid, 20), 10, 100, 200, 100);
      G.EndContainer(containerState);

      G.DrawLine(new Pen(Color.DarkOrchid, 20), 10, 100, 200, 100);

      G.Dispose();
    }

    public void World2PageXform(Graphics G)
    {
      int EndX = 1;
      int EndY = 1;
      G.Clear(Color.Azure);
      G.PageUnit=GraphicsUnit.Inch;
      G.TranslateTransform(1, 1);
      G.DrawLine(Pens.Blue, 0, 0, EndX, EndY);

      int Xpix = EndX * (int)G.DpiX;
      int Ypix = EndY * (int)G.DpiY;

    }

    public void RenderText(Graphics G)
    {
      Font F = new Font("Arial", 16);
      SolidBrush B = new SolidBrush(Color.Black);

      G.Clear(Color.Azure);
      G.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
      G.DrawString("SingleBitPerPixel", F, B, new PointF(10, 10));

      G.TextRenderingHint = TextRenderingHint.AntiAlias;
      G.DrawString("AntiAlias default Contrast", F, B, new PointF(10, 60));

      G.TextContrast = 12;
      G.DrawString("AntiAlias Low Contrast", F, B, new PointF(10, 90));

      G.TextContrast = 1;
      G.DrawString("AntiAlias High Contrast", F, B, new PointF(10, 120));

      
    }
    }
}



           
          


Line Joins: Miter, Bevel, Round, MiterClipped

image_pdfimage_print

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

class LineJoins: Form
{
public static void Main()
{
Application.Run(new LineJoins());
}
public LineJoins()
{
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 penNarrow = new Pen(clr);
Pen penWide = new Pen(Color.Gray, cx / 16);
Point[] apt = { new Point(1 * cx / 32, 1 * cy / 8),
new Point(4 * cx / 32, 6 * cy / 8),
new Point(7 * cx / 32, 1 * cy / 8) };

for (int i = 0; i < 4; i++) { penWide.LineJoin = (LineJoin) i; grfx.DrawLines(penWide, apt); grfx.DrawLines(penNarrow, apt); grfx.TranslateTransform(cx / 4, 0); } } } [/csharp]

Line Join style: Bevel

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();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pen Cap App";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      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;
      g.SmoothingMode = SmoothingMode.AntiAlias;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      Pen p = new Pen(Color.Black, 10);
      p.LineJoin = LineJoin.Bevel;
      e.Graphics.DrawRectangle(p, 20, 20, 200, 200);
      p.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


All Line Join

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;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;

public class Form1 : Form
{

      public Form1() {
            InitializeComponent();
            
      }
    private void SimpleStyleRenderer_Paint(object sender, PaintEventArgs e)
    {
      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

      Pen myPen = new Pen(Color.Blue, 10);
      int y = 20;

      foreach (LineJoin join in Enum.GetValues(typeof(LineJoin)))
      {
        myPen.LineJoin = join;
        e.Graphics.DrawRectangle(myPen, 20, y, 70, 40);
        e.Graphics.DrawString(join.ToString(), new Font("Tahoma", 8), Brushes.Black, 100, y + 10);
        y += 70;
      }
    }


    private void InitializeComponent()
    {
      this.SuspendLayout();
      // 
      // SimpleStyleRenderer
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(384, 353);
      this.Name = "SimpleStyleRenderer";
      this.Text = "SimpleStyleRenderer";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.SimpleStyleRenderer_Paint);
      this.ResumeLayout(false);

    }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }
}
           
          


Line Cap style

image_pdfimage_print

namespace PenCapApp
{
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();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = “Pen Cap App”;
this.Resize += new System.EventHandler(this.Form1_Resize);
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;
Pen thePen = new Pen(Color.Black, 10);
int yOffSet = 10;

Array obj = Enum.GetValues(typeof(LineCap));

for(int x = 0; x < obj.Length; x++) { LineCap temp = (LineCap)obj.GetValue(x); thePen.StartCap = temp; thePen.EndCap = temp; g.DrawString(temp.ToString(), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 0, yOffSet); g.DrawLine(thePen, 100, yOffSet, Width - 50, yOffSet); yOffSet += 40; } } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } } [/csharp]

Line cap: Arrow Anchor, Round

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();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      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;
      g.SmoothingMode = SmoothingMode.AntiAlias;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      Pen p = new Pen(Color.Black, 10);
      p.StartCap = LineCap.Round;
      p.EndCap = LineCap.ArrowAnchor;
      g.DrawLine(p, 30, 30, 80, 30);
      p.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


Pen Start Cap: LineCap.SquareAnchor

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;
            Pen p = new Pen(Color.Brown, 15);
 
            p.StartCap = LineCap.SquareAnchor;
            p.EndCap = LineCap.SquareAnchor ;
            g.DrawLine(p,30, 30, Width-50, 30);
    }
  }