Ejemplo n.º 1
0
        private Image DrawWatermarkImage(Image img, string imgPath)
        {
            Image img2 = null;

            try
            {
                if (img != null && !string.IsNullOrEmpty(imgPath) && File.Exists(imgPath) && Config.WatermarkImageScale > 0)
                {
                    img2 = Helpers.GetImageFromFile(imgPath);

                    int offset = Config.WatermarkOffset;
                    int width  = (int)(Config.WatermarkImageScale / 100f * img2.Width);
                    int height = (int)(Config.WatermarkImageScale / 100f * img2.Height);

                    if (Config.WatermarkAutoHide && ((img.Width < width + offset) || (img.Height < height + offset)))
                    {
                        return(img);
                    }

                    if (Config.WatermarkImageScale != 100)
                    {
                        img2 = ImageHelpers.ResizeImage(img2, width, height);
                    }

                    Point imgPos = FindPosition(Config.WatermarkPositionMode, offset, img.Size, img2.Size, 0);

                    using (Graphics g = Graphics.FromImage(img))
                    {
                        g.SetHighQuality();
                        g.DrawImage(img2, imgPos.X, imgPos.Y, img2.Width, img2.Height);

                        if (Config.WatermarkAddReflection)
                        {
                            using (Bitmap bmp = ImageHelpers.AddReflection(img2, 50, 150, 10))
                            {
                                g.DrawImage(bmp, new Rectangle(imgPos.X, imgPos.Y + img2.Height - 1, bmp.Width, bmp.Height));
                            }
                        }

                        if (Config.WatermarkUseBorder)
                        {
                            g.DrawRectangle(Pens.Black, new Rectangle(imgPos.X, imgPos.Y, img2.Width - 1, img2.Height - 1));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DebugHelper.WriteException(ex, "Error while drawing image watermark");
            }
            finally
            {
                if (img2 != null)
                {
                    img2.Dispose();
                }
            }

            return(img);
        }
Ejemplo n.º 2
0
        public static Image DrawReflection(Image img, int percentage, int maxAlpha, int minAlpha, int offset, bool skew, int skewSize)
        {
            Bitmap reflection = ImageHelpers.AddReflection(img, percentage, maxAlpha, minAlpha);

            if (skew)
            {
                reflection = ImageHelpers.AddSkew(reflection, skewSize, 0);
            }

            Bitmap result = new Bitmap(reflection.Width, img.Height + reflection.Height + offset);

            using (Graphics g = Graphics.FromImage(result))
                using (img)
                    using (reflection)
                    {
                        g.SetHighQuality();
                        g.DrawImage(img, 0, 0, img.Width, img.Height);
                        g.DrawImage(reflection, 0, img.Height + offset, reflection.Width, reflection.Height);
                    }

            return(result);
        }
Ejemplo n.º 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);
        }