Path Gradient

image_pdfimage_print


   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Drawing2D;  // PathGradientBrush 

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

        public PathGradient()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            
            this.Text = "PathGradient - CenterPoint";

            //
            // 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()
        {
            // 
            // PathGradient
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(256, 213);
            this.Name = "PathGradient";
            this.Text = "PathGradient";

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new PathGradient());
        }
        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);
            GraphicsPath gp;
            PathGradientBrush pGB;  // namespace System.Drawing.Drawing2D;
            Rectangle rec;
            Color cR = Color.Red, cW = Color.White, cY = Color.Yellow;
            int w = 100, h = 70;

            // Left upper rectangle:
            g.DrawString("Center", f, fb, 10, 5);
            gp = new GraphicsPath();
            rec = new Rectangle(10, 20, w, h);
            gp.AddRectangle(rec);
            pGB = new PathGradientBrush(gp);
            pGB.CenterPoint = new Point(10 + w/2, 20 + h/2);
            pGB.CenterColor = cR;
            pGB.SurroundColors = new Color[1]{cW};
            g.FillRectangle(pGB, rec);

            // Right upper rectangle:
            g.DrawString("Center - 2 x 2 Colors", f, fb, w + 20, 5);
            gp = new GraphicsPath();
            rec = new Rectangle(20 + w, 20, w, h);
            gp.AddRectangle(rec);
            pGB = new PathGradientBrush(gp);
            pGB.CenterPoint = new Point(w + 20 + w/2, 20 + h/2);
            pGB.CenterColor = cR;
            pGB.SurroundColors = new Color[4]{cW, cY, cW, cY};
            g.FillRectangle(pGB, rec);

            // Left down rectangle:
            g.DrawString("LefTopCenter", f, fb, 10, h + 25);
            gp = new GraphicsPath();
            rec = new Rectangle(10, h + 40, w, h);
            gp.AddRectangle(rec);
            pGB = new PathGradientBrush(gp);
            pGB.CenterPoint = new Point(10, h + 40);
            pGB.CenterColor = cR;
            pGB.SurroundColors = new Color[1]{cW};
            g.FillRectangle(pGB, rec);

            // Ellipse
            g.DrawString("Top", f, fb, w + 20, h + 25);
            gp = new GraphicsPath();
            rec = new Rectangle(w + 20, h + 40, w, h);
            gp.AddEllipse(rec);
            pGB = new PathGradientBrush(gp);
            pGB.CenterPoint = new Point(w + 20 + w/2, h + 40);
            pGB.CenterColor = cR;
            pGB.SurroundColors = new Color[1]{cW};
            g.FillRectangle(pGB, rec);

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


           
          


Line Gradient

image_pdfimage_print


   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Drawing2D;  // LinearGradientBrush

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

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

            this.Text = "LinearGradientMode";

            //
            // 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()
        {
            // 
            // LinGradient
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(256, 205);
            this.Name = "LinGradient";
            this.Text = "LinGradient";

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new LinGradient());
        }
        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;  // namespace System.Drawing.Drawing2D;
            Color cR = Color.Red, cW = Color.White;
            int w = 100, h = 70;

            // Left upper rectangle:
            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);
         
            // Right upper rectangle:
            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);

            // Left down rectangle:
            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);

            // Right down rectangle:
            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();
        }
    }
}


           
          


Gradient Label Host

