首页 / C# / C#:图片加上文字水印(书法印章生成)
C#:图片加上文字水印(书法印章生成)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#:图片加上文字水印(书法印章生成),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1600字,纯文字阅读大概需要3分钟。
内容图文
![C#:图片加上文字水印(书法印章生成)](/upload/InfoBanner/zyjiaocheng/835/6f6ec9e1ef134397abfba38de0caf2e1.jpg)
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#:图片加上文字水印(书法印章生成)全部内容,希望文章能够帮你解决C#:图片加上文字水印(书法印章生成)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。