Gradient Demo : Gradient « 2D Graphics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » 2D Graphics » GradientScreenshots 
Gradient Demo
Gradient  Demo

/*
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(101015050);
      EL2Rect = EL1Rect;
      //I could make a new rectangle but I can offset without knowing 
      //anything about the previous rectangle.
      EL2Rect.Offset(2000);

      //Set up Regions for invalidation
      EL1Region = new Region(EL1Rect);
      EL2Region = new Region(EL2Rect);
      EL3Region = new Regionnew Rectangle(new Point(065)
                                            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 Disposebool disposing )
        {
            ifdisposing )
            {
                if (components != null
                {
                    components.Dispose();
                }
        //Dispose of our own objects
        EL1Region.Dispose();
        EL2Region.Dispose();
        EL3Region.Dispose();
      }
            base.Disposedisposing );
        }

        #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(32224);
      this.BlendWidth.Name = "BlendWidth";
      this.BlendWidth.Size = new System.Drawing.Size(19216);
      this.BlendWidth.TabIndex = 0;
      this.BlendWidth.Scroll += new System.Windows.Forms.ScrollEventHandler(this.BlendChange);
      // 
      // Skew
      // 
      this.Skew.Location = new System.Drawing.Point(192272);
      this.Skew.Name = "Skew";
      this.Skew.Size = new System.Drawing.Size(10416);
      this.Skew.TabIndex = 1;
      this.Skew.Scroll += new System.Windows.Forms.ScrollEventHandler(this.SkewColor);
      // 
      // cmdDoubleBuffer
      // 
      this.cmdDoubleBuffer.Location = new System.Drawing.Point(40304);
      this.cmdDoubleBuffer.Name = "cmdDoubleBuffer";
      this.cmdDoubleBuffer.Size = new System.Drawing.Size(24824);
      this.cmdDoubleBuffer.TabIndex = 2;
      this.cmdDoubleBuffer.Text = "button1";
      this.cmdDoubleBuffer.Click += new System.EventHandler(this.cmdDoubleBuffer_Click);
      // 
      // GradientBlend
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(392373);
      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;

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

      base.OnPaint(e);
    }

    private void StandardGradientGraphics 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(020),
                                           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, 075this.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, 0100this.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.80f1.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(10110)
                                                        new Point(140110)
                                                        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(2021012050);
      Pth.AddString("Filled String"new FontFamily("Impact")
                    (int)FontStyle.Italic, 30new Point(200220)
                    StringFormat.GenericDefault );
      G.FillPath(B, Pth);

      Pen P = new Pen(B, 20);
      G.DrawLine P, 0300this.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.GetStyleControlStyles.AllPaintingInWmPaint && 
        this.GetStyleControlStyles.DoubleBuffer &&
        this.GetStyleControlStyles.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)
Related examples in the same category
1.Path Gradient DemoPath Gradient Demo
2.All Linear Gradient ModeAll Linear Gradient Mode
3.Simple way to create linear gradient brushSimple way to create linear gradient brush
4.Path Gradient Brush from Graphics PathPath Gradient Brush from Graphics Path
5.Use LinearGradientBrush to draw a RectangleUse LinearGradientBrush to draw a Rectangle
6.Gradient LabelGradient Label
7.Gradient ButtonGradient Button
8.Gradient BrushesGradient Brushes
9.Gradient WrapGradient Wrap
10.Gradient Label HostGradient Label Host
11.Line GradientLine Gradient
12.Path GradientPath Gradient
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.