『備註』GDI+ 繪製文本有鋸齒,透明背景文本繪製

背景:

GDI+ 繪製文本 時,如果 背景是透明的 —— 則會出現 鋸齒。

1 //其實,我不用這三個 屬性 好多年了 
2 //而且,這三個屬性 在關鍵時刻還有可能 幫倒忙
3 //關鍵是:這三個屬性,鳥用都沒有 —— 不能消除鋸齒
4 g.SmoothingMode = SmoothingMode.HighQuality;
5 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
6 g.CompositingQuality = CompositingQuality.HighQuality;

 

 

解法:

 1             Bitmap bitmap0 = new Bitmap(400, 200);
 2             using (Graphics g = Graphics.FromImage(bitmap0))
 3             {
 4                 g.Clear(Color.Transparent); //以透明色 作爲背景
 5                 g.DrawString("科技救國  (透明色背景)", new Font("微軟雅黑", 14), new SolidBrush(Color.Black), new PointF(10, 10));
 6                 bitmap0.Save(@"D:\XXX\TestString0.png", ImageFormat.Png);
 7             }
 8 
 9 
10 
11             Bitmap bitmap1 = new Bitmap(400, 200);
12             using (Graphics g = Graphics.FromImage(bitmap1))
13             {
14                 g.Clear(Color.White);
15                 g.DrawString("科技救國  (白色背景)", new Font("微軟雅黑", 14), new SolidBrush(Color.Black), new PointF(10, 10));
16                 bitmap1.Save(@"D:\XXX\TestString1.png", ImageFormat.Png);
17             }
18 
19 
20             Bitmap bitmap2 = new Bitmap(bitmap1.Width, bitmap1.Height);
21             for (int x = 0; x < bitmap1.Width; x++)
22                 for (int y = 0; y < bitmap1.Height; y++)
23                 {
24                     //這段代碼還有很大的優化空間
25                     Color color = bitmap1.GetPixel(x, y);
26                     byte a = (byte)(((short)(255 - color.R) + (short)(255 - color.G) + (short)(255 - color.B)) / 3);
27                     Color color2 = Color.FromArgb(a, 0, 0, 0);
28                     bitmap2.SetPixel(x, y, color2);
29                 }
30 
31             bitmap2.Save(@"D:\XXX\TestString2.png", ImageFormat.Png);

 

結果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章