GDI画图时的锯齿效果弱化
当在画图时,图形有时会出现锯齿,可以使用SmoothingMode.AntiAlias来消除
代码如下:
g.SmoothingMode = SmoothingMode.AntiAlias;
使图像的边缘圆滑清晰锐化的可以试试FillPath
代码如下
g.FillPath((Brushes.Black), path);
或者是针对于文本锯齿的话,可以采用TextRenderingHint
代码如下
SolidBrush brush = new SolidBrush(Color.Green);
e.Graphics.DrawString("ABCDEFGHIJKL", new Font("宋体", 15f), brush, 0, 20);
//消除锯齿
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.DrawString("ABCDEFGHIJKL", new Font("宋体", 15f), brush, 0, 50);
有时候使用Graphics.Clear()方法,会在图像上出现颗粒型的点,尽量不要采用Graphics.Clear()来填充区域,一般使用Graphics.FillPath();
有兴趣的可以尝试下下面的代码:
public Form11()
{
InitializeComponent();
this.BackColor = Color.Black;
Method();
this.Paint += Draw;
}
Bitmap bitmap1;
private void Draw(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(bitmap1, 0, 0);
e.Graphics.DrawString("ABCDEFGHIJKL", new Font("宋体", 15f), new SolidBrush(Color.Green), 0, 50);
}
private void Method()
{
bitmap1 = new Bitmap(500, 500);
Graphics g = Graphics.FromImage(bitmap1);
GraphicsPath path = new GraphicsPath();
//g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
//g.CompositingMode = CompositingMode.SourceCopy;
//g.TextRenderingHint = TextRenderingHint.AntiAlias;
int alpha = 1;
for (int i = 20; i > 2; i--)
{
path.Reset();
path.AddEllipse(20, 20, 50, 50);
path.Widen(new Pen(Color.Black, i));
g.SetClip(path);
//g.Clear(Color.Black); // wrong
g.ResetClip();
//g.FillPath((Brushes.Black), path); // right
g.FillPath(new SolidBrush(Color.FromArgb(alpha += 10, 255, 255, 255)), path);
}
}