c#双色渐变绘图

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApp8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //实例化2个point类,作为渐变图形的起始点和结束点
            Point pt1 = new Point(50, 50);
            Point pt2 = new Point(200, 200);

            //实例化Graphics
            Graphics g = this.CreateGraphics();

            //实例化LinearGradientBrush
            //设置起始点和结束点以及渐变色
            LinearGradientBrush myL = new LinearGradientBrush(
                pt1, pt2, Color.Black, Color.AntiqueWhite);

            //填充矩形
            Rectangle myR = new Rectangle(40, 40, 400, 200);
            g.FillRectangle(myL, myR);
        }
    }
}

结果

AfterRun.PNG

控件

只有一个Button控件,代码双击写在click事件下

你可能感兴趣的:(c#双色渐变绘图)