在图片添加旋转的水印文字

来源:互联网 发布:js联动下拉菜单 编辑:程序博客网 时间:2024/05/14 20:27

1.方法代码

    /// <summary>
    /// 在图片添加旋转的水印文字
    /// </summary>
    /// <param name="PicturePath">文件路径</param>
    /// <param name="sourcePicture">源图片名称</param>
    /// <param name="fileName">目标图片名称</param>
    /// <param name="waterWords">水印文字内容</param>
    /// <param name="fontSize">水印文字字体大小</param>
    /// <param name="alpha">水印文字透明度</param>
    /// <param name="color">水印文字颜色</param>
    /// <param name="rotate">旋转角度</param>
    /// <returns>返回目标图片全路径</returns>
    public static string DrawWords(string PicturePath, string sourcePicture, string fileName, string waterWords, float fontSize, float alpha, Color color, float rotate)
    {
        #region 参数判断

        // 判断参数是否有效
        if (string.IsNullOrEmpty(sourcePicture) || string.IsNullOrEmpty(waterWords) || string.IsNullOrEmpty(PicturePath) || string.IsNullOrEmpty(fileName))
        {
            throw new ArgumentNullException("params is error");
        }

        #endregion

        #region 图片途径获取

        // 源图片全路径
        string sourcePictureName = PicturePath + sourcePicture;

        // 目标图片全路径
        string targetImage = PicturePath + fileName;

        #endregion

        #region 源图片复制到画板

        //创建一个图片对象用来装载要被添加水印的图片
        Image imgPhoto = Image.FromFile(sourcePictureName);

        //获取源图片的宽和高
        int phWidth = imgPhoto.Width;
        int phHeight = imgPhoto.Height;

        //建立一个bitmap,和我们需要加水印的图片一样大小
        Bitmap bmPhoto = new Bitmap(phWidth, phHeight);

        //SetResolution:设置此 Bitmap 的分辨率
        //这里直接将我们需要添加水印的图片的分辨率赋给了bitmap
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        //Graphics:封装一个 GDI+ 绘图图面。
        Graphics grPhoto = Graphics.FromImage(bmPhoto);

        //设置图形的品质
        grPhoto.SmoothingMode = SmoothingMode.HighQuality;

        //将我们要添加水印的图片按照原始大小描绘(复制)到图形中
        grPhoto.DrawImage(
         imgPhoto,                                           //   要添加水印的图片
         new Rectangle(0, 0, phWidth, phHeight), //  根据要添加的水印图片的宽和高
         0,                                                     //  X方向从0点开始描绘
         0,                                                     // Y方向
         phWidth,                                            //  X方向描绘长度
         phHeight,                                           //  Y方向描绘长度
         GraphicsUnit.Pixel);                              // 描绘的单位,这里用的是像素

        #endregion

        #region 计算文字所在矩形的宽高

        //字体
        Font crFont = null;
        //矩形的宽度和高度,SizeF有三个属性,分别为Height高,width宽,IsEmpty是否为空
        SizeF crSize = new SizeF();

        //直到它的长度比图片的宽度小
        crFont = new Font("arial", fontSize, FontStyle.Regular);
        //测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串。
        crSize = grPhoto.MeasureString(waterWords, crFont);

        //截边5%的距离,定义文字显示(由于不同的图片显示的高和宽不同,所以按百分比截取)
        int yPixlesFromBottom = (int)(phHeight * .05);

        //定义在图片上文字的大小
        float wmHeight = crSize.Height;
        float wmWidth = crSize.Width;

        //封装文本布局信息(如对齐、文字方向和 Tab 停靠位),显示操作(如省略号插入和国家标准 (National) 数字替换)和 OpenType 功能。
        StringFormat StrFormat = new StringFormat();

        //定义需要印的文字居中对齐
        StrFormat.Alignment = StringAlignment.Center;

        //SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。
        //这个画笔为描绘阴影的画笔,呈灰色
        int m_alpha = Convert.ToInt32(256 * alpha);

        //从四个 ARGB 分量(alpha、红色、绿色和蓝色)值创建 Color 结构,这里设置透明度为153
        //这个画笔为描绘正式文字的笔刷,呈白色
        SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(m_alpha, color.R, color.G, color.B));

        #endregion

        #region 写旋转的水印文字

        int nWCount = 1;
        int nHCount = 1;
        nWCount = Convert.ToInt32(phWidth / (wmWidth + 25)) < nWCount ? nWCount : Convert.ToInt32(phWidth / (wmWidth + 25));
        nHCount = Convert.ToInt32(phHeight / (wmHeight * 3 / 2)) < nHCount ? nHCount : Convert.ToInt32(phHeight / (wmHeight * 3 / 2));

        float xPosOfWm = wmWidth;
        float yPosOfWm = wmHeight * 3 / 2;

        //平移Graphics对象到窗体中心
        grPhoto.TranslateTransform(phWidth / 2, phHeight / 2);
        //设置Graphics对象的输出角度
        grPhoto.RotateTransform(rotate);

        for (int i = 0; i < nWCount; i++)
        {
            for (int j = 0; j < nHCount; j++)
            {
                //旋转显示文字
                grPhoto.TranslateTransform(i * xPosOfWm, j * yPosOfWm);
                grPhoto.DrawString(waterWords, crFont, semiTransBrush, new PointF(0, 0), StrFormat);
                grPhoto.TranslateTransform(-i * xPosOfWm, -j * yPosOfWm);

                //旋转显示文字
                grPhoto.TranslateTransform(i * xPosOfWm, -j * yPosOfWm);
                grPhoto.DrawString(waterWords, crFont, semiTransBrush, new PointF(0, 0), StrFormat);
                grPhoto.TranslateTransform(-i * xPosOfWm, j * yPosOfWm);

                //旋转显示文字
                grPhoto.TranslateTransform(-i * xPosOfWm, j * yPosOfWm);
                grPhoto.DrawString(waterWords, crFont, semiTransBrush, new PointF(0, 0), StrFormat);
                grPhoto.TranslateTransform(i * xPosOfWm, -j * yPosOfWm);

                //旋转显示文字
                grPhoto.TranslateTransform(-i * xPosOfWm, -j * yPosOfWm);
                grPhoto.DrawString(waterWords, crFont, semiTransBrush, new PointF(0, 0), StrFormat);
                grPhoto.TranslateTransform(i * xPosOfWm, j * yPosOfWm);
            }
        }

        //恢复全局变换矩阵
        grPhoto.ResetTransform();

        #endregion

        #region 保存文件

        //imgPhoto是我们建立的用来装载最终图形的Image对象
        //bmPhoto是我们用来制作图形的容器,为Bitmap对象
        imgPhoto = bmPhoto;
        //释放资源,将定义的Graphics实例grPhoto释放,grPhoto功德圆满
        grPhoto.Dispose();

        //将grPhoto保存
        imgPhoto.Save(targetImage, ImageFormat.Jpeg);
        imgPhoto.Dispose();

        return targetImage;

        #endregion
    }

 

2.调用

WaterImageManage.DrawWords(MapPath("/Images/"),"123.jpg", "123456.jpg", "张三 12345678", 50, 0.8f, Color.Red, -45);

0 0