前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >C#:图片加上文字水印(书法印章生成)

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

作者头像
立羽
发布2023-08-24 14:55:30
发布2023-08-24 14:55:30
33500
代码可运行
举报
文章被收录于专栏:Unity3d程序开发Unity3d程序开发
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
复制
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());
              
            }
        }
    }
}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-08-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档