image_pdfimage_print


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;

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

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

        public GradientLabelHost()
        {
            //
            // 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.gradientLabel1 = new GradientLabel();
            this.SuspendLayout();
            // 
            // gradientLabel1
            // 
            this.gradientLabel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.gradientLabel1.Font = new System.Drawing.Font("Tahoma", 36F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.gradientLabel1.ForeColor = System.Drawing.Color.White;
            this.gradientLabel1.GradientFill.ColorA = System.Drawing.SystemColors.Info;
            this.gradientLabel1.GradientFill.ColorB = System.Drawing.Color.DarkViolet;
            this.gradientLabel1.GradientFill.GradientFillStyle = System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal;
            this.gradientLabel1.Name = "gradientLabel1";
            this.gradientLabel1.Size = new System.Drawing.Size(596, 118);
            this.gradientLabel1.TabIndex = 0;
            this.gradientLabel1.Text = "Gradient Label Test";
            // 
            // GradientLabelHost
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(596, 118);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.gradientLabel1});
            this.Name = "GradientLabelHost";
            this.Text = "GradientLabelHost";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new GradientLabelHost());
        }
    }
    /// <summary>
    /// Summary description for GradientLabel.
    /// </summary>
    public class GradientLabel : System.Windows.Forms.UserControl
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public GradientLabel()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitForm 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 Component 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()
        {
            // 
            // GradientLabel
            // 
            this.Name = "GradientLabel";
            this.Size = new System.Drawing.Size(372, 128);
            this.Load += new System.EventHandler(this.GradientLabel_Load);
            gradient.GradientChanged += new EventHandler(GradientChanged);

        }
        #endregion



        private string text;
        private GradientFill gradient = new GradientFill();

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public GradientFill GradientFill
        {
            get
            {
                return gradient;
            }
            set
            {
                gradient = value;
                gradient.GradientChanged += new EventHandler(GradientChanged);
                this.Invalidate();
            }
        }


        private void GradientLabel_Load(object sender, System.EventArgs e)
        {
            this.ResizeRedraw=true;
        }

        [Browsable(true),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 
        public override string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
                this.Invalidate();
            }
        }

        protected override void OnPaintBackground(
            System.Windows.Forms.PaintEventArgs e)
        {
            LinearGradientBrush brush = new LinearGradientBrush(e.ClipRectangle, gradient.ColorA,
                                     gradient.ColorB, gradient.GradientFillStyle);

            // Draw the gradient background.
            e.Graphics.FillRectangle(brush, e.ClipRectangle);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);

            // Draw the label text.
            e.Graphics.DrawString(text, this.Font, new SolidBrush(this.ForeColor), 0, 0);
        }

        private void GradientChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }

    }




    [TypeConverter(typeof(ExpandableObjectConverter)), 
    Editor(typeof(GradientFillEditor), typeof(UITypeEditor))]
    public class GradientFill
    {
        private Color colorA = Color.LightBlue;
        private Color colorB = Color.Purple;
        private LinearGradientMode gradientStyle= LinearGradientMode.ForwardDiagonal;
    
        public event EventHandler GradientChanged;

        public Color ColorA
        {
            get
            {
                return colorA;
            }
            set
            {
                colorA = value;
                OnGradientChanged(new EventArgs());
            }
        }

        public Color ColorB
        {
            get
            {
                return colorB;
            }
            set
            {
                colorB = value;
                OnGradientChanged(new EventArgs());
            }
        }

        [System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
        public LinearGradientMode GradientFillStyle
        {
            get
            {
                return gradientStyle;
            }
            set
            {
                gradientStyle = value;
                OnGradientChanged(new EventArgs());
            }
        }

        private void OnGradientChanged(EventArgs e)
        {
            if (GradientChanged != null)
            {
                GradientChanged(this, e);
            }
        }
    }


    public class GradientFillEditor : UITypeEditor
    {


        public override bool GetPaintValueSupported(
            System.ComponentModel.ITypeDescriptorContext context)
        {
            return true;
        }

        public override void PaintValue(
            System.Drawing.Design.PaintValueEventArgs e)
        {
            GradientFill fill = (GradientFill)e.Value;
            LinearGradientBrush brush = new LinearGradientBrush(e.Bounds,
                fill.ColorA, fill.ColorB, fill.GradientFillStyle);

            // Paint the thumbnail.
            e.Graphics.FillRectangle(brush, e.Bounds);
        }
    }



}


           
          


Gradient Wrap

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 GradientWrap_c
{
    /// <summary>
    /// Summary description for GradientWrap.
    /// </summary>
    public class GradientWrap : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

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

    }
        #endregion

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

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

    protected override void OnPaint( PaintEventArgs e )
    {
      GraphicsPath Path = new GraphicsPath();
      Rectangle R = new Rectangle(10, 10, 50, 50);
      e.Graphics.DrawRectangle(Pens.Black,R);
      Path.AddRectangle(R);
      
//      PathGradientBrush B = new PathGradientBrush(Path.PathPoints); 
      PathGradientBrush B = new PathGradientBrush(Path.PathPoints, 
        WrapMode.Tile);
      Color[] c = { Color.Blue, Color.Aqua, Color.Red };

      B.CenterColor = Color.White;
      B.SurroundColors = c;

      //Small circle inside gradient path
      e.Graphics.FillEllipse(B, 15, 15, 30, 30);
      //Large circle outside gradient path
      e.Graphics.FillEllipse(B, 50, 50, 150, 150);
    }

    }
}


           
          


