1. 程式人生 > >GDI+ 繪圖教程 驗證碼

GDI+ 繪圖教程 驗證碼

數字 驗證 task idt 操作 poi color rom tex

使用的 C# winform

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GDI_
{
    public partial class Form1 : Form
    {
        
public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 一根筆 繪制直線的對象 pen 顏色 Brush Brushes SolidBrush color 一張紙(圖面) Graphics 兩點 Point } private void button1_Click(object sender, EventArgs e) {
//創建GDI圖面對象 //Graphics g = new Graphics(); 沒有定義構造函數 //創對象 // 1 在堆中開空間 2 在開辟的空間創對象 3 調用構造函數 Graphics g = this.CreateGraphics(); //創建畫筆對象 畫筆 //1 //Pen pen = new Pen(Brushes.Yellow);//Brush 點不出來 看復數 //2 直接給筆上色 Pen pen = new
Pen(Color.Yellow);// //Pen pen = new Pen(new Brush(Color.Yellow));Brush 抽象類報錯 //創建兩個點 Point p1 = new Point(30, 50); Point p2 = new Point(250, 250); g.DrawLine(pen, p1, p2); } int i = 0; private void Form1_Paint(object sender, PaintEventArgs e) { i++; label1.Text = i.ToString(); //創建GDI對象 //Graphics g = new Graphics(); //創對象 // 1 在堆中開空間 2 在開辟的空間創對象 3 調用構造函數 Graphics g = this.CreateGraphics(); //創建畫筆對象 畫筆 Pen pen = new Pen(Brushes.Yellow);// 復數形式 返回對象 //創建兩個點 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 si = new System.Drawing.Size(80, 80); Rectangle rec = new Rectangle(new Point(50, 50), si); // 矩形對象 g.DrawRectangle(pen, rec); } private void button3_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); Pen pen = new Pen(Brushes.Blue); Size si = new System.Drawing.Size(80, 80); Rectangle rec = new Rectangle(new Point(100, 100), si); g.DrawPie(pen, rec, 60, 60);//他要什麽 我們就給什麽 //Draw 畫 在圖上繪制 Graphics } private void button4_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); Font f = new System.Drawing.Font("宋體", 20, FontStyle.Underline); g.DrawString("asd520", f, Brushes.Red, new Point(100, 200)); } } }

技術分享圖片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GDI_驗證碼
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //點擊更換驗證碼
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            //1 產生隨機數
            Random r = new Random();
            string str = null;
            for (int i = 0; i < 5; i++)
            {
                int rNumber = r.Next(0, 10);
                str += rNumber;
            }
            //MessageBox.Show(str);
            // 畫一個圖片 把文本放到圖片裏面去
            //創建GDI對象
            Bitmap bmp = new Bitmap(150, 40);//創建位圖
            //位圖 操作系統中 默認的圖片類型 其實就是位圖(.bmp)
            Graphics g = Graphics.FromImage(bmp); //對象從圖片來 就是在圖片上繪制

            // 畫數字
            for (int i = 0; i < 5; i++)
            {
                Point p = new Point(i * 20, 0);
                string[] fonts = { "宋體", "微軟雅黑", "黑體", "隸書", "仿宋" };
                Color[] colors = { Color.Yellow, Color.Blue, Color.Red, Color.Black, Color.Green };
                g.DrawString(str[i].ToString(), new Font(fonts[r.Next(0, 5)], 20, 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.Green), p1, p2);
            }
            //畫點
            for (int i = 0; i < 500; i++)
            {
                //創建點
                Point p = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
                bmp.SetPixel(p.X, p.Y, Color.Black);//再在圖上畫點
            }


            //將圖片鑲嵌到picturebox中
            pictureBox1.Image = bmp;
        }
    }
}

技術分享圖片

GDI+ 繪圖教程 驗證碼