private void btnWatermarkFont_Click(object sender, EventArgs e) { DialogResult result = ZScreenCoreHelper.ShowFontDialog(Config); if (result == DialogResult.OK) { pbWatermarkFontColor.BackColor = Config.WatermarkFontArgb; lblWatermarkFont.Text = FontToString(); TestWatermark(); } }
private Image DrawWatermark(Image img, string drawText) { if (!string.IsNullOrEmpty(drawText)) { if (0 == Config.WatermarkFont.Size) { ZScreenCoreHelper.ShowFontDialog(Config); } try { int offset = (int)Config.WatermarkOffset; Font font = Config.WatermarkFont; Size textSize = TextRenderer.MeasureText(drawText, font); Size labelSize = new Size(textSize.Width + 10, textSize.Height + 10); Point labelPosition = FindPosition(Config.WatermarkPositionMode, offset, img.Size, new Size(textSize.Width + 10, textSize.Height + 10), 1); if (Config.WatermarkAutoHide && ((img.Width < labelSize.Width + offset) || (img.Height < labelSize.Height + offset))) { return(img); //throw new Exception("Image size smaller than watermark size."); } Rectangle labelRectangle = new Rectangle(Point.Empty, labelSize); GraphicsPath gPath = GraphicsEx.GetRoundedRectangle(labelRectangle, (int)Config.WatermarkCornerRadius); int backTrans = (int)Config.WatermarkBackTrans; int fontTrans = (int)Config.WatermarkFontTrans; Color fontColor = Config.WatermarkFontArgb; Bitmap bmp = new Bitmap(labelRectangle.Width + 1, labelRectangle.Height + 1); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.HighQuality; g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; LinearGradientBrush brush; if (Config.WatermarkUseCustomGradient) { brush = GradientMaker.CreateGradientBrush(labelRectangle.Size, Config.GradientMakerOptions.GetBrushDataActive()); } else { brush = new LinearGradientBrush(labelRectangle, Color.FromArgb(backTrans, Config.WatermarkGradient1Argb), Color.FromArgb(backTrans, Config.WatermarkGradient2Argb), Config.WatermarkGradientType); } g.FillPath(brush, gPath); g.DrawPath(new Pen(Color.FromArgb(backTrans, Config.WatermarkBorderArgb)), gPath); StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; g.DrawString(drawText, font, new SolidBrush(Color.FromArgb(fontTrans, fontColor)), bmp.Width / 2, bmp.Height / 2, sf); Graphics gImg = Graphics.FromImage(img); gImg.SmoothingMode = SmoothingMode.HighQuality; gImg.DrawImage(bmp, labelPosition); if (Config.WatermarkAddReflection) { Bitmap bmp2 = AddReflection(bmp, 50, 200); gImg.DrawImage(bmp2, new Rectangle(labelPosition.X, labelPosition.Y + bmp.Height - 1, bmp2.Width, bmp2.Height)); } } catch (Exception ex) { DebugHelper.WriteException(ex, "Errow while drawing watermark"); } } return(img); }