C#指定图片添加文字
来源:互联网 发布:博达软件 编辑:程序博客网 时间:2024/06/05 06:49
using System.Drawing;using System.IO;using UnityEngine;public class TestImage : MonoBehaviour{ private string filePath; // Use this for initialization void Start() { filePath = @Application.streamingAssetsPath + "/test.jpg"; AddTextToImg("卧槽啊!小贱!"); } // Update is called once per frame void Update() { } /// <summary> /// 指定图片添加指定文字 /// </summary> /// <param name="text">添加的文字</param> /// <param name="picname">生成文件名</param> private void AddTextToImg(string text) { //判断指定图片是否存在 if (!File.Exists(filePath)) { throw new FileNotFoundException("The file don't exist!"); } if (text == string.Empty) { return; } Image image = Image.FromFile(filePath); Bitmap bitmap = new Bitmap(image, image.Width, image.Height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //字体大小 float fontSize = 40.0f; //文本的长度 float textWidth = text.Length * fontSize; //下面定义一个矩形区域,以后在这个矩形里画上白底黑字 float rectX = 120; float rectY = 200; float rectWidth = text.Length * (fontSize + 40); float rectHeight = fontSize + 40; //声明矩形域 RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight); //定义字体 System.Drawing.Font font = new System.Drawing.Font("微软雅黑", fontSize, System.Drawing.FontStyle.Bold); //font.Bold = true; //白笔刷,画文字用 Brush whiteBrush = new SolidBrush(System.Drawing.Color.DodgerBlue); //黑笔刷,画背景用 //Brush blackBrush = new SolidBrush(Color.Black); //g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight); g.DrawString(text, font, whiteBrush, textArea); //输出方法一:将文件生成并保存到C盘 string path = @Application.streamingAssetsPath + "/test2.jpg"; bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); g.Dispose(); bitmap.Dispose(); image.Dispose(); }}
原文点击这里
阅读全文
0 0
- C#指定图片添加文字
- C#指定图片添加文字——修改版
- C# 添加水印图片、文字、缩略图处理
- 给服务器上的指定的图片添加文字水印
- android图片添加文字保存到指定文件夹
- c#操作word 在指定书签插入文字或者图片
- C#做的图片添加文字的程序
- c# 使用Graphic 为图片添加水印 及文字
- C#简单的在图片上添加文字并保存
- 图片上添加文字
- 图片上添加文字
- 给图片添加文字
- java图片添加文字
- 图片上添加文字
- 导出图片添加文字
- 为图片添加文字
- 给图片添加文字
- 图片上添加文字
- 机器学习三(tensorflow 训练识别手写数字)
- 第一次写博客
- Win8.1无法安装msi软件提示2502、2503错误怎么解决?
- maven中端口占用报错
- js构造函数,原型对象,继承 (2)
- C#指定图片添加文字
- springboot-加载自定义的properties文件
- 主从复制、读写分离水平拆分及库表散列
- Java编辑pdf
- oracle数据库用户管理操作(DCL)
- CodeForces
- 常见的简单笔试题却又要小心的题
- Unity2D游戏入门教程(一)宝石迷阵(1)创建项目
- hdu 1018