欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

1.图片添加水印字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/// <summary>
        /// 文字水印
        /// </summary>
        /// <param name="imgPath">服务器图片相对路径</param>
        /// <param name="filename">保存文件名</param>
        /// <param name="watermarkText">水印文字</param>
        /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中  9=右下</param>
        /// <param name="quality">附加水印图片质量,0-100</param>
        /// <param name="fontsize">字体大小</param>
        /// <param name="fontname">字体</param>
        public void ImageAddWaterMarkStr(string imgPath, string filename, string watermarkText,
            int watermarkStatus, int quality, int fontsize, string fontname = "微软雅黑")
        {
            byte[] _ImageBytes = File.ReadAllBytes(imgPath);
            Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
 
            Graphics g = Graphics.FromImage(img);
            Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
            SizeF crSize;
            crSize = g.MeasureString(watermarkText, drawFont);
 
            float xpos = 0;
            float ypos = 0;
            switch (watermarkStatus)
            {
                case 1:
                    xpos = (float)img.Width * (float).01;
                    ypos = (float)img.Height * (float).01;
                    break;
                case 2:
                    xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                    ypos = (float)img.Height * (float).01;
                    break;
                case 3:
                    xpos = ((float)img.Width * (float).99) - crSize.Width;
                    ypos = (float)img.Height * (float).01;
                    break;
                case 4:
                    xpos = (float)img.Width * (float).01;
                    ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 5:
                    xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                    ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 6:
                    xpos = ((float)img.Width * (float).99) - crSize.Width;
                    ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 7:
                    xpos = (float)img.Width * (float).01;
                    ypos = ((float)img.Height * (float).99) - crSize.Height;
                    break;
                case 8:
                    xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                    ypos = ((float)img.Height * (float).99) - crSize.Height;
                    break;
                case 9:
                    xpos = ((float)img.Width * (float).99) - crSize.Width;
                    ypos = ((float)img.Height * (float).99) - crSize.Height;
                    break;
            }
 
            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);
 
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType.IndexOf("jpeg") > -1) ici = codec;
            }
            EncoderParameters encoderParams = new EncoderParameters();
            long[] qualityParam = new long[1];
            if (quality < 0 || quality > 100) quality = 80;
 
            qualityParam[0] = quality;
 
            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
            encoderParams.Param[0] = encoderParam;
 
            if (ici != null) img.Save(filename, ici, encoderParams);
            else img.Save(filename);
 
            g.Dispose();
            img.Dispose();
        }
 
//测试代码
private void 添加水印_Click(object sender, EventArgs e)
        {
            string img = Application.StartupPath + "\\app.ico";
            string waterImg = Application.StartupPath + "\\waterApp.ico";
             
            ImageAddWaterMarkStr(img, waterImg, "app", 2, 50, 10);
            this.pbWater.Image = Image.FromFile(waterImg);
        }

2.二进制流添加水印字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// <summary>
        /// 给二进制流添加水印文字并返回
        /// </summary>
        /// <param name="photo">二进制流</param>
        /// <param name="font">字体</param>
        /// <param name="fontSize">字体大小</param>
        /// <param name="wntType">水印位置</param>
        /// <param name="Text">水印文字</param>
        /// <returns></returns>
        public byte[] BytesAddWaterMarkStr(Byte[] photo, string font, int fontSize, string wntType, string Text)
        {
            MemoryStream stmBLOB = new MemoryStream(photo);
            Image pic = Image.FromStream(stmBLOB);
 
            Graphics grap = Graphics.FromImage(pic);
            Brush brush = new SolidBrush(Color.Blue);//创建一把刷子
            int xpos = 10;
            int ypos = 10;
            switch (wntType)
            {
                case "WMP_Left_Top":
                    xpos = 10;
                    ypos = 10;
                    break;
                case "WMP_Right_Top":
                    xpos = pic.Width - 10;
                    ypos = 10;
                    break;
                case "WMP_Right_Bottom":
                    xpos = pic.Width - 60;
                    ypos = pic.Height - 60;
                    break;
                case "WMP_Left_Bottom":
                    xpos = 10;
                    ypos = pic.Height - 10;
                    break;
                case "WM_ZJ":
                    xpos = pic.Width / 2;
                    ypos = pic.Height / 2;
                    break;
            }
            grap.DrawString(Text, new Font(font, fontSize), brush, xpos, ypos);//给图片(pic)加水印
 
            byte[] bt = null;
            using (MemoryStream mostream = new MemoryStream())
            {
                Bitmap bmp = new Bitmap(pic);
                bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Png);//将图像以指定的格式存入缓存内存流
                bt = new byte[mostream.Length];
                mostream.Position = 0;//设置留的初始位置
                mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
            }
            return bt;
        }
 
 
 
