C#生成文字图片

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

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            string text = "djhWH0K980";    //将获取到的字符串赋值到text字符串中
            Bitmap bmp = new Bitmap(450, 200);      //定义画布大小
            Graphics g = Graphics.FromImage(bmp);      //封装一个GDI+绘图图面
            Random r = new Random();
            g.Clear(ColorTranslator.FromHtml("#FFF"));  //背景色为白色
            for (int j = 0; j < 10; j++)
            {
                String[] fonts = { "微软雅黑", "Viner Hand ITC", "Tempus Sans ITC", "汉仪长艺体简", "汉仪双线体简", "汉仪花蝶体简" };//随机设置字体的样式
                Point p = new Point((j + 1) * 38, 80);  //每个字母的坐标
                Color[] colors = { Color.Red, Color.Green, Color.Black, Color.Yellow, Color.LightSkyBlue, Color.Blue };//随机设置字体的颜色
                g.DrawString(text[j].ToString(), new Font(fonts[r.Next(fonts.Length)], 40, FontStyle.Bold), new SolidBrush(colors[r.Next(colors.Length)]), p);//画图
            }
            var strFullName =@"D:\" + "测试.jpg";  //存储位置+图片名
            bmp.Save(strFullName, ImageFormat.Jpeg);    //以指定的格式保存图片文件
        }
    }
}

效果图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44790046/article/details/103879605