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

    


FontFamilies: IsStyleAvailable,

   
 

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

class FamiliesList : Form {
    const int iPointSize = 12;

    public static void Main() {
        Application.Run(new FamiliesList());
    }
    public FamiliesList() {
        Text = "Font Families List";
        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 x = 0, y = 0, fMaxWidth = 0;

        FontCollection fc = new InstalledFontCollection();
        FontFamily[] aff = fc.Families;

        foreach (FontFamily ff in aff) {
            Font font = CreateSampleFont(ff, iPointSize);
            SizeF sizef = grfx.MeasureString(ff.Name, font);

            fMaxWidth = Math.Max(fMaxWidth, sizef.Width);
        }
        foreach (FontFamily ff in aff) {
            Font font = CreateSampleFont(ff, iPointSize);
            float fHeight = font.GetHeight(grfx);

            if (y > 0 &amp;&amp; y + fHeight > cy) {
                x += fMaxWidth;
                y = 0;
            }
            grfx.DrawString(ff.Name, font, brush, x, y);

            y += fHeight;
        }
    }

    Font CreateSampleFont(FontFamily ff, float fPointSize) {
        if (ff.IsStyleAvailable(FontStyle.Regular))
            return new Font(ff, fPointSize);

        else if (ff.IsStyleAvailable(FontStyle.Bold))
            return new Font(ff, fPointSize, FontStyle.Bold);

        else if (ff.IsStyleAvailable(FontStyle.Italic))
            return new Font(ff, fPointSize, FontStyle.Italic);

        else
            return Font;
    }
}

    


Write font metrics for Times New Roman font family

   
 


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

public class MainClass {

    public static void Main() {
    String formatString = "{0,-16}{1,8}{2,9}{3,10}{4,14}";
    Console.WriteLine(formatString, "Font Family Name", "Ascent", "Descent","EmHeight", "Line Spacing");
    FontFamily ff = new FontFamily("Times New Roman");
    Console.WriteLine(formatString, ff.GetName(0),
      ff.GetCellAscent(FontStyle.Regular),
      ff.GetCellDescent(FontStyle.Regular),
      ff.GetEmHeight(FontStyle.Regular),
      ff.GetLineSpacing(FontStyle.Regular));
    }
}