zl程序教程

您现在的位置是:首页 >  其他

当前栏目

GDI+ 绘图教程 验证码

教程 验证码 绘图 gdi
2023-09-11 14:14:39 时间

使用的 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;
        }
    }
}