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);
                    }
                }
            }
  • 相关阅读:
    HDU 2594 扩展kmp模板题
    HDU 1358 简单kmp
    HDU 3336 扩展kmp
    SPOJ SUBLEX 求第k小子串
    Codeforces 235C
    HDU 4622 Reincarnation
    HDU 4622 求解区间字符串中的不同子串的个数
    [LeetCode] Length of Last Word 字符串查找
    [LeetCode] Sudoku Solver 解数独,递归,回溯
    [LeetCode] Longest Common Prefix 字符串公有前序
  • 原文地址:https://www.cnblogs.com/zhangjiang/p/2869951.html
Copyright © 2011-2022 走看看