GDI绘图

孤者浪人 提交于 2020-12-05 06:44:43

什么是GDI+
GDI+ (Graphics Device Interface) 是一种绘图装置接口, 可将应用程序和绘图硬件分隔, 让我们能够编写与装置无关的应用程序。它可以让我们不需注意特定显示装置的详细数据, 便可在屏幕或打印机显示信息。我们可以呼叫 GDI+ 类别所提供的方法, 然后这些方法会适当地呼叫特定的装置驱动程序, 而完成绘图。而且与.NET进行了更好的融合。
GDI (Graphics Device Interface), 是属于绘图方面的 API (Application Programming Interface)。
因为应用程序不能直接控制硬件, 所以当我们要进行绘图的动作时, 必须透过 GDI 才能完成。

 

 

 

 

 

 

 

 

确定坐标系:1.确定原点 2.确定x,y轴和方向

Graphics提供了非常多的绘图的方法可以让我们进行绘制。
绘图方法
Graphics 类别的常用绘图方法有:
DrawLine(直线)、
DrawRectangle (矩形)、
DrawEllipse (椭圆)、
DrawCurve (曲线)、
DarwArc (弧线)、
DrawPie (扇形)、
DrawLines (多边形)、
DrawPolygon (封闭多边形)、
DrawBezier (贝兹曲线)等。

 

 

使用GDI绘制简单的图形

 

//绘制一条直线
        private void button1_Click(object sender, EventArgs e)
        {
            //创建GDI对象
            Graphics g = this.CreateGraphics();// new Graphics();
            //创建画笔对象
            Pen pen = new Pen(Brushes.Red);
            //创建两个点
            Point p1 = new Point(30, 50);
            Point p2 = new Point(250, 250);
            g.DrawLine(pen, p1, p2);

        }

  //绘制一个矩形
        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen=new Pen(Brushes.Yellow);
            Size size=new System.Drawing.Size(80,80);
            Rectangle rec=new Rectangle(new Point(50,50),size);
            g.DrawRectangle(pen,rec);
        }

  //绘制一个扇形
        private void button3_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen=new Pen(Brushes.Blue);
            Size size=new System.Drawing.Size(180,180);
            Rectangle rec=new Rectangle(new Point(150,150),size);
            g.DrawPie(pen, rec, 60, 60);
        }

//绘制一个文本
        private void button4_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.DrawString("老赵是最帅的", new Font("宋体", 20, FontStyle.Underline), Brushes.Black, new Point(300, 300));
        }

//实心画圆
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g=e.Graphics;
            //定义实心填充画笔
            SolidBrush myBrush=new SolidBrush(Color.Yellow);
            g.FillEllipse(myBrush,50,50,300,200);
            myBrush.Dispose();
            g.Dispose();
        }
 

 

生成验证码图片实例


1.通过Random生成随机数或字符及验证码
2.通过验证码内容长度生成指定大小的图片
3.获取生成图片的Graphics对象
4.定义验证码字体格式
5.通过指定字体将验证码绘制到图片
6.向图片上添加背景噪音线
7.添加前景噪音点

 

Random r = new Random();
        /// <summary>
        /// 点击更换验证码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            //创建一张图片对象
            Bitmap bmp = new Bitmap(120, 20);
            //存储的就是验证码
            string str = string.Empty;
            for (int i = 0; i < 5; i++)
            {
                str += r.Next(0, 10);
            }
            //创建GDI+对象
            Graphics g = Graphics.FromImage(bmp);

            string[] fonts = { "微软雅黑", "隶书", "宋体", "黑体", "仿宋" };
            Color[] colors = { Color.Black, Color.Yellow, Color.Red, Color.Green, Color.Gold };

            //开始绘制验证码
            for (int i = 0; i < str.Length; i++)
            {
                Point p = new Point(i * 20, 0);
                g.DrawString(str[i].ToString(), new Font(fonts[r.Next(0, 5)], 15, FontStyle.Bold), new SolidBrush(colors[r.Next(0, 5)]), p);
            }
            //给验证码图片中绘制一些直线
            for (int i = 0; i < 20; i++)
            {
                Point p1 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
                Point p2 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
                g.DrawLine(new Pen(Brushes.Pink), p1, p2);
            }
            //给验证码添加像素颗粒
            for (int i = 0; i < 100; i++)
            {
                bmp.SetPixel(r.Next(0, bmp.Width), r.Next(0, bmp.Height), Color.Black);
            }

            //把创建的位图对象放在pictureBox上
            pictureBox1.Image = bmp;
        }

 

神奇的参数

/// <summary>
        /// 
        /// </summary>
        /// <param name="sender">触发这个事件的对象</param>
        /// <param name="e">关于事件的一些数据</param>
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            label1.Text = e.X + "," + e.Y;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // e.KeyCode 获取用户按下了哪个键
            if (e.KeyCode == Keys.W || e.KeyCode == Keys.Up)
            {
                MessageBox.Show("前进!!!"); } else if (e.KeyCode == Keys.S || e.KeyCode == Keys.Down) { MessageBox.Show("后退"); } else if (e.KeyCode == Keys.A || e.KeyCode == Keys.Left) { MessageBox.Show("向左"); } else if (e.KeyCode == Keys.D || e.KeyCode == Keys.Right) { MessageBox.Show("向右"); } }

 

小坦克移动案例

 

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 小坦克移动.Properties;
namespace 小坦克移动
{
    enum Direction
    {
        Up,
        Down,
        Left,
        Right
    }
    class Tank
    {
        //存储图片
        private Image[] imgs = { 
                               Resources.p1tankU,
                               Resources.p1tankD,
                               Resources.p1tankL,
                               Resources.p1tankR
                                };
        //小坦克的X、Y坐标
        public int X { get; set; }
        public int Y { get; set; }
        //定义4个方向
        public Direction Dir { get; set; }
        //定义坦克移动的速度
        public int Speed { get; set; }
        //定义坦克的构造函数
        public Tank(int x, int y, int speed, Direction dir)
        {
            this.X = x;
            this.Y = y;
            this.Speed = speed;
            this.Dir = dir;
        }

        //小坦克移动的行为
        public void Move()
        {
            //根据方向 来判断小坦克要改变的坐标
            switch (this.Dir)
            {
                case Direction.Up:
                    this.Y -= this.Speed;
                    break;
                case Direction.Down:
                    this.Y += this.Speed;
                    break;
                case Direction.Left:
                    this.X -= this.Speed;
                    break;
                case Direction.Right:
                    this.X += this.Speed;
                    break;
            }
            //移动完成后 判断小坦克是否超过了窗体
            if (this.X <= 0)
            {
                this.X = 0;
            }
            if (this.Y <= 0)
            {
                this.Y = 0;
            }
            if (this.X >= 700-80)
            {
                this.X = 620;
            }
            if (this.Y >= 600-100)
            {
                this.Y = 500;
            }
        }
        public void DrawTank(Graphics g)
        {
            switch (this.Dir)
            { 
                case Direction.Up:
                    g.DrawImage(imgs[0], this.X, this.Y);
                    break;
                case Direction.Down:
                    g.DrawImage(imgs[1], this.X, this.Y);
                    break;
                case Direction.Left:
                    g.DrawImage(imgs[2], this.X, this.Y);
                    break;
                case Direction.Right:
                    g.DrawImage(imgs[3], this.X, this.Y);
                    break;
            }
        }

        public void TankKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.W || e.KeyCode == Keys.Up)
            {
                this.Dir = Direction.Up;
            }
            else if (e.KeyCode == Keys.S || e.KeyCode == Keys.Down)
            {
                this.Dir = Direction.Down;
            }
            else if (e.KeyCode == Keys.A || e.KeyCode == Keys.Left)
            {
                this.Dir = Direction.Left;
            }
            else if (e.KeyCode == Keys.D || e.KeyCode == Keys.Right)
            {
                this.Dir = Direction.Right;
            }
            this.Move();//让坦克移动
        }


    }
}
Tank Class
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Tank tank;
        private void Form1_Load(object sender, EventArgs e)
        {
            //创建小坦克对象
            tank = new Tank(200, 200, 10, Direction.Up);
            //设置双缓冲 减少屏幕闪烁
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer
                       | ControlStyles.ResizeRedraw
                       | ControlStyles.Selectable
                       | ControlStyles.AllPaintingInWmPaint
                       | ControlStyles.UserPaint
                       | ControlStyles.SupportsTransparentBackColor,
                     true);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //在绘制窗体的时候 将坦克也绘制出来
            tank.DrawTank(e.Graphics);

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //获取用户按下了哪个键盘
            tank.TankKeyDown(e);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //不停的执行Paint事件
            this.Invalidate();
        }
    }
Form1

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!