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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM