首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

给图片加水印,然后覆盖原来的图片,为什么会报异常呢,

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

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

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) == false || (m_sSourceExtension != ".gif" && m_sSourceExtension != ".jpg" && m_sSourceExtension != ".png" && m_sSourceExtension != ".bmp") || (m_sWatermarkExtension != ".gif" && m_sWatermarkExtension != ".jpg" && m_sWatermarkExtension != ".png" && m_sWatermarkExtension != ".bmp"))                return;            System.Drawing.Image m_imgSource = System.Drawing.Image.FromFile(sourcePicture);            int m_iSourceWidth = m_imgSource.Width;            int m_iSourceHeight = m_imgSource.Height;            float m_fHorizontalResolution = m_imgSource.HorizontalResolution;            float m_fVerticalResolution = m_imgSource.VerticalResolution;            Bitmap m_bmSource = new Bitmap(m_imgSource, m_iSourceWidth, m_iSourceHeight);            m_imgSource.Dispose();            m_bmSource.SetResolution(m_fHorizontalResolution, m_fVerticalResolution);            m_imgSource.Dispose();            Graphics m_grSource = Graphics.FromImage(m_bmSource);            m_grSource.SmoothingMode = SmoothingMode.AntiAlias;            m_grSource.DrawImage(m_bmSource, new Rectangle(0, 0, m_iSourceWidth, m_iSourceHeight), 0, 0, m_iSourceWidth, m_iSourceHeight, GraphicsUnit.Pixel);            System.Drawing.Image m_imgWatermark = new Bitmap(watermarkPicture);            int m_iWatermarkWidth = m_imgWatermark.Width;            int m_iWatermarkHeight = m_imgWatermark.Height;            Bitmap m_bmWatermark = new Bitmap(m_bmSource);            m_bmWatermark.SetResolution(m_fHorizontalResolution, m_fVerticalResolution);            Graphics m_grWatermark = Graphics.FromImage(m_bmWatermark);            ImageAttributes m_imgAttributes = new ImageAttributes();            ColorMap colorMap = new ColorMap();            colorMap.OldColor = Color.FromArgb(255, m_colorWipeColor);            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);            ColorMap[] remapTable = { colorMap };            m_imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);            float[][] colorMatrixElements = {            new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f}, // red红色           new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f}, //green绿色           new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f}, //blue蓝色                  new float[] {0.0f,  0.0f,  0.0f,  watermarkAlpha, 0.0f}, //透明度                new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};//            ColorMatrix m_wmColorMatrix = new ColorMatrix(colorMatrixElements);            m_imgAttributes.SetColorMatrix(m_wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);            int m_iWatermarkX;            int m_iWatermarkY;            switch (watermarkPosition)            {                case 1:                    m_iWatermarkX = 10;                    m_iWatermarkY = 10;                    break;                case 2:                    m_iWatermarkX = (m_iSourceWidth - m_iWatermarkWidth) / 2;                    m_iWatermarkY = 10;                    break;                case 3:                    m_iWatermarkX = m_iSourceWidth - m_iWatermarkWidth - 10;                    m_iWatermarkY = 10;                    break;                case 4:                    m_iWatermarkX = 10;                    m_iWatermarkY = (m_iSourceHeight - m_iWatermarkHeight) / 2;                    break;                case 5:                    m_iWatermarkX = (m_iSourceWidth - m_iWatermarkWidth) / 2;                    m_iWatermarkY = (m_iSourceHeight - m_iWatermarkHeight) / 2;                    break;                case 6:                    m_iWatermarkX = m_iSourceWidth - m_iWatermarkWidth - 10;                    m_iWatermarkY = (m_iSourceHeight - m_iWatermarkHeight) / 2;                    break;                case 7:                    m_iWatermarkX = 10;                    m_iWatermarkY = m_iSourceHeight - m_iWatermarkHeight - 10;                    break;                case 8:                    m_iWatermarkX = (m_iSourceWidth - m_iWatermarkWidth) / 2;                    m_iWatermarkY = m_iSourceHeight - m_iWatermarkHeight - 10;                    break;                case 9:                    m_iWatermarkX = m_iSourceWidth - m_iWatermarkWidth - 10;                    m_iWatermarkY = m_iSourceHeight - m_iWatermarkHeight - 10;                    break;                default:                    m_iWatermarkX = m_iSourceWidth - m_iWatermarkWidth - 10;                    m_iWatermarkY = m_iSourceHeight - m_iWatermarkHeight - 10;                    break;            }            m_grWatermark.DrawImage(m_imgWatermark, new Rectangle(m_iWatermarkX, m_iWatermarkY, m_iWatermarkWidth, m_iWatermarkHeight), 0, 0, m_iWatermarkWidth, m_iWatermarkHeight, GraphicsUnit.Pixel, m_imgAttributes);            m_bmSource = m_bmWatermark;            m_grSource.Dispose();            m_grWatermark.Dispose();            ImageFormat m_imgformatTarget = ImageFormat.Jpeg;            switch (Right(targetPicture, 4).ToLower())            {                case ".gif":                    m_imgformatTarget = ImageFormat.Gif; break;                case ".jpg":                    m_imgformatTarget = ImageFormat.Jpeg; break;                case ".png":                    m_imgformatTarget = ImageFormat.Png; break;                case ".bmp":                    m_imgformatTarget = ImageFormat.Bmp; break;                default:                    m_imgformatTarget = ImageFormat.Jpeg; break;            }            m_bmSource.Save(targetPicture, m_imgformatTarget);            m_bmSource.Dispose();            m_imgWatermark.Dispose();            m_bmWatermark.Dispose();            m_grWatermark.Dispose();            m_grSource.Dispose();            m_imgAttributes.Dispose();        } 


[解决办法]
老问题了

C# code
System.Drawing.Image image = System.Drawing.Image.FromFile(Path); 

热点排行