什麼是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();//讓坦克移動 } } }
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(); } }