Enumerate LinearGradientMode

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.Drawing.Drawing2D;

public class MainForm : Form {
public MainForm() {
CenterToScreen();
}

protected void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
Rectangle r = new Rectangle(10, 10, 100, 100);
LinearGradientBrush theBrush = null;
int yOffSet = 10;
Array obj = Enum.GetValues(typeof(LinearGradientMode));
for (int x = 0; x < obj.Length; x++) { LinearGradientMode temp = (LinearGradientMode)obj.GetValue(x); theBrush = new LinearGradientBrush(r, Color.GreenYellow, Color.Blue, temp); g.DrawString(temp.ToString(), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 0, yOffSet); g.FillRectangle(theBrush, 150, yOffSet, 200, 50); yOffSet += 80; } } } [/csharp]

LinearGradientMode.Horizontal, Vertical, BackwardDiagonal, ForwardDiagonal

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

using System.Drawing.Drawing2D;  

public class Form1 : System.Windows.Forms.Form {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Font f = new Font(new FontFamily("Times New Roman"), 10);
        Brush fb = new SolidBrush(Color.Black);
        LinearGradientBrush lGB;  
        Color cR = Color.Red, cW = Color.White;
        int w = 100, h = 70;

        g.DrawString("Horizontal", f, fb, 10, 5);
        Rectangle rec = new Rectangle(10, 20, w, h);
        LinearGradientMode lGM = LinearGradientMode.Horizontal;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);

        g.DrawString("Vertical", f, fb, w + 20, 5);
        rec.Offset(w + 10, 0);
        lGM = LinearGradientMode.Vertical;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);

        g.DrawString("ForwardDiagonal", f, fb, 10, h + 25);
        rec.Offset(-w - 10, h + 20);
        lGM = LinearGradientMode.ForwardDiagonal;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);

        g.DrawString("BackwardDiagonal", f, fb, w + 20, h + 25);
        rec.Offset(w + 10, 0);
        lGM = LinearGradientMode.BackwardDiagonal;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);

        fb.Dispose();
        g.Dispose();
    }
}

    


Rectangle Linear-Gradient Brush

image_pdfimage_print
   
 


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class RectangleLinearGradientBrush: Form
{
     MenuItem miChecked;
   
     public static void Main()
     {
          Application.Run(new RectangleLinearGradientBrush());
     } 
     public RectangleLinearGradientBrush()
     {
          ResizeRedraw = true; 
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&amp;Gradient-Mode");
   
          foreach (LinearGradientMode gm in 
                              Enum.GetValues(typeof(LinearGradientMode)))
          {
               MenuItem mi = new MenuItem();
               mi.Text     = gm.ToString(); 
               mi.Click   += new EventHandler(MenuGradientModeOnClick);
               Menu.MenuItems[0].MenuItems.Add(mi);
          }
          miChecked = Menu.MenuItems[0].MenuItems[0];
          miChecked.Checked = true;
     }
     void MenuGradientModeOnClick(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)
     {
          Rectangle rectBrush = 
                         new Rectangle(cx / 4, cy / 4, cx / 2, cy / 2);
   
          LinearGradientBrush lgbrush = 
               new LinearGradientBrush(
                         rectBrush, Color.White, Color.Black,
                         (LinearGradientMode) miChecked.Index);
   
         grfx.FillRectangle(lgbrush, 0, 0, cx, cy);
         grfx.DrawRectangle(Pens.Black, rectBrush);
     }
}

    


Two-Point Linear Gradient Brush

image_pdfimage_print
   
 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class TwoPointLinearGradientBrush: Form
{
     public static void Main()
     {
          Application.Run(new TwoPointLinearGradientBrush());
     }
     TwoPointLinearGradientBrush()
     {
          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)
     {
          LinearGradientBrush lgbrush = 
               new LinearGradientBrush(
                         new Point(cx / 4, cy / 4), 
                         new Point(3 * cx / 4, 3 * cy / 4),
                         Color.White, Color.Black);
   
          grfx.FillRectangle(lgbrush, 0, 0, cx, cy);
     }
}

    


