C#—图形图像编程基础

/*
 * 绘制要求的线;
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.CadetBlue, 10.5f);
            g.DrawString("蓝色,宽度为10.5", this.Font, new SolidBrush(Color.CadetBlue), new PointF(5.0f, 10.0f));
            g.DrawLine(p, new Point(110, 10), new PointF(380, 10));

            p.Width = 2;
            p.Color = Color.Red;
            g.DrawString("红色,宽度为2", this.Font, new SolidBrush(Color.Black), 5,25);
            g.DrawLine(p, new Point(110, 30), new PointF(380, 30));

            p.StartCap = LineCap.Flat;
            p.EndCap = LineCap.ArrowAnchor;
            p.Width = 9;
            g.DrawString("红色箭头线", this.Font, new SolidBrush(Color.Black), 5, 45);
            g.DrawLine(p, new Point(110, 50), new PointF(380, 50));

            p.DashStyle = DashStyle.Custom;
            p.DashPattern = new float[] { 4, 4 };
            p.Width = 2;
            p.EndCap = LineCap.NoAnchor;
            g.DrawString("自定义虚线", this.Font, new SolidBrush(Color.Black), 5, 65);
            g.DrawLine(p, new Point(110, 70), new PointF(380, 70));

            p.DashStyle = DashStyle.Dot;
            g.DrawString("点划线", this.Font, new SolidBrush(Color.Black), 5, 85);
            g.DrawLine(p, new Point(110, 90), new Point(380, 90));
        }
    }
}

运行结果:

C#—图形图像编程基础_第1张图片


/*
 * 纹理画刷TextureBrush类应用实例:将一副名为b.png的图像添加到项目中,
 * 并设置其“复制到输出目录”属性为“如果较新则复制”;
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Bitmap bitmap = new Bitmap(Application.StartupPath + @"\b.png");   //创建Bitmap对象
            bitmap = new Bitmap(bitmap, this.ClientRectangle.Size);     //将其缩放到当前窗体大小
            TextureBrush brush = new TextureBrush(bitmap);
            g.FillEllipse(brush, this.ClientRectangle);
            g.Dispose();
        }
    }
}

运行结果:

C#—图形图像编程基础_第2张图片


/*
 * 图案画刷HatchBrush类应用。该画刷使用90%的阴影,前景色与背景色的比例为90:100,并使用白色作为前景色,红色作为背景色.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            HatchBrush brush = new HatchBrush(HatchStyle.Percent90, Color.White, Color.Red);
            g.FillEllipse(brush, new Rectangle(10, 10, 300, 100));
        }
    }
}

运行结果:

C#—图形图像编程基础_第3张图片


/*
 * LinearGradientBrush类创建渐变画刷实例。实现由白色渐变到黑色.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            LinearGradientBrush brush1 = new LinearGradientBrush(this.ClientRectangle, Color.White, Color.Black, LinearGradientMode.Vertical);
            g.FillRectangle(brush1, this.ClientRectangle);
            g.Dispose();
        }
    }
}
运行结果:

C#—图形图像编程基础_第4张图片



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