//给二进制流加水印文字
byte[] imgData =BytesAddWaterMarkStr(data.SignetName, "宋体", 3, "WMP_Right_Bottom", "水印字符串");

3.二进制流添加水印字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
   /// <summary>
        /// 文字水印
        /// </summary>
        /// <param name="photo">二进制流</param>
        /// <param name="watermarkText">水印文字</param>
        /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中  9=右下</param>
        /// <param name="quality">附加水印图片质量,0-100</param>
        /// <param name="fontsize">字体大小</param>
        /// <param name="fontname">字体</param>
        public byte[] BytesAddWaterMarkStr2(Byte[] photo, string watermarkText,
          int watermarkStatus, int quality, int fontsize, string fontname = "微软雅黑")
        {
            Image img = Image.FromStream(new System.IO.MemoryStream(photo));
            Graphics grap = Graphics.FromImage(img);
            Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
            SizeF crSize = grap.MeasureString(watermarkText, drawFont);
 
            float xpos = 0;
            float ypos = 0;
            switch (watermarkStatus)
            {
                case 1:
                    xpos = (float)img.Width * (float).01;
                    ypos = (float)img.Height * (float).01;
                    break;
                case 2:
                    xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                    ypos = (float)img.Height * (float).01;
                    break;
                case 3:
                    xpos = ((float)img.Width * (float).99) - crSize.Width;
                    ypos = (float)img.Height * (float).01;
                    break;
                case 4:
                    xpos = (float)img.Width * (float).01;
                    ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 5:
                    xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                    ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 6:
                    xpos = ((float)img.Width * (float).99) - crSize.Width;
                    ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 7:
                    xpos = (float)img.Width * (float).01;
                    ypos = ((float)img.Height * (float).99) - crSize.Height;
                    break;
                case 8:
                    xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                    ypos = ((float)img.Height * (float).99) - crSize.Height;
                    break;
                case 9:
                    xpos = ((float)img.Width * (float).99) - crSize.Width;
                    ypos = ((float)img.Height * (float).99) - crSize.Height;
                    break;
            }
 
            grap.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
            grap.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);
 
            //grap.Dispose();
            //         img.Dispose();
 
            byte[] bt = null;
            using (MemoryStream mostream = new MemoryStream())
            {
                Bitmap bmp = new Bitmap(img);
                bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Png);//将图像以指定的格式存入缓存内存流
                bt = new byte[mostream.Length];
                mostream.Position = 0;//设置留的初始位置
                mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
            }
            return bt;
        }
 
 
调用方式:
            byte[] bytes = ImageToByte(filePath);
            byte[] bytes2 = BytesAddWaterMarkStr2(bytes, "添加水印", 5, 50, 15);
            this.pbWater.Image = ByteArrayToImage(bytes2);

运行效果如下:

 

 注:如果需要添加的水印字符串 倾斜 等,设置如下——仅供参考

    Font drawFont = new Font(fontname, fontsize, FontStyle.Italic | FontStyle.Strikeout, GraphicsUnit.Pixel);

posted on   sunwugang  阅读(1193)  评论(0编辑  收藏  举报
编辑推荐:
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
阅读排行:
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 【译】我们最喜欢的2024年的 Visual Studio 新功能
· 如何打造一个高并发系统?
点击右上角即可分享
微信分享提示