zoukankan      html  css  js  c++  java
  • 在图片上加水印效果的字符

    客户要求在打印的时候背景上要有水印效果的文字,用来显示打印内容的发布时间等,程序如下:
    代码
        private string CreateWaterImg(string text)
        {
            
    string fileName = DateTime.Now.ToString("yyyyMMddhhmmss"+ ".jpg";
            
    // 已经存在的一个图片
            string exsitImg = Server.MapPath("."+ "/Empty.jpg";
            
    #region 文件夹(因为打印后无法及时删除,所以采用单双日文件夹删除前一天的方法)
            
    string path = Server.MapPath("."+ "/WaterImg";
            
    string path1 = Server.MapPath("."+ "/WaterImg";
            
    if ((DateTime.Now.DayOfYear % 2== 0)
            {
                path 
    += "D";
                path1 
    += "S";
            }
            
    else
            {
                path 
    += "S";
                path1 
    += "D";
            }
            DirectoryInfo curImgDir 
    = new DirectoryInfo(path);
            DirectoryInfo lastImgDir 
    = new DirectoryInfo(path1);

            
    if (!curImgDir.Exists)
            {
                curImgDir.Create();
            }
            
    if (lastImgDir.Exists)
            {
                lastImgDir.Delete(
    true);
            }
            
    #endregion
            
    //加文字水印
            System.Drawing.Image image = System.Drawing.Image.FromFile(exsitImg);

            Graphics g 
    = Graphics.FromImage(image);
            g.DrawImage(image, 
    00, image.Width, image.Height);
            Font f 
    = new Font("Verdana"20, FontStyle.Italic);
            
    // 后两个Color参数结合可以设置字体透明度
            Brush b = new HatchBrush(HatchStyle.Percent30, Color.FromArgb(100, Color.Gray), Color.FromArgb(100, Color.Gray));
            
    // 把当前图片上的内容清空
            g.Clear(Color.White);
            
    // 在图片上添加文字
            for (int i = 10; i < image.Width ; i += 400)
            {
                
    for (int j = 0; j < image.Height - 80; j += 80)
                {
                    // 这儿的处理时为了竖直对齐且为斜线
                    g.TranslateTransform(i, j);
                    g.RotateTransform(
    45);
                    g.DrawString(text, f, b, 
    00);
                    g.RotateTransform(
    -45);
                    g.ResetTransform();
                }
            }

            g.Dispose();
            
    // 保存
            string newPath = path + "/" + fileName;
            image.Save(newPath);
            image.Dispose();
            
    // 返回相对路径
            return newPath.Replace(Server.MapPath("."+ "/""");
        }

    效果如下:

  • 相关阅读:
    向cmd中添加字体的方法
    学生成绩管理系统C++
    立即抢注我的免费1T云空间
    js原型
    step3 . day8数据结构之算法
    代码练习中的bug及修改方法
    step3 . day7数据结构之二叉顺序数的创建和二叉树的栈形式遍历
    step3 . day6数据结构之非线性表 满二叉树和不完全二叉树
    step3 . day5 数据结构之线性表 栈和队的应用-球钟问题
    step3 . day4 数据结构之线性表 栈和队
  • 原文地址:https://www.cnblogs.com/bmy_light/p/1619217.html
Copyright © 2011-2022 走看看