asp.net 添加文字水印

来源:互联网 发布:手机遥控汽车软件 编辑:程序博客网 时间:2024/04/29 18:19
 /// <summary>
        /// 添加水印图片
        /// </summary>
        /// <param name="fileName">原始图片路径</param>
        /// <param name="path_down">保存图片路径</param>\
        ///   /// <param name="path_down">保存图片名称</param>
        private void AddTextToImg(string fileName,string imageSYurl,string imageSYname)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(fileName);
            Graphics g = Graphics.FromImage(image);
            float fontSize = 50;    //字体大小 
            float textWidth = 16 * fontSize;  //文本的长度 
            //下面定义一个矩形区域
            float rectX = 0;
            float rectY = 261;
            float rectWidth = 16 * (fontSize + 8);
            float rectHeight = fontSize + 52;
            //声明矩形
            RectangleF textArea = new RectangleF(181, 361, rectWidth, rectHeight);
            RectangleF textArea2 = new RectangleF(181, 500, rectWidth, rectHeight);
            Font font = new Font("华文彩云", fontSize);   //定义字体 
            Brush whiteBrush = new SolidBrush(Color.LightGray);   //暗黑
            Brush blackBrush = new SolidBrush(Color.Transparent);   //透明背景 
            g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);
            g.DrawString("莱芜市国土资源局", font, whiteBrush, textArea);
            if (!string.IsNullOrEmpty(userName))
            {
                g.DrawString(userName + System.DateTime.Now.ToString("yyyy-MM-dd"), font, whiteBrush, textArea2);
            }
            else
            {
                g.DrawString(System.DateTime.Now.ToString("yyyy-MM-dd"), font, whiteBrush, textArea2);
            }
            //MemoryStream ms = new MemoryStream();
            //保存为Jpg类型 
            //bitmap.Save(ms, ImageFormat.Jpeg);
            g.Dispose();
            //保存加水印过后的图片,删除原始图片
            image.Save(imageSYurl + @"\" + imageSYname.Split('.')[0] + "SY." + imageSYname.Split('.')[1]);
            image.Dispose();
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            image.Dispose();
        }
0 0
原创粉丝点击