日期:2014-05-17  浏览次数:20659 次

给图片加水印,然后覆盖原来的图片,为什么会报错误呢,在线等啊!!
下面的代码是我上传图片类,保存的代码
C# code

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";
            HttpPostedFile file = context.Request.Files["Filedata"];
            string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
            if (file != null)
            {
                if (!System.IO.Directory.Exists(uploadPath))
                {
                    System.IO.Directory.CreateDirectory(uploadPath);
                }
                file.SaveAs(uploadPath + file.FileName);//这里是保存在服务器上,我想就在这句之后给图片加水印
                string webFilePath = uploadPath + file.FileName;
                string webFilePath_sy = uploadPath + file.FileName;

                AddWater(webFilePath, webFilePath_sy);
       
                context.Response.Write("1");
            }
            else
            {
                context.Response.Write("0");
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }



下面是我增加水印的代码
C# code

/// <summary>
        /// 在图片上增加文字水印
        /// </summary>
        /// <param name="Path">原服务器图片路径</param>
        /// <param name="Path_sy">生成的带文字水印的图片路径</param>
        public static void AddWater(string Path, string Path_sy)
        {
            string addText = "文字水印";

            System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            System.Drawing.Font f = new System.Drawing.Font("Verdana", 60);
            System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);

            g.DrawString(addText, f, b, 35, 35);
            g.Dispose();
            image.Save(Path_sy);
            image.Dispose();
        }



在上面上传的时候,在image.Save(Path_sy);这句报错,报错GDI一般性错误!
我想原因是保存的时候,覆盖原图片的问题,可能是原图片没有关闭,在线求解决办法啊!!

------解决方案--------------------
给你一段代码吧 懒的去找你的错误了?

C# code

/// <summary>
        /// 添加图片水印
        /// </summary>
        /// <param name="sourcePicture">源图片(绝对路径)</param>
        /// <param name="targetPicture">生成图片(绝对路径)</param>
        /// <param name="watermarkPicture">水印图片(绝对路径)</param>
        /// <param name="watermarkAlpha">水印图片透明度(0.1-1.0数值越小透明度越高)</param>
        /// <param name="wipeColor">水印图片中要去除的颜色</param>
        /// <param name="watermarkPosition">水印图片位置(从1-9,1:左上;2:中上;3:右上;4:左中;5:正中;6:右中;7:左下;8:中下;9:右下)</param>
        public static void MakeImgWatemark(string sourcePicture, string targetPicture, string watermarkPicture, float watermarkAlpha, string wipeColor, int watermarkPosition)
        {
            Color m_colorWipeColor = new Color();
            try
            {
                m_colorWipeColor = ColorTranslator.FromHtml(wipeColor);
            }
            catch
            {
                return;
            }

            if (sourcePicture == string.Empty || targetPicture == string.Empty || watermarkPicture == string.Empty || watermarkAlpha == 0.0 || watermarkPosition < 1 || watermarkPosition > 9)
                return;
            string m_sSourceExtension = Path.GetExtension(sourcePicture).ToLower();
            string m_sWatermarkExtension = Path.GetExtension(watermarkPicture).ToLower();

            if (File.Exists(sourcePicture) == false || File.Exists(watermarkPicture) =