您的位置:首页 > 编程语言 > ASP

asp.net 添加文字水印

2017-02-16 17:27 337 查看
 /// <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();

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: