打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【新提醒】【C#指定图片添加文字(仅PC)】
本帖最后由 AWNUXCVBN 于 2017-9-28 10:18 编辑
[C#] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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();
}
}
 20170926204317523.jpg (55.94 KB, 下载次数: 0)
下载附件
昨天 10:14 上传
System.Drawing.dll
http://pan.baidu.com/s/1skOuTA1
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
c# 改变图片的大小(w,h)
C# 中使用Image.FromFile(string path)后,提示该文件正在被另一进程使用X...
asp.net 文件上传示例整理
关于asp.net上传图片自动生成缩略图
WinForm控件与WPF控件的交互
C#使用iTextSharp封装的PDF文件操作类实例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服