标签:C# text System 水印 break 印章 Length imgSrc using
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TextToPng
{
class Program
{
static void Main(string[] args)
{
if (args != null && args.Length > 0)
{
PngAddText(AppDomain.CurrentDomain.BaseDirectory + @"kuang.png", args[0]);
}
else {
PngAddText(AppDomain.CurrentDomain.BaseDirectory + @"kuang.png", "立羽");
}
Console.WriteLine("SaveImgOK");
}
/// <summary>
/// 图片上嵌入文字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void PngAddText(string imgPath,string text)
{
try
{
System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(imgPath);
if (text.Length < 4)
{
text += "印";
}
float x = 0;
float y = 0;
float xOffset = 20;
using (Graphics g = Graphics.FromImage(imgSrc))
{
g.DrawImage(imgSrc, 0, 0, imgSrc.Width, imgSrc.Height);
using (Font f = new Font("楷体", 65, FontStyle.Bold))
{
using (Brush b = new SolidBrush(Color.Red))
{
for (int i = 0; i < text.Length; i++)
{
switch (i)
{
case 0:
x = imgSrc.Width / 2 - xOffset;
y = xOffset;
break;
case 1:
x = imgSrc.Width / 2 - xOffset;
y = imgSrc.Height / 2;
break;
case 2:
if (text.Length == 4)
{
x = 0;
y = xOffset;
}
else if (text.Length == 3)
{
x = 0;
y = imgSrc.Height / 2 - 65 / 2.0f;
}
break;
case 3:
x = 0;
y = imgSrc.Height / 2;
break;
default:
break;
}
g.DrawString(text[i].ToString(), f, b, x, y);
}
}
}
}
string fontpath = AppDomain.CurrentDomain.BaseDirectory + @"save.png";
imgSrc.Save(fontpath, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}
标签:C#,text,System,水印,break,印章,Length,imgSrc,using 来源: https://blog.csdn.net/luoyikun/article/details/88963296
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。