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);    //以指定的格式保存图片文件
        }
    }
}

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeRecently

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值