zoukankan      html  css  js  c++  java
  • 给图片加自定义字体水印

    处理前:
    处理后:

    代码:
    加水印可以用DrawString 方法
               FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                Image image = Image.FromStream(fs);
                fs.Close();
                Bitmap b = new Bitmap(image);
                Graphics graphics = Graphics.FromImage(b);
                Font font = GetFont(72);
                Color cl = Color.FromArgb(0, 64, 128);
                SolidBrush myBrush = new SolidBrush(cl);
                //加名字
                graphics.DrawString(name, font, myBrush, 450, 225);
    

      GetFont方法载入自定义字体文件       

    private Font GetFont(int size)
    {
    System.Drawing.Font fn = this.Font;
    FontFamily ff = pfc.Families[0];
    
    if (ff.IsStyleAvailable(FontStyle.Regular))
    {
    fn = new Font(ff, size, FontStyle.Regular);
    }
    if (ff.IsStyleAvailable(FontStyle.Bold))
    {
    fn = new Font(ff, size, FontStyle.Bold);
    }
    if (ff.IsStyleAvailable(FontStyle.Italic))
    {
    fn = new Font(ff, size, FontStyle.Italic);
    
    }
    return fn;
    }  
    

    如果字体文件(TTF格式)没安装,那么需要在程序启动时候载入

     private void LoadFont()
            {
                Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication1.SaginawBold.ttf");
                byte[] fontdata = new byte[fontStream.Length];
                fontStream.Read(fontdata, 0, (int)fontStream.Length);
                fontStream.Close();
                unsafe
                {
                    fixed (byte* pFontData = fontdata)
                    {
                        pfc.AddMemoryFont((System.IntPtr)pFontData, fontdata.Length);
                    }
                }
            }
  • 相关阅读:
    Python 三级菜单
    linux 下按文件类型删除
    linux 做内网端口映射
    ss
    fio
    libXtst.so.6 is needed by teamviewer-12.0.76279-0.i686
    copy 浅复制 与深复制
    Git 使用方法
    关于 爬虫使用 urllib.urlopen 提交默认 User-Agent值
    Python 官方模块文档
  • 原文地址:https://www.cnblogs.com/zhangjiang/p/2869951.html
Copyright © 2011-2022 走看看