Draw a line 2

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.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace PenCaps_c
{
    /// <summary>
    /// Summary description for PenCaps.
    /// </summary>
    public class PenCaps2 : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public PenCaps2()
        {
            //
            // 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()
        {
      // 
      // PenCaps
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(440, 405);
      this.Name = "PenCaps";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "PenCaps";
      this.Load += new System.EventHandler(this.PenCaps_Load);

    }
        #endregion

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

    private void PenCaps_Load(object sender, System.EventArgs e)
    {
    
    }

    protected override void OnPaint ( PaintEventArgs e )
    {
      Graphics G = e.Graphics;
      Point[] PtsA = { new Point(10, 10), 
                       new Point(150, 150), 
                       new Point(400, 10) };
      Point[] PtsB = { new Point(10, 40), 
                       new Point(150, 180), 
                       new Point(400, 40) };
      Point[] PtsC = { new Point(10, 70), 
                       new Point(150, 210), 
                       new Point(400, 70) };
      Point[] PtsD = { new Point(10, 100), 
                       new Point(150, 240), 
                       new Point(400, 100) };
      Pen P = new Pen(Color.Blue, 10);

      G.SmoothingMode=SmoothingMode.AntiAlias;
      P.LineJoin=LineJoin.Bevel;
      G.DrawLines(P, PtsA);

      P.LineJoin=LineJoin.Miter;
      G.DrawLines(P, PtsB);

      P.LineJoin=LineJoin.MiterClipped;
      G.DrawLines(P, PtsC);

      P.LineJoin=LineJoin.Round;
      G.DrawLines(P, PtsD);
    }

    }
}


           
          


Simplest Coordinate

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.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace CustomCap_c
{
    /// <summary>
    /// Summary description for CustomCap.
    /// </summary>
    public class CustomCap : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public CustomCap()
        {
            //
            // 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()
        {
      // 
      // CustomCap
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "CustomCap";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "CustomCap";
      this.Load += new System.EventHandler(this.CustomCap_Load);

    }
        #endregion

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

    private void CustomCap_Load(object sender, System.EventArgs e)
    {
    }

    protected override void OnPaint ( PaintEventArgs e )
    {
      Graphics G = e.Graphics;
      Pen P = new Pen(Color.Blue, 1 );
      Point[] Pts = { new Point( 10, 10 ),
                      new Point( 15, 10 ),
                      new Point( 20, 15 ),
                      new Point( 20, 20 ),
                      new Point( 15, 25 ),
                      new Point( 10, 25 ),
                      new Point( 5, 20 ),
                      new Point( 5, 15 ),
                      new Point( 10, 10 )};
      GraphicsPath Path = new GraphicsPath();

      Path.AddLines (Pts);

      G.SmoothingMode=SmoothingMode.AntiAlias;
      CustomLineCap Lc = new CustomLineCap( null, Path );
      Lc.BaseInset=0;
      Lc.WidthScale=1;
      Lc.StrokeJoin=LineJoin.Miter;
      P.CustomEndCap = Lc;
      P.CustomStartCap=Lc;

      G.DrawLine ( P, 50, 150, 200, 150 );
      G.DrawLine ( P, 150, 50, 150, 200 );

      Lc.Dispose();
      Path.Dispose();
      P.Dispose();
    }

    }
}


           
          


Draw line 3

image_pdfimage_print


   

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

public class TestGDI5 : System.Windows.Forms.Form{
    
    //in order to paint something OnPaint method needs to be overridden
    
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) {
        //OnPaint method is a member of Form class 
        //The following call sends pe to an event listener Graphics
        base.OnPaint(pe);
        
        
        Graphics g = pe.Graphics ;
        Pen pn = new Pen( Color.Blue );
        // Rectangle rect = new Rectangle(50, 50, 200, 100);
        Point pt1 = new Point( 30, 30);
        Point pt2 = new Point( 110, 100);
        g.DrawLine( pn, pt1, pt2 ); 

    }
    public static void Main() {
        System.Windows.Forms.Application.Run(new TestGDI5());//display form
    }
}