C# 画图(1)

protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            //获取画板
            Graphics gfx = paintEvnt.Graphics;
            // 构造画笔
            Pen myPen = new Pen(Color.Black);
            // 画线
            for (int i = 20; i < 250; i = i + 10)
            {
                gfx.DrawLine(myPen, 20, i, 270, i);
            }
        }

C# 画图(1)

垂直画一遍,画出方格:

protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            // Get the graphics object
            Graphics gfx = paintEvnt.Graphics;
            // Create a new pen that we shall use for drawing the line
            Pen myPen = new Pen(Color.Black);
            // Loop and create a horizontal line 10 pixels below the last one
            for(int i = 20; i <= 250; i = i + 10)
            {
                gfx.DrawLine(myPen, 20, i, 270, i);
            }
            // Loop and create a vertical line 10 pixels next to the last one
            for(int x = 20; x < 280; x = x + 10)
            {
                gfx.DrawLine(myPen, x, 20, x, 250);
            }
        }
画方框:

protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            // Get the graphics object
            Graphics gfx = paintEvnt.Graphics;
            // Create a new pen that we shall use for drawing the line
            Pen myPen = new Pen(Color.Black);
            // Loop until the coordinates reach 250 (the lower right corner of the form)
            for(int i = 0; i < 250; i = i + 50)
            {
                // Draw a 50x50 pixels rectangle
                gfx.DrawRectangle(myPen, i, i, 50, 50);
            }
        }

循环,画渐变色

 

        protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            // Get the graphics object
            Graphics gfx = paintEvnt.Graphics;
            int x1 = 0;
            int y1 = 0;
            // Loop trough the 255 values red can have
            for (int i = 0; i <= 255; i++)
            {
                // 指定画刷颜色
                Color brushColor = Color.FromArgb(i, 0, 0);
                // 实心画刷用来画实心矩形
                SolidBrush myBrush = new SolidBrush(brushColor);
                // 绘制
                gfx.FillRectangle(myBrush, x1, y1, 10, 10);
                // 挨着画下一个矩形
                x1 = x1 + 10;
                //当一行结束后,画下一行
                if ((x1 % 290) == 0)
                {
                    y1 = y1 + 10;
                    x1 = 0;
                }
            }
            for (int i = 0; i <= 255; i++)
            {
                Color brushColor = Color.FromArgb(0, i, 0);
                SolidBrush myBrush = new SolidBrush(brushColor);
                gfx.FillRectangle(myBrush, x1, y1, 10, 10);
                x1 = x1 + 10;
                if ((x1 % 290) == 0)
                {
                    y1 = y1 + 10;
                    x1 = 0;
                }
            }
            for (int i = 0; i <= 255; i++)
            {
                Color brushColor = Color.FromArgb(0, 0, i);
                SolidBrush myBrush = new SolidBrush(brushColor);
                gfx.FillRectangle(myBrush, x1, y1, 10, 10);
                x1 = x1 + 10;
                if ((x1 % 290) == 0)
                {
                    y1 = y1 + 10;
                    x1 = 0;
                }
            }


        }

C# 画图(1)

你可能感兴趣的:(C#)