首页  编辑  

两个颜色的渐变原理

Tags: /C#/图形图像/   Date Created:

private void button1_Click( object sender , EventArgs e)

{

    Graphics vGraphics = CreateGraphics();

    Rectangle vRectangle = new Rectangle ( 10 , 10 , 200 , 50 );

    Brush vBrush = new LinearGradientBrush (

       vRectangle,

        Color . Red,

        Color . Blue,

        LinearGradientMode . Horizontal);

   vGraphics . FillRectangle(vBrush, vRectangle);

}

private void button2_Click( object sender , EventArgs e)

{

    Color sourceColor = Color . Red;

    Color destColor = Color . Blue;

    int redSpace = destColor . R - sourceColor . R;

    int greenSpace = destColor . G - sourceColor . G;

    int blueSpace = destColor . B - sourceColor . B;

    Graphics vGraphics = CreateGraphics();

    for ( int i = 0 ; i <= 200 ; i ++ )

   {

        Color vColor = Color . FromArgb(

           sourceColor . R + ( int )(( double )i / 200 * redSpace),

           sourceColor . G + ( int )(( double )i / 200 * greenSpace),

           sourceColor . B + ( int )(( double )i / 200 * blueSpace)

       );

       vGraphics . DrawLine( new Pen (vColor), new Point ( 10 + i, 100 ), new Point ( 10 + i, 150 ));

   }

}