public static void AddWaterMark(string originalImagePath) { string filename = ""; string extension = Path.GetExtension(originalImagePath); filename = originalImagePath.Replace(extension, "WaterMark" + extension); WaterMarkText waterMarkTextInfo = SiteConfig.WaterMarkConfig.WaterMarkTextInfo; WaterMarkImage waterMarkImageInfo = SiteConfig.WaterMarkConfig.WaterMarkImageInfo; int waterMarkType = SiteConfig.WaterMarkConfig.WaterMarkType; string str4 = VirtualPathUtility.AppendTrailingSlash(SiteConfig.SiteOption.UploadDir); string str5 = VirtualPathUtility.AppendTrailingSlash(HttpContext.Current.Server.MapPath("~/")); originalImagePath = str5 + str4 + originalImagePath; filename = str5 + str4 + filename; Image image = null; try { image = Image.FromFile(originalImagePath); } catch (FileNotFoundException) { CustomException.ThrowBllException("添加水印的源图片未找到。"); } Bitmap bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb); Graphics picture = Graphics.FromImage(bitmap); picture.Clear(Color.Green); picture.SmoothingMode = SmoothingMode.HighQuality; picture.InterpolationMode = InterpolationMode.High; picture.DrawImage(image, 0, 0, image.Width, image.Height); switch (waterMarkType) { case 0: AddWatermarkText(picture, waterMarkTextInfo, image.Width, image.Height); break; case 1: AddWatermarkImage(picture, waterMarkImageInfo, image.Width, image.Height); break; } try { bitmap.Save(filename, ImageFormat.Jpeg); } catch (ArgumentNullException) { CustomException.ThrowBllException("该图像的保存路径未找到。"); } finally { bitmap.Dispose(); image.Dispose(); picture.Dispose(); } FileSystemObject.Delete(originalImagePath, FsoMethod.File); string newFile = filename.Replace("WaterMark", ""); FileSystemObject.Move(filename, newFile, FsoMethod.File); }
public WaterMarkConfig() { if (this.waterMarkTextInfo == null) { this.waterMarkTextInfo = new WaterMarkText(); } if (this.waterMarkImageInfo == null) { this.waterMarkImageInfo = new WaterMarkImage(); } }
private static void AddWatermarkText(Graphics picture, WaterMarkText waterMarkTextInfo, int width, int height) { SizeF ef; string text = waterMarkTextInfo.Text; string waterMarkPosition = waterMarkTextInfo.WaterMarkPosition; int waterMarkPositionX = waterMarkTextInfo.WaterMarkPositionX; int waterMarkPositionY = waterMarkTextInfo.WaterMarkPositionY; int foneSize = waterMarkTextInfo.FoneSize; string foneType = waterMarkTextInfo.FoneType; string foneStyle = waterMarkTextInfo.FoneStyle; string foneColor = waterMarkTextInfo.FoneColor; int foneBorder = waterMarkTextInfo.FoneBorder; string foneBorderColor = waterMarkTextInfo.FoneBorderColor; Font font = null; string str7 = foneStyle; if (str7 != null) { if (!(str7 == "Bold")) { if (str7 == "Italic") { font = new Font(foneType, (float)foneSize, FontStyle.Italic); goto Label_00F9; } if (str7 == "Regular") { font = new Font(foneType, (float)foneSize, FontStyle.Regular); goto Label_00F9; } if (str7 == "Strikeout") { font = new Font(foneType, (float)foneSize, FontStyle.Strikeout); goto Label_00F9; } if (str7 == "Underline") { font = new Font(foneType, (float)foneSize, FontStyle.Underline); goto Label_00F9; } } else { font = new Font(foneType, (float)foneSize, FontStyle.Bold); goto Label_00F9; } } font = new Font(foneType, (float)foneSize, FontStyle.Regular); Label_00F9: ef = picture.MeasureString(text, font); float x = 0f; float y = 0f; string str8 = waterMarkPosition; if (str8 != null) { if (!(str8 == "WM_TOP_LEFT")) { if (str8 == "WM_TOP_RIGHT") { x = (width * 0.99f) - (ef.Width / 2f); y = height * 0.01f; } else if (str8 == "WM_BOTTOM_RIGHT") { x = (width * 0.99f) - (ef.Width / 2f); y = (height * 0.99f) - ef.Height; } else if (str8 == "WM_BOTTOM_LEFT") { x = (width * 0.01f) + (ef.Width / 2f); y = (height * 0.99f) - ef.Height; } else if (str8 == "WM_SetByManual") { x = waterMarkPositionX; y = waterMarkPositionY; } } else { x = (width * 0.01f) + (ef.Width / 2f); y = height * 0.01f; } } StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; ColorConverter converter = new ColorConverter(); Color color = (Color)converter.ConvertFromString(foneBorderColor); SolidBrush brush = new SolidBrush(color); picture.DrawString(text, font, brush, x + foneBorder, y + foneBorder, format); ColorConverter converter2 = new ColorConverter(); Color color2 = (Color)converter2.ConvertFromString(foneColor); SolidBrush brush2 = new SolidBrush(color2); try { picture.DrawString(text, font, brush2, x, y, format); } catch (ArgumentNullException) { CustomException.ThrowBllException("添加文字水印出现异常。"); } finally { brush.Dispose(); brush2.Dispose(); format.Dispose(); font.Dispose(); } }