Ejemplo n.º 1
0
        /// <summary>
        /// 设置水印
        /// </summary>
        /// <param name="original"></param>
        /// <param name="watermark"></param>
        /// <returns></returns>
        public Image MakeWatermark(Image original, Watermark watermark)
        {
            //Bitmap b = new Bitmap(original.Width, original.Height, PixelFormat.Format24bppRgb);
            //b.SetResolution(original.HorizontalResolution, original.VerticalResolution);

            // 不加水印
            if (watermark.Ratio < 0)
            {
                return(original);
            }
            //当ratio大于1时,加的部分水印会在图片外面(不显示)
            Size markSize = new Size((int)Math.Round(watermark.Ratio * original.Width), (int)Math.Round(watermark.Ratio * original.Height));

            watermark.SetSize(markSize);

            switch (watermark.WatermarkType)
            {
            case WatermarkType.Text:
                //添加文字水印
                var textWm = watermark as TextWatermark;
                if (!string.IsNullOrEmpty(textWm.Text))
                {
                    textWm.HorizontalResolution = original.HorizontalResolution; // b.HorizontalResolution;
                    textWm.VerticalResolution   = original.VerticalResolution;   // b.VerticalResolution;
                    var            textMark = this.CreateTextWatermark(textWm);
                    ImageWatermark imageWm  = new ImageWatermark()
                    {
                        Alignment        = textWm.Alignment,
                        Alpha            = textWm.Alpha,
                        Image            = textMark,
                        MarginPercentage = textWm.MarginPercentage,
                        Ratio            = textWm.Ratio,
                    };
                    imageWm.SetSize(textWm.Size);
                    return(MakeImageWatermark(original, imageWm));
                }
                break;

            case WatermarkType.Image:
                return(MakeImageWatermark(original, watermark as ImageWatermark));

                break;

            default:
                break;
            }
            return(original);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="watermark"></param>
        /// <returns></returns>
        protected Image MakeImageWatermark(Image original, ImageWatermark watermark)
        {
            ImageAttributes imageAttributes = new ImageAttributes();
            ColorMap        colorMap        = new ColorMap();

            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] remapTable = { colorMap, };

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
            var alpha = watermark.Alpha;

            float[][] colorMatrixElements =
            {
                new float[] { 1.0f, 0.0f, 0.0f,  0.0f, 0.0f }, //red
                new float[] { 0.0f, 1.0f, 0.0f,  0.0f, 0.0f }, //green
                new float[] { 0.0f, 0.0f, 1.0f,  0.0f, 0.0f }, //blue
                new float[] { 0.0f, 0.0f, 0.0f, alpha, 0.0f }, //alpha
                new float[] { 0.0f, 0.0f, 0.0f,  0.0f, 1.0f }, //P
            };

            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            int xpos            = 0;
            int ypos            = 0;
            int markWidth       = 0;
            int markHeight      = 0;
            int estimatedWidth  = (int)Math.Round(original.Width * watermark.Ratio);
            int estimatedHeight = (int)Math.Round(original.Height * watermark.Ratio);

            //watermark.Image.Width > original.Width * watermark.Ratio ? (int)Math.Round(original.Width * watermark.Ratio) : watermark.Image.Width;
            // watermark.Image.Height > original.Height * watermark.Ratio ? (int)Math.Round(original.Height * watermark.Ratio) : watermark.Image.Height;
            if (watermark.Image.Width > estimatedWidth)
            {
                if (watermark.Image.Height > estimatedHeight)
                {
                    if (estimatedHeight > estimatedWidth)
                    {
                        markWidth  = estimatedWidth;
                        markHeight = watermark.Image.Height * estimatedWidth / watermark.Image.Width;
                        if (markHeight > estimatedHeight)
                        {
                            markWidth  = estimatedHeight * estimatedHeight / markHeight;
                            markHeight = estimatedHeight;
                        }
                    }
                    else
                    {
                        markWidth  = watermark.Image.Width * estimatedHeight / watermark.Image.Height;
                        markHeight = estimatedHeight;
                        if (markWidth > estimatedWidth)
                        {
                            markHeight = estimatedHeight * estimatedWidth / markWidth;
                            markWidth  = estimatedWidth;
                        }
                    }
                }
                else
                {
                    markWidth  = estimatedWidth;
                    markHeight = watermark.Image.Height * estimatedWidth / watermark.Image.Width;
                }
            }
            else
            {
                if (watermark.Image.Height > estimatedHeight)
                {
                    markWidth  = watermark.Image.Width * estimatedHeight / watermark.Image.Height;
                    markHeight = estimatedHeight;
                }
                else
                {
                    markWidth  = watermark.Image.Width;
                    markHeight = watermark.Image.Height;
                }
            }
            int marginX = (int)Math.Round(watermark.MarginPercentage / 100 * original.Width);
            int marginY = (int)Math.Round(watermark.MarginPercentage / 100 * original.Height);

            switch (watermark.Alignment)
            {
            case ContentAlignment.TopLeft:
                xpos = marginX;
                ypos = marginY;
                break;

            case ContentAlignment.TopCenter:
                xpos = (original.Width - markWidth) / 2;
                ypos = marginY;
                break;

            case ContentAlignment.TopRight:
                xpos = (original.Width - markWidth) - marginX;
                ypos = marginY;
                break;

            case ContentAlignment.MiddleLeft:
                xpos = marginX;
                ypos = (original.Height - markHeight) / 2;
                break;

            case ContentAlignment.MiddleCenter:
                xpos = (original.Width - markWidth) / 2;
                ypos = (original.Height - markHeight) / 2;
                break;

            case ContentAlignment.MiddleRight:
                xpos = (original.Width - markWidth) - marginX;
                ypos = (original.Height - markHeight) / 2;
                break;

            case ContentAlignment.BottomLeft:
                xpos = marginX;
                ypos = (original.Height - markHeight) - marginY;
                break;

            case ContentAlignment.BottomCenter:
                xpos = (original.Width - markWidth) / 2;
                ypos = (original.Height - markHeight) - marginY;
                break;

            case ContentAlignment.BottomRight:
                xpos = (original.Width - markWidth) - marginX;
                ypos = (original.Height - markHeight) - marginY;
                break;

            default:
                break;
            }

            Bitmap combined = new Bitmap(original);

            // gdi+图片
            Graphics g = Graphics.FromImage(combined);

            // AntiAlias    指定消除锯齿的呈现。
            // Default      指定不消除锯齿。
            // HighQuality  指定高质量、低速度呈现。
            // HighSpeed    指定高速度、低质量呈现。
            // Invalid      指定一个无效模式。
            // None         指定不消除锯齿。
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //g.InterpolationMode = InterpolationMode.High;
            //g.Clear(Color.White);
            //g.DrawImage(combined, new Rectangle(0, 0, combined.Width, combined.Height));
            g.DrawImage(watermark.Image, new Rectangle()
            {
                X = xpos, Y = ypos, Height = markHeight, Width = markWidth,
            }, 0, 0, watermark.Image.Width, watermark.Image.Height, GraphicsUnit.Pixel);
            imageAttributes.Dispose();
            g.Dispose();
            return(combined);
        }