Gradient Label

image_pdfimage_print


   


/*
Code revised from chapter 3


GDI+ Custom Controls with Visual C# 2005
By Iulian Serban, Dragos Brezoi, Tiberiu Radu, Adam Ward 

Language English
Paperback 272 pages [191mm x 235mm]
Release date July 2006
ISBN 1904811604

Sample chapter
http://international.us.server12.fileserver.kutayzorlu.com/files/download/2017/01/1604_CustomControls_SampleChapter_uuid-6297319b-9374-4099-b18c-41b668f47669_crc-0.pdf


For More info on GDI+ Custom Control with Microsoft Visual C# book 
visit website www.packtpub.com 


*/ 

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;

namespace GradientLabelTest00
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            this.gradientLabel1 = new GradientLabelTest00.GradientLabel();
            this.SuspendLayout();

            this.gradientLabel1.BackColor = System.Drawing.SystemColors.ControlLight;
            this.gradientLabel1.BackColor2 = System.Drawing.SystemColors.ControlDarkDark;
            this.gradientLabel1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
            this.gradientLabel1.Location = new System.Drawing.Point(66, 68);
            this.gradientLabel1.Name = "gradientLabel1";
            this.gradientLabel1.Size = new System.Drawing.Size(150, 47);
            this.gradientLabel1.TabIndex = 0;
            this.gradientLabel1.Text = "Gradient Label";
            // 
            // 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.Controls.Add(this.gradientLabel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private GradientLabel gradientLabel1;        
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

    }
    public partial class GradientLabel : Control
    {
        private Color backColor2 = SystemColors.ControlLight;
        public GradientLabel()
        {
            InitializeComponent();
        }
        public Color BackColor2
        {
            get
            {
                return backColor2;
            }
            set
            {
                if (backColor2 != value)
                {
                    backColor2 = value;
                    Invalidate();
                }
            }
        }

        private void GradientLabel_Paint(object sender, PaintEventArgs e)
        {
        }
        protected override void OnPaint(PaintEventArgs e)
        {
                base.OnPaint(e);
                LinearGradientBrush brush = new LinearGradientBrush(new Point(0, 0), new Point(0, Height), BackColor, BackColor2);
                e.Graphics.FillRectangle(brush, ClientRectangle);
                Brush foreBrush = new SolidBrush(ForeColor);
                SizeF textSize = e.Graphics.MeasureString(Text, Font);
                e.Graphics.DrawString(Text, Font, foreBrush, (Width - textSize.Width) / 2, (Height - textSize.Height) / 2);
                brush.Dispose();
                foreBrush.Dispose();
        }

        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing &amp;&amp; (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Component Designer generated code

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.BackColor = System.Drawing.SystemColors.ControlDarkDark;
            this.ForeColor = System.Drawing.SystemColors.HotTrack;
            this.Size = new System.Drawing.Size(150, 24);
            this.Text = "Gradient Label";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.GradientLabel_Paint);
            this.ResumeLayout(false);
        }
        #endregion        
    }
}
           
          


Use LinearGradientBrush to draw a Rectangle

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{
  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

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

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;

      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

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

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 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

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