Exemplo n.º 1
0
        /// <summary>
        /// Adds a watermark icon to an image and saves it to a new file.
        /// </summary>
        /// <param name="originalImageFilePath">Path to the image that will get a watermark.</param>
        /// <param name="watermarkFilePath">Path to the image with the watermark.</param>
        /// <param name="outputFilePath">Path where the watermarked image will be created.</param>
        /// <param name="position">The position of the watermark on the image.</param>
        public static void AddWatermarkIcon(string originalImageFilePath,
                                            string watermarkFilePath,
                                            string outputFilePath,
                                            PositionInImage position)
        {
            Image img       = Image.FromFile(originalImageFilePath);
            Image watermark = Image.FromFile(watermarkFilePath);

            int x = 0;
            int y = 0;

            switch (position)
            {
            case PositionInImage.TopLeft:
                x = 0;
                y = 0;
                break;

            case PositionInImage.TopRight:
                x = img.Width - watermark.Width;
                y = 0;
                break;

            case PositionInImage.BottomLeft:
                x = 0;
                y = img.Height - watermark.Height;
                break;

            case PositionInImage.BottomRight:
                x = img.Width - watermark.Width;
                y = img.Height - watermark.Height;
                break;
            }

            using (var g = Graphics.FromImage(img))
            {
                g.SmoothingMode     = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                g.DrawImage(watermark, x, y, watermark.Width, watermark.Height);
            }
            img.Save(outputFilePath);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a watermark text to the right bottom corner of an image.
        /// </summary>
        /// <param name="filePath">The filepath to the image.</param>
        /// <param name="value">The text to write as watermark.</param>
        /// <param name="position">The position of the text on the image.</param>
        public static void AddWatermarkString(string filePath,
                                              string value,
                                              string outputPath,
                                              PositionInImage position)
        {
            Image img = Image.FromFile(filePath);

            using (var g = Graphics.FromImage(img))
            {
                Font  f = new Font("Calibri", 15);
                Brush b = new SolidBrush(Color.Black);
                g.SmoothingMode     = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode   = PixelOffsetMode.HighQuality;

                int x = 15;
                int y = 15;

                switch (position)
                {
                case PositionInImage.TopLeft:
                    break;

                case PositionInImage.TopRight:
                    x = img.Width - (int)Math.Ceiling(g.MeasureString(value, f).Width);
                    break;

                case PositionInImage.BottomLeft:
                    y = img.Height - 25;
                    break;

                case PositionInImage.BottomRight:
                    x = img.Width - (int)Math.Ceiling(g.MeasureString(value, f).Width);
                    y = img.Height - 25;
                    break;
                }

                g.DrawString(value, f, b, x, y);
            }
            img.Save(outputPath);
        }