GDI简单绘制和验证码功能

**

1、实现简单绘制直线、矩形、扇形

**

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 _01GDI简单绘制1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //一根笔  颜色  一张纸  两点 绘制直线
        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Color.Black);
            Point p1 = new Point(30,30);
            Point p2 = new Point(60, 60);
            g.DrawLine(pen, p1, p2);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.DrawRectangle(new Pen(Brushes.Black, 2), new Rectangle(new Point(20, 20), new Size(80, 80)));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Color.Black);
            Rectangle rectangle = new Rectangle(new Point(10, 10), new Size(100, 100));
            g.DrawPie(pen, rectangle, 30, 90);
        }
    }
}

Form1设计
在这里插入图片描述
**

2、实现点击更换二维码

**

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 _02GDI更换验证码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {

        }
        //点击
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            string str = null;
            for (int i = 0; i < 5; i++)
            {
                int dNum = r.Next(0, 10);
                str += dNum;
            }

            Bitmap bitmap = new Bitmap(150, 30);
            Graphics graphics = Graphics.FromImage(bitmap);
            String[] font = { "楷体", "宋体", "黑体", "仿宋" };
            Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Yellow };
            
            for (int i = 0; i < 5; i++)
            {
                Point point1 = new Point(i * 20, 0);

                graphics.DrawString(str[r.Next(0, 5)].ToString(), new Font(font[r.Next(0, 4)], 25), new SolidBrush(color[r.Next(0, 5)]), point1);
            }
            for (int i = 0; i < 50; i++)
            {
                Point pt1 = new Point(r.Next(0, bitmap.Width), r.Next(0, bitmap.Height));
                Point pt2 = new Point(r.Next(0, bitmap.Width), r.Next(0, bitmap.Height));
                graphics.DrawLine(new Pen(Brushes.Brown), pt1, pt2);
            }
            for (int i = 0; i < 500; i++)
            {
                bitmap.SetPixel(r.Next(0, bitmap.Width), r.Next(0, bitmap.Height), color[r.Next(0, 5)]);
            }
            pictureBox1.Image = bitmap;
        }
    }
}
2.1 实现效果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值