码迷,mamicode.com
首页 > 其他好文 > 详细

GDI+ 绘图教程 验证码

时间:2018-02-24 19:39:29      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:create   phi   颜色   文本   font   rms   +=   c#   ++   

使用的 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+ 绘图教程 验证码

标签:create   phi   颜色   文本   font   rms   +=   c#   ++   

原文地址:https://www.cnblogs.com/cjm123/p/8467125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!