编程语言
首页 > 编程语言> > C#:图片加上文字水印(书法印章生成)

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

在这里插入图片描述

标签:C#,text,System,水印,break,印章,Length,imgSrc,using
来源: https://blog.csdn.net/luoyikun/article/details/88963296