Gradient Demo

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

namespace GradientBlend_c
{
  /// <summary>
    /// Summary description for GradientBlend.
    /// </summary>
    public class GradientBlend : System.Windows.Forms.Form
    {
    private System.Windows.Forms.HScrollBar BlendWidth;
        private System.ComponentModel.Container components = null;
    private System.Windows.Forms.HScrollBar Skew;
    private System.Windows.Forms.Button cmdDoubleBuffer;

    private int BlWidth;
    private int SkewVal;
    private Rectangle EL1Rect;
    private Rectangle EL2Rect;
    private Region EL1Region;
    private Region EL2Region;
    private Region EL3Region;

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

      //Set up rectangles to draw ellipses in
      EL1Rect = new Rectangle(10, 10, 150, 50);
      EL2Rect = EL1Rect;
      //I could make a new rectangle but I can offset without knowing 
      //anything about the previous rectangle.
      EL2Rect.Offset(200, 0);

      //Set up Regions for invalidation
      EL1Region = new Region(EL1Rect);
      EL2Region = new Region(EL2Rect);
      EL3Region = new Region( new Rectangle(new Point(0, 65), 
                                            new Size(this.Width, 50)));


      //Set up the blend scroll bar
      BlendWidth.Top = 120;
      BlendWidth.Left = this.Width/3;
      BlendWidth.Width = this.Width/3;
      BlendWidth.Minimum = 10;
      BlendWidth.Maximum = 200;
      BlendWidth.SmallChange = 1;
      BlendWidth.LargeChange = 10;
      BlendWidth.Value = BlendWidth.Minimum;

      //Set up the Skew Scroll Bar
      Skew.Top = 145;
      Skew.Left = this.Width/3;
      Skew.Width = this.Width/3;
      Skew.Minimum = 10;
      Skew.Maximum = 40;
      Skew.SmallChange = 1;
      Skew.LargeChange = 10;
      Skew.Value = Skew.Minimum;

      //Set up the double buffer button
      cmdDoubleBuffer.Top = Skew.Top + Skew.Height + 5;
      cmdDoubleBuffer.Width = Skew.Width;
      cmdDoubleBuffer.Left = Skew.Left;
      cmdDoubleBuffer.Text = "Allow Flicker";

      BlWidth = BlendWidth.Value;
      SkewVal = Skew.Value;

