背景:
GDI+ 绘制文本 时,如果 背景是透明的 —— 则会出现 锯齿。
//其实,我不用这三个 属性 好多年了
//而且,这三个属性 在关键时刻还有可能 帮倒忙
//关键是:这三个属性,鸟用都没有 —— 不能消除锯齿
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
解法:
Bitmap bitmap0 = new Bitmap(, );
using (Graphics g = Graphics.FromImage(bitmap0))
{
g.Clear(Color.Transparent); //以透明色 作为背景
g.DrawString("科技救国 (透明色背景)", new Font("微软雅黑", ), new SolidBrush(Color.Black), new PointF(, ));
bitmap0.Save(@"D:\XXX\TestString0.png", ImageFormat.Png);
} Bitmap bitmap1 = new Bitmap(, );
using (Graphics g = Graphics.FromImage(bitmap1))
{
g.Clear(Color.White);
g.DrawString("科技救国 (白色背景)", new Font("微软雅黑", ), new SolidBrush(Color.Black), new PointF(, ));
bitmap1.Save(@"D:\XXX\TestString1.png", ImageFormat.Png);
} Bitmap bitmap2 = new Bitmap(bitmap1.Width, bitmap1.Height);
for (int x = ; x < bitmap1.Width; x++)
for (int y = ; y < bitmap1.Height; y++)
{
//这段代码还有很大的优化空间
Color color = bitmap1.GetPixel(x, y);
byte a = (byte)(((short)( - color.R) + (short)( - color.G) + (short)( - color.B)) / );
Color color2 = Color.FromArgb(a, , , );
bitmap2.SetPixel(x, y, color2);
} bitmap2.Save(@"D:\XXX\TestString2.png", ImageFormat.Png);
结果: