Use LinearGradientBrush to draw a Rectangle


   

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{
  private System.ComponentModel.Container components = null;

  public Form1(){
    InitializeComponent();
        SetStyle(ControlStyles.Opaque, true);
  }
  protected override void Dispose( bool disposing ){
    if( disposing ){
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
    protected override void OnPaint(PaintEventArgs e) {
         Graphics g = e.Graphics;
         g.FillRectangle(Brushes.White, ClientRectangle);

         Brush linearGradientBrush = new LinearGradientBrush(
            new Rectangle(10, 60, 50, 50), Color.Blue, Color.White, 45);
         g.FillRectangle(linearGradientBrush, new Rectangle(10, 60, 50, 50));

         linearGradientBrush.Dispose();
         
    }
  private void InitializeComponent(){
    this.components = new System.ComponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  static void Main() {
    Application.Run(new Form1());
  }
}



           
          


Path Gradient Brush from Graphics Path


   



  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;
      GraphicsPath gp = new GraphicsPath();

      gp.AddLine(10, 10, 110, 15);
      gp.AddLine(110, 15, 100, 95);
      gp.AddLine(100, 95, 15, 110);
      gp.CloseFigure();

      g.FillRectangle(Brushes.White, this.ClientRectangle);
      g.SmoothingMode = SmoothingMode.AntiAlias;

      PathGradientBrush pgb = new PathGradientBrush(gp);
      pgb.CenterColor = Color.White;
      pgb.SurroundColors = new Color[] { Color.Red };
      g.FillPath(pgb, gp);

      g.DrawPath(Pens.Black, gp);
      pgb.Dispose();
      gp.Dispose();

    }

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

           
          


Simple way to create linear gradient brush


   


  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;

      LinearGradientBrush lgb = new LinearGradientBrush(
                         new Point(0, 0),
                         new Point(25, 10),
                         Color.White,
                         Color.Black);
      g.FillRectangle(lgb, this.ClientRectangle);
      lgb.Dispose();
    }

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


           
          


All Linear Gradient Mode

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;
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.Red, Color.Blue, temp); g.DrawString(temp.ToString(), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 0, yOffSet); g. FillRectangle(theBrush, 120, yOffSet, 200, 50); yOffSet += 80; } } } [/csharp]

Path Gradient Demo


   


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 Form1 : Form
{

  public Form1() {
        InitializeComponent();
  }
  private void Form1_Paint(object sender, PaintEventArgs e)
  {
      GraphicsPath path = new GraphicsPath();
      int size = 150;
      path.AddEllipse(10, 10, size, size);
      
      PathGradientBrush brush = new PathGradientBrush(path);
      brush.WrapMode = WrapMode.Tile;
      brush.SurroundColors = new Color[] { Color.White };
      brush.CenterColor = Color.Violet;
      e.Graphics.FillRectangle(brush, 10, 10, size, size);
      
      path.Dispose();
      brush.Dispose();
  }

  private void InitializeComponent()
  {
    this.SuspendLayout();
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Name = "Form1";
    this.Text = "Alpha Blending";
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    this.ResumeLayout(false);

  }


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

}


           
          


Play Gif animation


   


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
  {
    private Bitmap bmp;
    
    public Form1()
    {
      bmp = new Bitmap("winter.jpg");
      ImageAnimator.Animate(bmp, new EventHandler(this.OnFrameChanged));
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
        public static void Main(){
           Application.Run(new Form1());    
        }

    private void OnFrameChanged(object o, EventArgs e)
    {
      this.Invalidate();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
      ImageAnimator.UpdateFrames();
      e.Graphics.DrawImage(this.bmp, new Point(0, 0));
    }
  }

           
          


List Fonts

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {
    public Form1() {
        this.BackColor = Color.White;
    }
    protected override void OnPaint(PaintEventArgs e) {
        int verticalCoordinate = 10;
        Point topLeftCorner;
        InstalledFontCollection insFont = new InstalledFontCollection();
        FontFamily[] families = insFont.Families;
        e.Graphics.TranslateTransform(AutoScrollPosition.X,AutoScrollPosition.Y);
        foreach (FontFamily family in families) {
            if (family.IsStyleAvailable(FontStyle.Regular)) {
                Font f = new Font(family.Name, 10);
                topLeftCorner = new Point(10, verticalCoordinate);
                verticalCoordinate += f.Height;
                e.Graphics.DrawString(family.Name, f, Brushes.Black, topLeftCorner);
                f.Dispose();
            }
        }
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}