private void btnSelectGradient_Click(object sender, EventArgs e)
 {
     using (GradientMaker gradientForm = new GradientMaker(Config.WatermarkGradient) { Icon = Icon })
     {
         if (gradientForm.ShowDialog() == DialogResult.OK)
         {
             Config.WatermarkGradient = gradientForm.GradientData;
             UpdatePreview();
         }
     }
 }
 private void btnSelectGradient_Click(object sender, EventArgs e)
 {
     using (GradientMaker gradientForm = new GradientMaker(Config.WatermarkGradient)
     {
         Icon = Icon
     })
     {
         if (gradientForm.ShowDialog() == DialogResult.OK)
         {
             Config.WatermarkGradient = gradientForm.GradientData;
             UpdatePreview();
         }
     }
 }
Example #3
0
        private Image DrawWatermarkText(Image img, string drawText)
        {
            if (!string.IsNullOrEmpty(drawText) && Config.WatermarkFont.Size > 0)
            {
                Font  font            = null;
                Brush backgroundBrush = null;

                try
                {
                    int offset = Config.WatermarkOffset;

                    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);
                    }

                    Rectangle labelRectangle = new Rectangle(Point.Empty, labelSize);
                    Color     fontColor      = Config.WatermarkFontArgb;

                    using (Bitmap bmp = new Bitmap(labelRectangle.Width + 1, labelRectangle.Height + 1))
                        using (Graphics g = Graphics.FromImage(bmp))
                        {
                            g.SmoothingMode     = SmoothingMode.HighQuality;
                            g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                            if (Config.WatermarkUseCustomGradient)
                            {
                                backgroundBrush = GradientMaker.CreateGradientBrush(labelRectangle.Size, Config.WatermarkGradient);
                            }
                            else
                            {
                                backgroundBrush = new LinearGradientBrush(labelRectangle, Config.WatermarkGradient1Argb, Config.WatermarkGradient2Argb, Config.WatermarkGradientType);
                            }

                            using (GraphicsPath gPath = new GraphicsPath())
                                using (Pen borderPen = new Pen(Config.WatermarkBorderArgb))
                                {
                                    gPath.AddRoundedRectangle(labelRectangle, Config.WatermarkCornerRadius);
                                    g.FillPath(backgroundBrush, gPath);
                                    g.DrawPath(borderPen, gPath);
                                }

                            using (Brush textBrush = new SolidBrush(fontColor))
                                using (StringFormat sf = new StringFormat {
                                    Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center
                                })
                                {
                                    g.DrawString(drawText, font, textBrush, bmp.Width / 2f, bmp.Height / 2f, sf);
                                }

                            using (Graphics gImg = Graphics.FromImage(img))
                            {
                                gImg.SmoothingMode = SmoothingMode.HighQuality;
                                gImg.DrawImage(bmp, labelPosition.X, labelPosition.Y, bmp.Width, bmp.Height);

                                if (Config.WatermarkAddReflection)
                                {
                                    using (Bitmap bmp2 = ImageHelpers.AddReflection(bmp, 50, 150, 10))
                                    {
                                        gImg.DrawImage(bmp2, new Rectangle(labelPosition.X, labelPosition.Y + bmp.Height - 1, bmp2.Width, bmp2.Height));
                                    }
                                }
                            }
                        }
                }
                catch (Exception ex)
                {
                    DebugHelper.WriteException(ex, "Error while drawing watermark");
                }
                finally
                {
                    if (font != null)
                    {
                        font.Dispose();
                    }
                    if (backgroundBrush != null)
                    {
                        backgroundBrush.Dispose();
                    }
                }
            }

            return(img);
        }