/// <summary> /// 添加水印 /// </summary> public void ToWaterMark() { string path = (this.SaveWaterMarkImagePath == null) ? this.SourceImagePath : this.SaveWaterMarkImagePath; string sExt = this.SourceImagePath.Substring(this.SourceImagePath.LastIndexOf(".")).ToLower(); if (this.SourceImagePath.ToString() == string.Empty) { throw new NullReferenceException("SourceImagePath is null!"); } if (!this.CheckValidExt(sExt)) { throw new ArgumentException("原图片文件格式不正确,支持的格式有[ " + this.AllowExt + " ]", "SourceImagePath"); } FileStream stream = File.OpenRead(this.SourceImagePath); Image original = Image.FromStream(stream, true); stream.Close(); int width = original.Width; int height = original.Height; float horizontalResolution = original.HorizontalResolution; float verticalResolution = original.VerticalResolution; Bitmap image = new Bitmap(original, width, height); original.Dispose(); image.SetResolution(72f, 72f); Graphics graphics = Graphics.FromImage(image); try { if ((this.WaterMarkText != null) && (this.WaterMarkText.Trim().Length > 0)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel); int[] numArray = new int[] { 0x10, 14, 12, 10, 8, 6, 4 }; Font font = null; SizeF ef = new SizeF(0f, 0f); for (int i = 0; i < numArray.Length; i++) { font = new Font("arial", (float)numArray[i], FontStyle.Bold); ef = graphics.MeasureString(this.WaterMarkText, font); if (((ushort)ef.Width) < ((ushort)width)) { break; } } numArray = null; float y = (height - ((int)(height * 0.05000000074505806))) - (ef.Height / 2f); float x = width / 2; StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; graphics.DrawString(this.WaterMarkText, font, new SolidBrush(Color.FromArgb(0x99, 0, 0, 0)), new PointF(x + 1f, y + 1f), format); graphics.DrawString(this.WaterMarkText, font, new SolidBrush(Color.FromArgb(0x99, 0xff, 0xff, 0xff)), new PointF(x, y), format); format.Dispose(); } } catch (Exception exception) { throw exception; } finally { graphics.Dispose(); } if (this.WaterMarkImagePath == null) { this.SaveImage(image, path, GetCodecInfo(htmimes[sExt])); } else if (this.WaterMarkImagePath.Trim() == string.Empty) { this.SaveImage(image, path, GetCodecInfo(htmimes[sExt])); } else { Image image2 = new Bitmap(this.WaterMarkImagePath); int num8 = image2.Width; int num9 = image2.Height; if ((width < num8) || (height < (num9 * 2))) { this.SaveImage(image, this.SourceImagePath, GetCodecInfo(htmimes[sExt])); } else { int num10; int num11; Bitmap bitmap2 = new Bitmap(image); image.Dispose(); bitmap2.SetResolution(horizontalResolution, verticalResolution); Graphics graphics2 = Graphics.FromImage(bitmap2); ImageAttributes imageAttr = new ImageAttributes(); ColorMap map = new ColorMap(); map.OldColor = Color.FromArgb(0xff, 0, 0xff, 0); map.NewColor = Color.FromArgb(0, 0, 0, 0); imageAttr.SetRemapTable(new ColorMap[] { map }, ColorAdjustType.Bitmap); float[][] newColorMatrix = new float[5][]; float[] numArray3 = new float[5]; numArray3[0] = 1f; newColorMatrix[0] = numArray3; float[] numArray4 = new float[5]; numArray4[1] = 1f; newColorMatrix[1] = numArray4; float[] numArray5 = new float[5]; numArray5[2] = 1f; newColorMatrix[2] = numArray5; float[] numArray6 = new float[5]; numArray6[3] = 0.3f; newColorMatrix[3] = numArray6; float[] numArray7 = new float[5]; numArray7[4] = 1f; newColorMatrix[4] = numArray7; imageAttr.SetColorMatrix(new ColorMatrix(newColorMatrix), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); this.GetWaterMarkXY(out num10, out num11, width, height, num8, num9); graphics2.DrawImage(image2, new Rectangle(num10, num11, num8, num9), 0, 0, num8, num9, GraphicsUnit.Pixel, imageAttr); imageAttr.ClearColorMatrix(); imageAttr.ClearRemapTable(); image2.Dispose(); graphics2.Dispose(); try { this.SaveImage(bitmap2, path, GetCodecInfo(htmimes[sExt])); } catch (Exception exception2) { throw exception2; } finally { bitmap2.Dispose(); } } } }