c# 图文添加文字斜水印 优化

之前一篇给图片加水印的功能,加出来水印的图片位置有一点问题,并且如果图片分辨率有变动的话,水印会有层次不齐的问题。

目前只能优化到增加一条居中显示的斜水印,在不同分辨率不同大小的图片中,都能保证文字水印的字体大小从左下至右上能撑满整张图片。

 

思路是:先生成一张文字水印图片的PNG图片。

 

在你需要添加水印的图片上,把之前加的水印图片贴上去就可以了。

 

核心代码:

 1     //新建原始普通大小的bmp
 2     Bitmap bmCanvas = new Bitmap(imgSrc.Width, imgSrc.Height, PixelFormat.Format24bppRgb);
 3     Graphics gCanvas = Graphics.FromImage(bmCanvas);
 4     gCanvas.Clear(Color.White);
 5     gCanvas.SmoothingMode = SmoothingMode.HighQuality;
 6     gCanvas.InterpolationMode = InterpolationMode.High;
 7     //将原始图片加载入画布
 8     gCanvas.DrawImage(imgSrc, 0, 0, imgSrc.Width, imgSrc.Height);
 9     //计算图片对角线长度
10     double diagonal = Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2));
11     
12     //计算对角线倾角
13     double angle = Math.Asin(height / Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2))) / Math.PI * 180;
14     
15     // 确定水印文字的字体大小
16     int[] sizes = new int[]
17     {
18         280, 276, 272, 268, 264, 260, 256, 252, 248, 244, 240, 236, 232, 228, 224, 220, 216, 212, 208,
19         204, 200, 196, 192, 188, 184, 180, 176, 172, 168, 164, 160, 156, 152, 148, 144, 140, 136, 132,
20         128, 124, 120, 116, 112, 108, 104, 100, 96, 92, 88, 84, 80, 76, 72, 68, 64, 60, 56, 52, 48, 44,
21         40, 36, 32, 28, 24, 20, 16, 12, 8, 4
22     };
23     Font crFont = null;
24     SizeF crSize = new SizeF();
25     
26     for (int i = 0; i < sizes.Length; i++)
27     {
28         crFont = new Font("微软雅黑", sizes[i], FontStyle.Bold);
29         crSize = gCanvas.MeasureString(watermarkText, crFont);
30         if ((int)crSize.Width < (int)diagonal * 0.9)
31         {
32             break;
33         }
34     }
35     // 生成水印图片(将文字写到图片中)
36     //Bitmap bmWaterMark = new Bitmap((int)crSize.Width + 3, (int)crSize.Height + 3, PixelFormat.Format32bppArgb);
37     Bitmap bmWaterMark = new Bitmap(width, height, PixelFormat.Format32bppArgb);
38     Graphics gWaterMark = Graphics.FromImage(bmWaterMark);
39     
40     gWaterMark.TranslateTransform(width / 2, height / 2);
41     gWaterMark.RotateTransform(-(int)angle);
42     gWaterMark.TranslateTransform(-crSize.Width / 2, -crSize.Height / 2);
43     
44     PointF pt = new PointF(0, 0);
45     // 画阴影文字
46     Brush transparentBrush0 = new SolidBrush(Color.FromArgb(255, Color.Black));
47     Brush transparentBrush1 = new SolidBrush(Color.FromArgb(255, Color.Black));
48     gWaterMark.DrawString(watermarkText, crFont, transparentBrush0, pt.X, pt.Y + 1);
49     gWaterMark.DrawString(watermarkText, crFont, transparentBrush0, pt.X + 1, pt.Y);
50     gWaterMark.DrawString(watermarkText, crFont, transparentBrush1, pt.X + 1, pt.Y + 1);
51     gWaterMark.DrawString(watermarkText, crFont, transparentBrush1, pt.X, pt.Y + 2);
52     gWaterMark.DrawString(watermarkText, crFont, transparentBrush1, pt.X + 2, pt.Y);
53     transparentBrush0.Dispose();
54     transparentBrush1.Dispose();
55     
56     // 画文字
57     gWaterMark.SmoothingMode = SmoothingMode.HighQuality;
58     //Brush SolidBrush3 = new SolidBrush(Color.White);
59     Brush solidBrush3 = new SolidBrush(Color.FromArgb(255, Color.White));
60     gWaterMark.DrawString(watermarkText, crFont, solidBrush3, pt.X, pt.Y, StringFormat.GenericDefault);
61     solidBrush3.Dispose();
62     
63     // 保存刚才的操作
64     gWaterMark.Save();
65     gWaterMark.Dispose();
66     bmWaterMark.Save(_wmImgSavePath, ImageFormat.Jpeg);
67     
68     //// 将水印图片加到原图中
69     //AddWatermarkImage(gCanvas, new Bitmap(bmWaterMark), "WM_TOP_LEFT", width, height);
70     
71     using (var imageAttr = new ImageAttributes())
72     {
73         ColorMap colorMap = new ColorMap();
74         colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
75         colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
76         ColorMap[] remapTable = { colorMap };
77         imageAttr.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
78         float[][] colorMatrixElements =
79         {
80             new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
81             new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
82             new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
83             new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
84             new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
85         };
86         ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
87         imageAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
88         gCanvas.DrawImage(bmWaterMark, new Rectangle(10, 10, bmWaterMark.Width, bmWaterMark.Height), 0, 0,
89             bmWaterMark.Width, bmWaterMark.Height, GraphicsUnit.Pixel, imageAttr);
90         gCanvas.Dispose();
91     }
92     bmWaterMark.Dispose();
View Code

 

代码已上传至GitHub,地址:https://github.com/hano7758/WaterMark

posted @ 2019-12-20 09:53  Mr_Sun  阅读(946)  评论(0编辑  收藏  举报