asp.net 添加文字水印

这个方法展示了如何在ASP.NET中为图片添加文字水印。代码创建了一个Graphics对象,使用特定字体和颜色在图片上绘制文字,并提供保存加水印图片的功能。水印内容包括固定文字和可选的用户名及日期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /// <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();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值