      // Set up for double buffering.
      //This, along with invalidating only those areas that need it TOTALY
      //eliminate flicker in this program
      this.SetStyle ( ControlStyles.AllPaintingInWmPaint, true);
      this.SetStyle ( ControlStyles.DoubleBuffer, true);
      this.SetStyle ( ControlStyles.UserPaint, true);

    }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
        //Dispose of our own objects
        EL1Region.Dispose();
        EL2Region.Dispose();
        EL3Region.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.BlendWidth = new System.Windows.Forms.HScrollBar();
      this.Skew = new System.Windows.Forms.HScrollBar();
      this.cmdDoubleBuffer = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // BlendWidth
      // 
      this.BlendWidth.Location = new System.Drawing.Point(32, 224);
      this.BlendWidth.Name = "BlendWidth";
      this.BlendWidth.Size = new System.Drawing.Size(192, 16);
      this.BlendWidth.TabIndex = 0;
      this.BlendWidth.Scroll += new System.Windows.Forms.ScrollEventHandler(this.BlendChange);
      // 
      // Skew
      // 
      this.Skew.Location = new System.Drawing.Point(192, 272);
      this.Skew.Name = "Skew";
      this.Skew.Size = new System.Drawing.Size(104, 16);
      this.Skew.TabIndex = 1;
      this.Skew.Scroll += new System.Windows.Forms.ScrollEventHandler(this.SkewColor);
      // 
      // cmdDoubleBuffer
      // 
      this.cmdDoubleBuffer.Location = new System.Drawing.Point(40, 304);
      this.cmdDoubleBuffer.Name = "cmdDoubleBuffer";
      this.cmdDoubleBuffer.Size = new System.Drawing.Size(248, 24);
      this.cmdDoubleBuffer.TabIndex = 2;
      this.cmdDoubleBuffer.Text = "button1";
      this.cmdDoubleBuffer.Click += new System.EventHandler(this.cmdDoubleBuffer_Click);
      // 
      // GradientBlend
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(392, 373);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.cmdDoubleBuffer,
                                                                  this.Skew,
                                                                  this.BlendWidth});
      this.Name = "GradientBlend";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "GradientBlend";
      this.Load += new System.EventHandler(this.GradientBlend_Load);
      this.ResumeLayout(false);

    }
        #endregion

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

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

    protected override void OnPaint ( PaintEventArgs e )
    {
      e.Graphics.SmoothingMode=SmoothingMode.AntiAlias;

      StandardGradient( e.Graphics );
      e.Graphics.DrawLine(Pens.Black, 0, cmdDoubleBuffer.Bottom+10, this.Width,
                          cmdDoubleBuffer.Bottom+10);
      InterpolateGradient( e.Graphics );

      base.OnPaint(e);
    }

    private void StandardGradient( Graphics G )
    {
      //This brush defines how the color is distributed across the whole 
      //graphics container. Any filled object that gets drawn in the container
      //will pick up the color starting with the color gradient at that 
      //particular point on the screen.
      
      LinearGradientBrush B = new LinearGradientBrush(new PointF(0, 20),
                                           new PointF(BlWidth, SkewVal),
                                           Color.Blue,
                                           Color.Red);

      //Draw an image inside the second rectangle
      G.DrawImage(Image.FromFile("Colorbars.jpg"), EL2Rect);
      
      //Draw a line across the screen with the brush
      //to show the repeating pattern
      Pen P = new Pen(B, 15);
      G.DrawLine ( P, 0, 75, this.Width, 75 );
      //Draw a filled ellipse to show how the colors are used
      G.FillEllipse(B, EL1Rect);

      //Change the starting and ending colors
      //Set the alpha so the image below shows through
      Color[] c = {Color.FromArgb(100, Color.LightBlue),
                   Color.FromArgb(100, Color.DarkBlue)};
      B.LinearColors = c;
      P.Brush = B;
      G.DrawLine ( P, 0, 100, this.Width, 100 );
      G.FillEllipse(B, EL2Rect );

      //Reclaim some memory
      c = null;
      P.Dispose();
      B.Dispose();
    }

    private void InterpolateGradient ( Graphics G )
    {
      //Make a set of colors to use in the blend
      Color[] EndColors = {Color.Green,
                           Color.Yellow,
                           Color.Yellow,
                           Color.Blue,
                           Color.Red,
                           Color.Red};

      //These are the positions of the colors along the Gradient line
      float[] ColorPositions = {0.0f, .20f, .40f, .60f, .80f, 1.0f};

      //Fill the blend object with the colors and their positions
      ColorBlend C_Blend = new ColorBlend();
      C_Blend.Colors = EndColors;
      C_Blend.Positions = ColorPositions;
      
      //Make the linear brush and assign the custom blend to it
      LinearGradientBrush B = new LinearGradientBrush ( new Point(10, 110), 
                                                        new Point(140, 110), 
                                                        Color.White, 
                                                        Color.Black );
      B.InterpolationColors = C_Blend;

      //Make a graphics path that we can fill and show custom blended fill
      GraphicsPath Pth = new GraphicsPath();
      Pth.AddEllipse(20, 210, 120, 50);
      Pth.AddString("Filled String", new FontFamily("Impact"), 
                    (int)FontStyle.Italic, 30, new Point(200, 220), 
                    StringFormat.GenericDefault );
      G.FillPath(B, Pth);

      Pen P = new Pen(B, 20);
      G.DrawLine ( P, 0, 300, this.Width, 300 );

      if (P != null)
        P.Dispose();
      if (B != null)
        B.Dispose();
      if (Pth != null)
        Pth.Dispose();
    }
    private void BlendChange(object sender, 
                             System.Windows.Forms.ScrollEventArgs e)
    {
      BlWidth = BlendWidth.Value;
      //Redraw the first ellipse
      this.Invalidate(EL1Region);
      //Redraw the second ellipse
      this.Invalidate(EL2Region);
      //Redraw the lines
      this.Invalidate(EL3Region);
    }

    private void SkewColor(object sender, 
                           System.Windows.Forms.ScrollEventArgs e)
    {
      SkewVal = Skew.Value;
      //Redraw the first ellipse
      this.Invalidate(EL1Region);
      //Redraw the second ellipse
      this.Invalidate(EL2Region);
      //Redraw the lines
      Invalidate(EL3Region);
    }

    private void cmdDoubleBuffer_Click(object sender, System.EventArgs e)
    {
      if (  this.GetStyle( ControlStyles.AllPaintingInWmPaint ) &amp;&amp; 
        this.GetStyle( ControlStyles.DoubleBuffer ) &amp;&amp;
        this.GetStyle( ControlStyles.UserPaint ) )
      {
        cmdDoubleBuffer.Text = "Eliminate Flicker";
        this.SetStyle ( ControlStyles.AllPaintingInWmPaint, false);
        this.SetStyle ( ControlStyles.DoubleBuffer, false);
      }
      else
      {
        cmdDoubleBuffer.Text = "Allow Flicker";
        this.SetStyle ( ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle ( ControlStyles.DoubleBuffer, true);
      }
    }
    }
}


           
          


GradientBlend-c.zip( 7 k)

Gradient Brushes

image_pdfimage_print


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

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

        public GradientBrushes()
        {
            //
            // 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()
        {
            // 
            // GradientBrushes
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(388, 298);
            this.Name = "GradientBrushes";
            this.Text = "GradientBrushes";
            this.Resize += new System.EventHandler(this.GradientBrushes_Resize);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.GradientBrushes_Paint);

        }
        #endregion



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

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

        private void GradientBrushes_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            ArrayList stylesShown = new ArrayList();
            LinearGradientBrush myBrush;
            int y = 20;
            int x = 20;
    
            foreach (LinearGradientMode gradientStyle in System.Enum.GetValues(typeof(LinearGradientMode)))
            {
                myBrush = new LinearGradientBrush(new Rectangle(x, y, 100, 60), Color.Violet, Color.White, gradientStyle);
                e.Graphics.FillRectangle(myBrush, x, y, 100, 60);
                e.Graphics.DrawString(gradientStyle.ToString(), new Font("Tahoma", 8), Brushes.Black, 110 + x, y + 20);
                y += 70;
            }

        }
    }
}



           
          


Gradient Button

image_pdfimage_print


   


/*
Code revised from chapter 5


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-9204cb98-8c2a-4f19-b5c3-3d6d82b4d0cf_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;


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

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

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.gradientButton1 = new GradientButton();
            this.SuspendLayout();
            // 
            // gradientButton1
            // 
            this.gradientButton1.BackColor = System.Drawing.SystemColors.ControlDark;
            this.gradientButton1.BorderStyle = System.Windows.Forms.Border3DStyle.Raised;
            this.gradientButton1.EndColor = System.Drawing.SystemColors.ActiveCaption;
            this.gradientButton1.Location = new System.Drawing.Point(91, 53);
            this.gradientButton1.Name = "gradientButton1";
            this.gradientButton1.Size = new System.Drawing.Size(100, 40);
            this.gradientButton1.StartColor = System.Drawing.SystemColors.ControlLightLight;
            this.gradientButton1.TabIndex = 0;
            this.gradientButton1.Text = "Gradient Button";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.gradientButton1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private GradientButton gradientButton1;

        private void controlPart2_Click(object sender, EventArgs e)
        {

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

    public partial class GradientButton : BorderGradientPanel
    {
        public GradientButton()
        {
            UpdateAppearance();
            InitializeComponent();
        }
        private bool clicked = false;
        private void UpdateAppearance()
        {
            if (clicked)
            {
                StartColor = SystemColors.Control;
                EndColor = SystemColors.ControlLight;
                BorderStyle = Border3DStyle.Sunken;
            }
            else
            {
                StartColor = SystemColors.ControlLight;
                EndColor = SystemColors.Control;
                BorderStyle = Border3DStyle.Raised;
            }

        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                clicked = true;
                UpdateAppearance();
            }
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                clicked = false;
                UpdateAppearance();
            }
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Brush foreBrush = new SolidBrush(ForeColor);
            SizeF size = pe.Graphics.MeasureString(Text, Font);
            PointF pt = new PointF((Width - size.Width) / 2, (Height - size.Height) / 2);
            if (clicked)
            {
                pt.X += 2;
                pt.Y += 2;
            }
            pe.Graphics.DrawString(Text, Font, foreBrush, pt);
            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()
        {
            components = new System.ComponentModel.Container();
            //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }

        #endregion
        
    }


    public partial class BorderGradientPanel : Control
    {
        private Border3DStyle borderStyle = Border3DStyle.Sunken;
        private Color startColor = SystemColors.Control;
        private Color endColor = SystemColors.Control;
        public Color EndColor
        {
            get
            {
                return endColor;
            }
            set
            {
                if (endColor != value)
                {
                    endColor = value;
                    Invalidate();
                }
            }
        }
        public Color StartColor
        {
            get
            {
                return startColor;
            }
            set
            {
                if (startColor != value)
                {
                    startColor = value;
                    Invalidate();
                }
            }
            
        }
        public Border3DStyle BorderStyle
        {
            get
            {
                return borderStyle;
            }
            set
            {
                if (borderStyle != value)
                {
                    borderStyle = value;
                    Invalidate();
                }
            }
        }
        public BorderGradientPanel()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            LinearGradientBrush brush = new LinearGradientBrush(new Point(0, 0), new Point(0, Height), startColor, endColor);
            e.Graphics.FillRectangle(brush, ClientRectangle);
            ControlPaint.DrawBorder3D(e.Graphics, ClientRectangle, borderStyle);
            brush.Dispose();
        }

        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing &amp;&amp; (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component 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()
        {
            components = new System.ComponentModel.Container();
            //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }

        #endregion
    }




    public class ScrollArrowButton : ControlPart
    {
        protected override void RenderControl(Graphics g, ButtonState buttonState, CheckState checkState)
        {
            //ControlPaint.DrawScrollButton(g, ClientRectangle, ScrollButton.Up,buttonState);
            ControlPaint.DrawScrollButton(g, ClientRectangle, sButton, buttonState);
        }
        private ScrollButton sButton = ScrollButton.Up;
        public ScrollButton ButtonType
        {
            get
            {
                return sButton;
            }
            set
            {
                if (sButton != value)
                {
                    sButton = value;
                    Invalidate();
                }
            }
        }
    }

    public class CheckButton : ControlPart
    {
        protected override void RenderControl(Graphics g, ButtonState buttonState, CheckState checkState)
        {
            ButtonState bstate = buttonState;
            switch (checkState)
            {
                case CheckState.Checked: bstate = ButtonState.Checked; break;
                case CheckState.Indeterminate: bstate = ButtonState.All; break;
            }
            ControlPaint.DrawCheckBox(g, ClientRectangle, bstate);
        }

    }

    public class ControlPart : Control
    {
        private ButtonState buttonState = ButtonState.Flat;
        private CheckState checkState = CheckState.Unchecked;
        //indicates wheter the mouse is hovering over the control
        protected bool mouseOver = false;
        public ControlPart()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            buttonState = ButtonState.Normal;
            mouseOver = true;
            Invalidate(true);
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            buttonState = ButtonState.Flat;
            mouseOver = false;
            Invalidate(true);
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.Focus();
            if (!(e.Button == MouseButtons.Left)) return;
            buttonState = ButtonState.Pushed;
            switch (checkState)
            {
                case CheckState.Checked: checkState = CheckState.Unchecked; break;
                case CheckState.Unchecked: checkState = CheckState.Checked; break;
                case CheckState.Indeterminate: checkState = CheckState.Unchecked; break;
            }
            Invalidate(true);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (!((e.Button &amp; MouseButtons.Left) == MouseButtons.Left)) return;
            buttonState = ButtonState.Normal;
            Invalidate(true);
        }
        protected virtual void RenderControl(Graphics g, ButtonState buttonState, CheckState checkState)
        {
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            RenderControl(e.Graphics, buttonState, checkState);
        }

    }