给图片添加水印中碰到过的问题

    技术2025-12-19  9

    网站论坛可以上传图片,而给图片添加水印是比较普遍的做法。在实际使用方法的过程中,发现一个问题。

     

    添加水印相关代码如下:

     

    调用添加水印的方法比如测试用的一个button:

     

         /// <summary>        /// 添加水印的测试方法        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        protected void Button4_Click(object sender, EventArgs e)        {            string str = FCKRemarkInfo.Value.Trim();            Regex re = new Regex("/upload/20([^*]{0,50}).[jpg|bmp]{3}");   //这里是2种图片可以根据自己需求添加

                MatchCollection mat = re.Matches(str);            string st = "";            string url = "";            string path = AddDirectory();            foreach (Match ma in mat)            {                url = Server.MapPath(ma.Value);                if (url.Contains("jpg"))                {                    st = addshuiyin(url, path);                }                else                {                    st = addshuiyinBm(url, path);                }                str = str.Replace(ma.Value, st);   //替换图片的src            }            neirong = str;        }

     

            /// <summary>        /// 添加水印试验方法(JPG图片)        /// </summary>        /// <param name="filepath"></param>        string addshuiyin(string filepath, string path)        {            System.Drawing.Image image = System.Drawing.Image.FromFile(filepath);            string copypath = Server.MapPath("/manager/skin/default/watermark.gif");            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(copypath);            Graphics g = Graphics.FromImage(image);            Rectangle rect = new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height);            g.DrawImage(copyImage, rect, 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);            g.Dispose();            string filename = Guid.NewGuid().ToString() + ".jpg";            string serverPath = Server.MapPath(path);            image.Save(serverPath + filename);            image.Dispose();            return path + filename;        }

            /// <summary>        /// 添加水印试验方法(BMP图片)        /// </summary>        /// <param name="filepath"></param>        string addshuiyinBm(string filepath, string path)        {            System.Drawing.Image image = System.Drawing.Image.FromFile(filepath);            string copypath = Server.MapPath("/manager/skin/default/watermark.gif");            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(copypath);            Graphics g = Graphics.FromImage(image);            Rectangle rect = new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height);            g.DrawImage(copyImage, rect, 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);            g.Dispose();            string filename = Guid.NewGuid().ToString() + ".bmp";            string serverPath = Server.MapPath(path);            image.Save(serverPath + filename);            image.Dispose();            return path + filename;        }

            /// <summary>        /// 创建目录,如果没有就新建        /// </summary>        string AddDirectory()        {            string path = "/upload/watermark/" + DateTime.Now.Year + DateTime.Now.Month + "/";            string savepath = Server.MapPath(path);            if (!Directory.Exists(savepath))                Directory.CreateDirectory(savepath);            return path;        }

     

    这里针对2种不同类型的图片写了2个重复的方法,在实际运用时,如果把文件的后缀名作为参数带入,那么当需要添加水印的图片超过1张时就会报未能映射路径的错误,如果只有1张图片,那么不管是什么类型都可以顺利添加水印且不会报错。至于其中的原因,本人确实水平较低,不是很清楚。

    最新回复(0)