GDI绘图

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

 

 

 

 

 

 

 

 

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

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();
        }
 
 

 

生成验证码图片实例this


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

 

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; }

 

神奇的参数3d

/// <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("向右"); } }

 

小坦克移动案例code

 

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
相关文章
相关标签/搜索