public bool ToWaterMark() { Exception exception; Random random; string str3; int num10; int num11; FileHelper helper = new FileHelper(); string str = (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)) { this.message = "原图片文件格式不正确,支持的格式有[ " + this.AllowExt + " ]"; return false; } FileStream stream = File.OpenRead(HttpContext.Current.Server.MapPath("~/" + 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(this.Diaphaneity, 0xff, 0xff, 0xff)), new PointF(x, y), format); format.Dispose(); } } catch (Exception exception1) { exception = exception1; this.message = exception.Message; } finally { graphics.Dispose(); } if ((this.WaterMarkImagePath == null) || (this.WaterMarkImagePath.Trim() == string.Empty)) { random = new Random(); str3 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + random.Next().ToString() + sExt; string str4 = HttpContext.Current.Server.MapPath("~/" + str); image.Save(str4 + @"\" + str3); helper.DeleteFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); if (str.Substring(0, 1) == "/") { str = str.Substring(1, str.Length - 1); } this.filePath = str + "/" + str3; return true; } Image image2 = Image.FromFile(HttpContext.Current.Server.MapPath("~/" + this.WaterMarkImagePath)); int num8 = image2.Width; int num9 = image2.Height; if ((width < num8) || (height < (num9 * 2))) { random = new Random(); str3 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + random.Next().ToString() + sExt; image.Save(HttpContext.Current.Server.MapPath("~/" + str) + @"\" + str3); helper.DeleteFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); if (str.Substring(0, 1) == "/") { str = str.Substring(1, str.Length - 1); } this.filePath = str + "/" + str3; return true; } 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; numArray3 = new float[5]; numArray3[1] = 1f; newColorMatrix[1] = numArray3; numArray3 = new float[5]; numArray3[2] = 1f; newColorMatrix[2] = numArray3; numArray3 = new float[5]; numArray3[3] = this.ImageDeaphaneity; newColorMatrix[3] = numArray3; numArray3 = new float[5]; numArray3[4] = 1f; newColorMatrix[4] = numArray3; 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 { random = new Random(); str3 = "YXShop" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + random.Next().ToString() + sExt; bitmap2.Save(HttpContext.Current.Server.MapPath("~/" + str) + @"\" + str3); helper.DeleteFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); if (str.Substring(0, 1) == "/") { str = str.Substring(1, str.Length - 1); } this.filePath = str + "/" + str3; return true; } catch (Exception exception2) { exception = exception2; this.message = exception.Message; } finally { bitmap2.Dispose(); } return false; }
private void UpdateImageAttributes() { GdiHelpers.DisposeObject(ref imageAttribs); imageAttribs = GdiHelpers.GetImageAttributes(ImageLayoutMode.Fit, isPreview); imageAttribs.ClearRemapTable(); // As long as we cannot set/unset the color (available in next version), we map every color... //if (ReplaceColor != Color.Empty) { colorReplaceMap.OldColor = ReplaceColor; colorReplaceMap.NewColor = FillStyle.BaseColorStyle.Color; ColorMap[] colorMap = { colorReplaceMap }; imageAttribs.SetRemapTable(colorMap); //} }