public static DialogResult ShowFontDialog() { DialogResult result = DialogResult.Cancel; try { FontDialog fDialog = new FontDialog { ShowColor = true }; try { fDialog.Color = XMLSettings.DeserializeColor(Engine.conf.WatermarkFontColor); fDialog.Font = XMLSettings.DeserializeFont(Engine.conf.WatermarkFont); } catch (Exception err) { FileSystem.AppendDebug("Error while initializing Font and Color", err); } result = fDialog.ShowDialog(); if (result == DialogResult.OK) { Engine.conf.WatermarkFont = XMLSettings.SerializeFont(fDialog.Font); Engine.conf.WatermarkFontColor = XMLSettings.SerializeColor(fDialog.Color); } } catch (Exception ex) { FileSystem.AppendDebug("Error while setting Watermark Font", ex); } return(result); }
private static Image DrawWatermark(Image img, string drawText) { if (!string.IsNullOrEmpty(drawText)) { if (0 == Engine.conf.WatermarkFont.Size) { Adapter.ShowFontDialog(); } try { int offset = (int)Engine.conf.WatermarkOffset; Font font = XMLSettings.DeserializeFont(Engine.conf.WatermarkFont); Size textSize = TextRenderer.MeasureText(drawText, font); Size labelSize = new Size(textSize.Width + 10, textSize.Height + 10); Point labelPosition = FindPosition(Engine.conf.WatermarkPositionMode, offset, img.Size, new Size(textSize.Width + 10, textSize.Height + 10), 1); if (Engine.conf.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 = RoundedRectangle.Create(labelRectangle, (int)Engine.conf.WatermarkCornerRadius); int backTrans = (int)Engine.conf.WatermarkBackTrans; int fontTrans = (int)Engine.conf.WatermarkFontTrans; Color fontColor = XMLSettings.DeserializeColor(Engine.conf.WatermarkFontColor); 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 (Engine.conf.WatermarkUseCustomGradient) { brush = GradientMaker.CreateGradientBrush(labelRectangle.Size, Engine.conf.GradientMakerOptions.GetBrushDataActive()); } else { brush = new LinearGradientBrush(labelRectangle, Color.FromArgb(backTrans, XMLSettings.DeserializeColor(Engine.conf.WatermarkGradient1)), Color.FromArgb(backTrans, XMLSettings.DeserializeColor(Engine.conf.WatermarkGradient2)), Engine.conf.WatermarkGradientType); } g.FillPath(brush, gPath); g.DrawPath(new Pen(Color.FromArgb(backTrans, XMLSettings.DeserializeColor(Engine.conf.WatermarkBorderColor))), 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 (Engine.conf.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) { FileSystem.AppendDebug("Errow while drawing watermark", ex); } } return(img); }