Loading

C#基于两种需求向图片添加水印

使用场景

        1、也就是大家经常用的,一般是图片的4个角落,基于横纵坐标来添加。

        2、在图片内基于固定位置,文字始终居中。刚开始我基于第一种场景来根据水印汉字的长度来计算坐标,后来发现方法始终不可靠。现在是先在图片固定区域(水印区域)画一个矩形,然后再矩形内添加水印汉字,并使用画刷保持文字居中。

 

场景一效果

 

 

场景二效果

 

场景一添加水印方法

static void addWatermarkText(Graphics picture,int fontsize, string _watermarkText, string _watermarkPosition, int _width, int _height)
        {
            int[] sizes = new int[] {32, 14, 12, 10, 8, 6, 4 };
            Font crFont = null;
            SizeF crSize = new SizeF();

            crFont = new Font("微软雅黑", fontsize, FontStyle.Bold);
            crSize = picture.MeasureString(_watermarkText, crFont);

            float xpos = 0;
            float ypos = 0;
            Color color =Color.Firebrick;

            switch (_watermarkPosition)
            {
                case "WM_TOP_LEFT":
                    xpos = ((float)_width * (float).01) + (crSize.Width / 2);
                    ypos = (float)_height * (float).01;
                    break;
                case "WM_TOP_RIGHT":
                    xpos = ((float)_width * (float).99) - (crSize.Width / 2);
                    ypos = (float)_height * (float).01;
                    break;
                case "WM_BOTTOM_RIGHT":
                    xpos = ((float)_width * (float).99) - (crSize.Width / 2);
                    ypos = ((float)_height * (float).99) - crSize.Height;
                    break;
                case "WM_BOTTOM_LEFT":
                    xpos = ((float)_width * (float).01) + (crSize.Width / 2);
                    ypos = ((float)_height * (float).99) - crSize.Height;
                    break;
                
            }
            
            StringFormat StrFormat = new StringFormat();
            StrFormat.Alignment = StringAlignment.Center;
            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));//加阴影
            picture.DrawString(_watermarkText, crFont, semiTransBrush2, xpos + 1, ypos + 1, StrFormat);

            SolidBrush semiTransBrush = new SolidBrush(color);  //添加水印
            picture.DrawString(_watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat);

            semiTransBrush2.Dispose();
            semiTransBrush.Dispose();
        }

 

场景二添加水印方法

static void addWatermarkText(Graphics picture,string type, int fontsize, string _watermarkText)
        {
            //1、先画矩形
            RectangleF drawRect;
            Color color;
            if (type == "Top")
            {
                 drawRect = new RectangleF(73, 135, 450, 64);
                 color = Color.FromArgb(255, 255, 255);
            }
            else
            {
                 drawRect = new RectangleF(194, 245, 250, 39);
                 color = Color.FromArgb(244, 226, 38);
            }
            
            //2、在基于矩形画水印文字
            Font crFont = null;

            StringFormat StrFormat = new StringFormat();
            StrFormat.Alignment = StringAlignment.Center;

            crFont = new Font("微软雅黑", fontsize, FontStyle.Bold);
            SolidBrush semiTransBrush = new SolidBrush(color);  //添加水印
            picture.DrawString(_watermarkText, crFont, semiTransBrush, drawRect, StrFormat);

            semiTransBrush.Dispose();
        }

 

总结

       和第一种方法比起来,第二种方法更直观,更短小精悍,只需要在你需要添加水印的图片上计算好固定坐标然后先画一个矩形,然后把水印汉字画在矩形内,这样不管水印汉字如何变化都可以在图片固定位置居中。

posted @ 2015-07-14 13:29  歪头儿在北京  阅读(1159)  评论(0编辑  收藏  举报