//绘制文本水印 private static void DrawTxtWaterMark(ModelUploadFileConfig objConfig) { if (!File.Exists(objConfig.ServerFileFullPath)) { objConfig.OPFlag = false; objConfig.OPMessage = "文件不存在,加水印失败。"; return; } Image objImg = Image.FromFile(objConfig.ServerFileFullPath); switch (objImg.PixelFormat) { case PixelFormat.Format1bppIndexed: case PixelFormat.Format4bppIndexed: case PixelFormat.Format8bppIndexed: case PixelFormat.Undefined: case PixelFormat.Format16bppArgb1555: case PixelFormat.Format16bppGrayScale: objConfig.OPFlag = false; objConfig.OPMessage = "上传的图片包含不正确的像素格式,不能加水印!"; objImg.Dispose(); return; default: break; } Graphics g = null; Font f = new Font("Verdana", 12f, FontStyle.Bold); Brush b = new SolidBrush(Color.Red); try { g = Graphics.FromImage(objImg); g.DrawImage(objImg, 0, 0, objImg.Width, objImg.Height); g.DrawString(objConfig.WaterMarkTxtString, f, b, Convert.ToSingle(objConfig.WaterMarkMarginRight), Convert.ToSingle(objConfig.WaterMarkMarginBottom)); objConfig.ServerFileHasWMFullPath = RenameFile(objConfig.ServerFileFullPath); objImg.Save(objConfig.ServerFileHasWMFullPath); objConfig.OPFlag = true; } catch (Exception ex) { throw ex; } finally { f.Dispose(); b.Dispose(); g.Dispose(); objImg.Dispose(); HelperFile.DeleteFile(objConfig.ServerFileHasWMFullPath); } }
//绘制图片水印 private static void DrawPicWaterMark(ModelUploadFileConfig objConfig) { if (!File.Exists(objConfig.ServerFileFullPath)) { objConfig.OPFlag = false; objConfig.OPMessage = "图片文件不存在,加水印失败。"; return; } string strRootPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath; objConfig.WaterMarkImagePath = strRootPath + objConfig.WaterMarkImagePath; if (!File.Exists(objConfig.WaterMarkImagePath)) { objConfig.OPFlag = false; objConfig.OPMessage = "水印图片文件不存在,加水印失败。"; return; } //将服务器端要加水印的图像文件载入到Image对象里 Image objImg = Image.FromFile(objConfig.ServerFileFullPath); switch (objImg.PixelFormat) { case PixelFormat.Format1bppIndexed: case PixelFormat.Format4bppIndexed: case PixelFormat.Format8bppIndexed: case PixelFormat.Undefined: case PixelFormat.Format16bppArgb1555: case PixelFormat.Format16bppGrayScale: objConfig.OPFlag = false; objConfig.OPMessage = "上传的图片包含不正确的像素格式,不能加水印。"; objImg.Dispose(); return; } //将水印图像文件载入到另一个Image对象里 Image objWMImg = Image.FromFile(objConfig.WaterMarkImagePath); // 将保存了图像文件的Image对象存到一个Graphics对象里 Graphics g = Graphics.FromImage(objImg); MemoryStream objMS = new MemoryStream(); ImageAttributes imgAttr = new ImageAttributes(); FileStream objFS = null; try { //获取要绘制图形坐标 int x = objImg.Width - objConfig.WaterMarkMarginRight; int y = objImg.Height - objConfig.WaterMarkMarginBottom; //设置颜色矩阵 float[][] matrixItems = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, Convert.ToSingle(objConfig.WaterMarkTransparent) / 100f, 0 }, new float[] { 0, 0, 0, 0, 1 } }; ColorMatrix colorMatrix = new ColorMatrix(matrixItems); imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.SmoothingMode = SmoothingMode.AntiAlias; //在Graphics对象里绘制水印图像,此时,和这个Graphics对象相关联的服务器文件也跟着被画上了水印 g.DrawImage(objWMImg, new Rectangle(x, y, objWMImg.Width, objWMImg.Height), 0, 0, objWMImg.Width, objWMImg.Height, GraphicsUnit.Pixel, imgAttr); //读取原来服务器端要写入水印的文件的文件类型 FileInfo objFile = new FileInfo(objConfig.ServerFileFullPath); ImageFormat imageType = default(ImageFormat); switch (objFile.Extension.ToLower()) { case ".jpg": imageType = ImageFormat.Jpeg; break; case ".jpeg": imageType = ImageFormat.Jpeg; break; case ".gif": imageType = ImageFormat.Gif; break; case ".png": imageType = ImageFormat.Png; break; case ".bmp": imageType = ImageFormat.Bmp; break; case ".tif": imageType = ImageFormat.Tiff; break; case ".wmf": imageType = ImageFormat.Wmf; break; case ".ico": imageType = ImageFormat.Icon; break; default: imageType = ImageFormat.Jpeg; break; } // 将已经画好水印的Image对象保存到内存流中 objImg.Save(objMS, imageType); // 将内存流以数组形式保存到图像数组里 byte[] imgData = objMS.ToArray(); //将写了水印的图像数组存入改了名的文件 objFS = new FileStream(RenameFile(objConfig.ServerFileFullPath), FileMode.Create, FileAccess.Write); if ((objFS != null)) { objFS.Write(imgData, 0, imgData.Length); } objConfig.OPFlag = true; objConfig.ServerFileHasWMFullPath = RenameFile(objConfig.ServerFileFullPath); } catch (Exception ex) { throw ex; } finally { objFS.Close(); objFS.Dispose(); objFS = null; objImg.Dispose(); objImg = null; objWMImg.Dispose(); objWMImg = null; g.Dispose(); g = null; objMS.Close(); objMS.Dispose(); objMS = null; imgAttr.Dispose(); imgAttr = null; HelperFile.DeleteFile(objConfig.ServerFileHasWMFullPath); } }