C#:图片加上文字水印(书法印章生成)

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());
              
            }
        }
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

四夕立羽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值